Added buy/points/sell command
This commit is contained in:
parent
64cb948038
commit
219c9024bd
|
|
@ -66,7 +66,7 @@ public class BuyGUI extends GUIMerchant {
|
|||
int itemPts = (int) (Math.floor(price.getPrice(1) / WorthConfig.POINT_MOD) + 1);
|
||||
int transPts = itemPts * amount;
|
||||
EconUser econUser = EconUser.getUser(player.getUniqueId());
|
||||
int oldPoints = Objects.requireNonNullElse(econUser.getPointsMap().get(villagerType.getName()), 0);
|
||||
int oldPoints = econUser.getPointsMap().getOrDefault(villagerType.getName(), 0);
|
||||
double cost = price.calculatePriceThing(oldPoints, transPts, true, itemPts);
|
||||
|
||||
Purchase purchase = new Purchase(material, cost, itemPts, transPts, amount);
|
||||
|
|
|
|||
|
|
@ -74,7 +74,7 @@ public class SellGUI extends GUIMerchant {
|
|||
.mapToInt(ItemStack::getAmount).sum();
|
||||
|
||||
EconUser econUser = EconUser.getUser(player.getUniqueId());
|
||||
int oldPoints = Objects.requireNonNullElse(econUser.getPointsMap().get(villagerType.getName()), 0);
|
||||
int oldPoints = econUser.getPointsMap().getOrDefault(villagerType.getName(), 0);
|
||||
int itemPts = (int) (Math.floor(price.getPrice(1) / WorthConfig.POINT_MOD) + 1);
|
||||
int transPts = (itemPts * amount) * -1;
|
||||
double cost = price.calculatePriceThing(oldPoints, transPts, false, itemPts);
|
||||
|
|
|
|||
|
|
@ -1,10 +1,7 @@
|
|||
package com.alttd.commands;
|
||||
|
||||
import com.alttd.VillagerUI;
|
||||
import com.alttd.commands.subcommands.CommandCreateVillager;
|
||||
import com.alttd.commands.subcommands.CommandHelp;
|
||||
import com.alttd.commands.subcommands.CommandReload;
|
||||
import com.alttd.commands.subcommands.CommandRemoveVillager;
|
||||
import com.alttd.commands.subcommands.*;
|
||||
import com.alttd.config.Config;
|
||||
import com.alttd.util.Logger;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
|
|
@ -37,6 +34,9 @@ public class CommandManager implements CommandExecutor, TabExecutor {
|
|||
|
||||
subCommands = Arrays.asList(
|
||||
new CommandHelp(this),
|
||||
new CommandPoints(),
|
||||
new CommandBuy(),
|
||||
new CommandSell(),
|
||||
new CommandCreateVillager(),
|
||||
new CommandReload(),
|
||||
new CommandRemoveVillager());
|
||||
|
|
@ -77,7 +77,9 @@ public class CommandManager implements CommandExecutor, TabExecutor {
|
|||
} else {
|
||||
SubCommand subCommand = getSubCommand(args[0]);
|
||||
if (subCommand != null && commandSender.hasPermission(subCommand.getPermission()))
|
||||
res.addAll(subCommand.getTabComplete(commandSender, args));
|
||||
res.addAll(subCommand.getTabComplete(commandSender, args).stream()
|
||||
.filter(str -> str.toLowerCase().startsWith(args[args.length - 1].toLowerCase()))
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
|
|
|||
85
src/main/java/com/alttd/commands/subcommands/CommandBuy.java
Normal file
85
src/main/java/com/alttd/commands/subcommands/CommandBuy.java
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
package com.alttd.commands.subcommands;
|
||||
|
||||
import com.alttd.commands.SubCommand;
|
||||
import com.alttd.config.Config;
|
||||
import com.alttd.config.WorthConfig;
|
||||
import com.alttd.objects.EconUser;
|
||||
import com.alttd.objects.Price;
|
||||
import com.alttd.objects.VillagerType;
|
||||
import com.alttd.util.Logger;
|
||||
import com.alttd.util.Utilities;
|
||||
import net.kyori.adventure.text.minimessage.Template;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CommandBuy extends SubCommand {
|
||||
@Override
|
||||
public boolean onCommand(CommandSender commandSender, String[] args) {
|
||||
if (!(commandSender instanceof Player player)) {
|
||||
commandSender.sendMiniMessage(Config.NO_CONSOLE, null);
|
||||
return true;
|
||||
}
|
||||
if (args.length == 1) {
|
||||
//TODO open gui
|
||||
player.sendMiniMessage(getHelpMessage(), null); //TODO remove later
|
||||
return true;
|
||||
}
|
||||
if (args.length != 2) {
|
||||
player.sendMiniMessage(getHelpMessage(), null);
|
||||
return true;
|
||||
}
|
||||
Material item = Material.valueOf(args[1].toUpperCase());
|
||||
Optional<VillagerType> optionalVillagerType = VillagerType.getVillagerTypes().stream()
|
||||
.filter(villagerType -> villagerType.getBuying().stream()
|
||||
.map(ItemStack::getType)
|
||||
.anyMatch(material -> material.equals(item)))
|
||||
.findFirst();
|
||||
if (optionalVillagerType.isEmpty()) {
|
||||
player.sendMiniMessage(Config.NO_BUY_AT_SPAWN, List.of(Template.template("material", item.name())));
|
||||
return true;
|
||||
}
|
||||
VillagerType villagerType = optionalVillagerType.get();
|
||||
Price price = Utilities.getPrice(new ItemStack(item, 1), WorthConfig.buy);
|
||||
if (price == null) {
|
||||
Logger.warning("Price was null despite being impossible to be null");
|
||||
return true;
|
||||
}
|
||||
EconUser user = EconUser.getUser(player.getUniqueId());
|
||||
Integer curPoints = user.getPointsMap().getOrDefault(villagerType.getName(), 0);
|
||||
double cost = price.calculatePriceThing(curPoints, price.getPoints(), true, price.getPoints());
|
||||
player.sendMiniMessage(Config.BUY_ITEM_MESSAGE, List.of(
|
||||
Template.template("material", item.name()),
|
||||
Template.template("price", String.valueOf(cost)),
|
||||
Template.template("points", String.valueOf(price.getPoints())),
|
||||
Template.template("current_points", String.valueOf(curPoints)),
|
||||
Template.template("villager_type", villagerType.getDisplayName())
|
||||
));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "buy";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTabComplete(CommandSender commandSender, String[] args) {
|
||||
List<String> res = new ArrayList<>();
|
||||
if (args.length == 2)
|
||||
res.addAll(Arrays.stream(Material.values()).map(Material::name).collect(Collectors.toList()));
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpMessage() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,84 @@
|
|||
package com.alttd.commands.subcommands;
|
||||
|
||||
import com.alttd.commands.SubCommand;
|
||||
import com.alttd.config.Config;
|
||||
import com.alttd.objects.EconUser;
|
||||
import com.alttd.objects.VillagerType;
|
||||
import com.alttd.util.Logger;
|
||||
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.Template;
|
||||
import net.kyori.adventure.text.minimessage.template.TemplateResolver;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CommandPoints extends SubCommand {
|
||||
|
||||
static MiniMessage miniMessage = MiniMessage.miniMessage();
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender commandSender, String[] args) {
|
||||
if (!(commandSender instanceof Player player)) {
|
||||
commandSender.sendMiniMessage(Config.NO_CONSOLE, null);
|
||||
return true;
|
||||
}
|
||||
EconUser user = EconUser.getUser(player.getUniqueId());
|
||||
var ref = new Object() {
|
||||
Component message = miniMessage.deserialize(Config.POINTS_HEADER, TemplateResolver.resolving(
|
||||
Template.template("player", player.getName())));
|
||||
};
|
||||
if (args.length == 1) {
|
||||
Object2ObjectArrayMap<String, Integer> pointsMap = user.getPointsMap();
|
||||
pointsMap.keySet().forEach(key -> {
|
||||
VillagerType villagerType = VillagerType.getVillagerType(key);
|
||||
if (villagerType == null) {
|
||||
Logger.warning("Player % has unused villager type % in their point list.", player.getName(), key);
|
||||
return;
|
||||
}
|
||||
ref.message = ref.message.append(miniMessage.deserialize("\n", TemplateResolver.resolving()));
|
||||
ref.message = ref.message.append(miniMessage.deserialize(Config.POINTS_CONTENT, TemplateResolver.resolving(
|
||||
Template.template("villager_type", VillagerType.getVillagerType(key).getDisplayName()),
|
||||
Template.template("points", String.valueOf(pointsMap.getOrDefault(key, 0)))
|
||||
)));
|
||||
});
|
||||
} else if (args.length == 2){
|
||||
Object2ObjectArrayMap<String, Integer> pointsMap = user.getPointsMap();
|
||||
if (!pointsMap.containsKey(args[1])) {
|
||||
player.sendMessage(ref.message);
|
||||
return true;
|
||||
}
|
||||
ref.message = ref.message.append(miniMessage.deserialize(Config.POINTS_CONTENT, TemplateResolver.resolving(
|
||||
Template.template("villager_type", VillagerType.getVillagerType(args[1]).getDisplayName()),
|
||||
Template.template("points", String.valueOf(pointsMap.get(args[1]))))));
|
||||
} else
|
||||
player.sendMiniMessage(getHelpMessage(), null);
|
||||
|
||||
player.sendMessage(ref.message);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "points";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTabComplete(CommandSender commandSender, String[] args) {
|
||||
List<String> res = new ArrayList<>();
|
||||
if (args.length == 2)
|
||||
res.addAll(VillagerType.getVillagerTypes().stream()
|
||||
.map(VillagerType::getName)
|
||||
.collect(Collectors.toList()));
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpMessage() {
|
||||
return Config.POINTS_MESSAGE;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
package com.alttd.commands.subcommands;
|
||||
|
||||
import com.alttd.commands.SubCommand;
|
||||
import com.alttd.config.Config;
|
||||
import com.alttd.config.WorthConfig;
|
||||
import com.alttd.objects.EconUser;
|
||||
import com.alttd.objects.Price;
|
||||
import com.alttd.objects.VillagerType;
|
||||
import com.alttd.util.Logger;
|
||||
import com.alttd.util.Utilities;
|
||||
import net.kyori.adventure.text.minimessage.Template;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CommandSell extends SubCommand {
|
||||
@Override
|
||||
public boolean onCommand(CommandSender commandSender, String[] args) {
|
||||
if (!(commandSender instanceof Player player)) {
|
||||
commandSender.sendMiniMessage(Config.NO_CONSOLE, null);
|
||||
return true;
|
||||
}
|
||||
if (args.length == 1) {
|
||||
//TODO open gui
|
||||
player.sendMiniMessage(getHelpMessage(), null); //TODO remove later
|
||||
return true;
|
||||
}
|
||||
if (args.length != 2) {
|
||||
player.sendMiniMessage(getHelpMessage(), null);
|
||||
return true;
|
||||
}
|
||||
Material item = Material.valueOf(args[1].toUpperCase());
|
||||
Optional<VillagerType> optionalVillagerType = VillagerType.getVillagerTypes().stream()
|
||||
.filter(villagerType -> villagerType.getSelling().stream()
|
||||
.map(ItemStack::getType)
|
||||
.anyMatch(material -> material.equals(item)))
|
||||
.findFirst();
|
||||
if (optionalVillagerType.isEmpty()) {
|
||||
player.sendMiniMessage(Config.NO_SELL_AT_SPAWN, List.of(Template.template("material", item.name())));
|
||||
return true;
|
||||
}
|
||||
VillagerType villagerType = optionalVillagerType.get();
|
||||
Price price = Utilities.getPrice(new ItemStack(item, 1), WorthConfig.sell);
|
||||
if (price == null) {
|
||||
Logger.warning("Price was null despite being impossible to be null");
|
||||
return true;
|
||||
}
|
||||
EconUser user = EconUser.getUser(player.getUniqueId());
|
||||
Integer curPoints = user.getPointsMap().getOrDefault(villagerType.getName(), 0);
|
||||
double cost = price.calculatePriceThing(curPoints, price.getPoints(), true, price.getPoints());
|
||||
player.sendMiniMessage(Config.SELL_ITEM_MESSAGE, List.of(
|
||||
Template.template("material", item.name()),
|
||||
Template.template("price", String.valueOf(cost)),
|
||||
Template.template("points", String.valueOf(-price.getPoints())),
|
||||
Template.template("current_points", String.valueOf(curPoints)),
|
||||
Template.template("villager_type", villagerType.getDisplayName())
|
||||
));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "sell";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getTabComplete(CommandSender commandSender, String[] args) {
|
||||
List<String> res = new ArrayList<>();
|
||||
if (args.length == 2)
|
||||
res.addAll(Arrays.stream(Material.values()).map(Material::name).collect(Collectors.toList()));
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpMessage() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -56,6 +56,7 @@ public final class Config extends AbstractConfig {
|
|||
|
||||
public static String HELP_MESSAGE_WRAPPER = "<gold>VillagerShopUI help:\n<commands></gold>";
|
||||
public static String HELP_MESSAGE = "<green>Show this menu: <gold>/villagerui help</gold></green>";
|
||||
public static String POINTS_MESSAGE = "<green>Show points: <gold>/villagerui points [villagerType]</green>";
|
||||
public static String RELOAD_MESSAGE = "<green>Reload configs: <gold>/villagerui reload</gold></green>";
|
||||
public static String CREATE_VILLAGER_MESSAGE = "<green>Create a new trading villager: <gold>/villagerui createvillager <type> <biome> <x> <y> <z> <yaw> <pitch> <world></gold></green>";
|
||||
public static String REMOVE_VILLAGER_MESSAGE = "<green>Removes all existing trading villagers in a 2 block radius: <gold>/villagerui removevillager</gold></green>";
|
||||
|
|
@ -63,6 +64,7 @@ public final class Config extends AbstractConfig {
|
|||
private static void loadHelp() {
|
||||
HELP_MESSAGE_WRAPPER = config.getString("help.help-wrapper", HELP_MESSAGE_WRAPPER);
|
||||
HELP_MESSAGE = config.getString("help.help", HELP_MESSAGE);
|
||||
POINTS_MESSAGE = config.getString("help.points", POINTS_MESSAGE);
|
||||
RELOAD_MESSAGE = config.getString("help.reload", RELOAD_MESSAGE);
|
||||
CREATE_VILLAGER_MESSAGE = config.getString("help.create-villager", CREATE_VILLAGER_MESSAGE);
|
||||
REMOVE_VILLAGER_MESSAGE = config.getString("help.remove-villager", REMOVE_VILLAGER_MESSAGE);
|
||||
|
|
@ -99,6 +101,14 @@ public final class Config extends AbstractConfig {
|
|||
public static String SOLD_ITEM = "<green>You sold <amount> <item> for <price> and got <points> points for a total of " +
|
||||
"<total_points> for <villager_name>!</green>";
|
||||
public static String REMOVED_VILLAGER = "<green>Removed villager with uuid <uuid></green>";
|
||||
public static String POINTS_HEADER = "<gold>Villager points for <player>: ";
|
||||
public static String POINTS_CONTENT = "<gold><villager_type>: <dark_aqua><points></dark_aqua></gold>";
|
||||
public static String BUY_ITEM_MESSAGE = "<green><material> can be bought at spawn at the <villager_type> villager for $<price> and <points> points per item " +
|
||||
"at your current amount of points (<current_points>).</green>";
|
||||
public static String NO_BUY_AT_SPAWN = "<red><material> can not be bought at spawn, try a player shop!</red>";
|
||||
public static String SELL_ITEM_MESSAGE = "<green><material> can be sold to spawn at the <villager_type> villager for $<price> and <points> points per item " +
|
||||
"at your current amount of points (<current_points>).</green>";
|
||||
public static String NO_SELL_AT_SPAWN = "<red><material> can not be sold to spawn, try a player shop!</red>";
|
||||
|
||||
private static void loadMessages() {
|
||||
NOT_ENOUGH_MONEY = config.getString("messages.not-enough-money", NOT_ENOUGH_MONEY);
|
||||
|
|
@ -107,6 +117,12 @@ public final class Config extends AbstractConfig {
|
|||
PURCHASED_ITEM = config.getString("messages.purchased-item", PURCHASED_ITEM);
|
||||
SOLD_ITEM = config.getString("messages.sold-item", SOLD_ITEM);
|
||||
REMOVED_VILLAGER = config.getString("messages.removed-villager", REMOVED_VILLAGER);
|
||||
POINTS_HEADER = config.getString("messages.points-header", POINTS_HEADER);
|
||||
POINTS_CONTENT = config.getString("messages.points-content", POINTS_CONTENT);
|
||||
BUY_ITEM_MESSAGE = config.getString("messages.buy-item-message", BUY_ITEM_MESSAGE);
|
||||
NO_BUY_AT_SPAWN = config.getString("messages.no-buy-at-spawn", NO_BUY_AT_SPAWN);
|
||||
SELL_ITEM_MESSAGE = config.getString("messages.sell-item-message", SELL_ITEM_MESSAGE);
|
||||
NO_SELL_AT_SPAWN = config.getString("messages.no-sell-at-spawn", NO_SELL_AT_SPAWN);
|
||||
}
|
||||
|
||||
public static boolean DEBUG = false;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package com.alttd.objects;
|
||||
|
||||
import com.alttd.config.WorthConfig;
|
||||
import com.alttd.util.Utilities;
|
||||
|
||||
public final class Price {
|
||||
|
|
@ -12,7 +13,7 @@ public final class Price {
|
|||
|
||||
public Price(double price) {
|
||||
this.price = price;
|
||||
this.points = (int) price;
|
||||
this.points = (int) (Math.floor(price / WorthConfig.POINT_MOD) + 1);
|
||||
}
|
||||
|
||||
public static Price addPrice(Price one, Price two) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user