Fixed stuff and did stuff and i forgot sorry to anyone reading this

This commit is contained in:
Teriuihi 2022-01-02 06:15:07 +01:00
parent 861d3ea2a3
commit d45c2f9ace
13 changed files with 64 additions and 58 deletions

View File

@ -25,12 +25,12 @@ public class GUIListener implements Listener {
GUI gui = GUI.GUIByUUID.get(player.getUniqueId()); GUI gui = GUI.GUIByUUID.get(player.getUniqueId());
if (gui == null || gui.getInventory() == null) { if (gui == null || gui.getInventory() == null) {
if (event.getSlotType().equals(InventoryType.SlotType.CRAFTING) && event.getRawSlot() < 2) // if (event.getSlotType().equals(InventoryType.SlotType.CRAFTING) && event.getRawSlot() < 2)
event.setCancelled(true); // event.setCancelled(true);
else if (event.getRawSlot() == 2 && event.getSlotType().equals(InventoryType.SlotType.RESULT)) { // else if (event.getRawSlot() == 2 && event.getSlotType().equals(InventoryType.SlotType.RESULT)) {
event.setCancelled(true); // event.setCancelled(true);
onResultSlotClick(event, gui); // onResultSlotClick(event, gui);
} // }
return; return;
} }
if (!gui.getInventory().equals(event.getInventory())) { if (!gui.getInventory().equals(event.getInventory())) {

View File

@ -35,7 +35,7 @@ public class BuyGUI extends GUIMerchant {
0))) 0)))
)), villagerType); )), villagerType);
for (ItemStack itemStack : villagerType.getBuying()) { for (ItemStack itemStack : villagerType.getBuying()) {
Price price = Utilities.getPrice(itemStack); Price price = Utilities.getPrice(itemStack, WorthConfig.buy);
if (price == null) if (price == null)
continue; continue;
addItem(itemStack, addItem(itemStack,

View File

@ -14,7 +14,6 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import java.util.List;
import java.util.Objects; import java.util.Objects;
public class OpenGUI extends GUIInventory { public class OpenGUI extends GUIInventory {

View File

@ -37,7 +37,7 @@ public class SellGUI extends GUIMerchant {
.requireNonNullElse(econUser.getPointsMap().get(villagerType.getName()) .requireNonNullElse(econUser.getPointsMap().get(villagerType.getName())
, 0))))), villagerType); , 0))))), villagerType);
for (ItemStack itemStack : villagerType.getSelling()) { for (ItemStack itemStack : villagerType.getSelling()) {
Price price = Utilities.getPrice(itemStack); Price price = Utilities.getPrice(itemStack, WorthConfig.sell);
if (price == null) if (price == null)
continue; continue;
addItem(itemStack, addItem(itemStack,

View File

@ -46,12 +46,10 @@ public class CommandManager implements CommandExecutor, TabExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String cmd, @NotNull String[] args) { public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String cmd, @NotNull String[] args) {
if (args.length == 0) { if (args.length == 0) {
commandSender.sendMiniMessage(Config.HELP_MESSAGE_WRAPPER, List.of( commandSender.sendMiniMessage(Config.HELP_MESSAGE_WRAPPER.replaceAll("<config>", subCommands.stream()
Template.template("commands", subCommands.stream()
.filter(subCommand -> commandSender.hasPermission(subCommand.getPermission())) .filter(subCommand -> commandSender.hasPermission(subCommand.getPermission()))
.map(SubCommand::getHelpMessage) .map(SubCommand::getHelpMessage)
.collect(Collectors.joining("\n"))) .collect(Collectors.joining("\n"))), null);
));
return true; return true;
} }

View File

@ -22,12 +22,11 @@ public class CommandHelp extends SubCommand {
@Override @Override
public boolean onCommand(CommandSender commandSender, String[] args) { public boolean onCommand(CommandSender commandSender, String[] args) {
commandSender.sendMiniMessage(Config.HELP_MESSAGE_WRAPPER, commandSender.sendMiniMessage(Config.HELP_MESSAGE_WRAPPER.replaceAll("<commands>", commandManager
List.of(Template.template("commands", commandManager
.getSubCommands().stream() .getSubCommands().stream()
.filter(subCommand -> commandSender.hasPermission(subCommand.getPermission())) .filter(subCommand -> commandSender.hasPermission(subCommand.getPermission()))
.map(SubCommand::getHelpMessage) .map(SubCommand::getHelpMessage)
.collect(Collectors.joining("\n"))))); .collect(Collectors.joining("\n"))), null);
return true; return true;
} }

View File

@ -128,7 +128,6 @@ public final class Config extends AbstractConfig {
villagerType.getString("name"), villagerType.getString("name"),
loadProducts(villagerType.getConfigurationSection("buying")), loadProducts(villagerType.getConfigurationSection("buying")),
loadProducts(villagerType.getConfigurationSection("selling")), loadProducts(villagerType.getConfigurationSection("selling")),
villagerType.getDouble("price-modifier"),
villagerType.getString("profession")) villagerType.getString("profession"))
); );
}); });

View File

@ -28,12 +28,18 @@ public class WorthConfig extends AbstractConfig {
config.readConfig(WorthConfig.class, null); config.readConfig(WorthConfig.class, null);
} }
public static Object2ObjectOpenHashMap<Material, Price> prices = new Object2ObjectOpenHashMap<>(); public static Object2ObjectOpenHashMap<Material, Price> buy = new Object2ObjectOpenHashMap<>();
public static Object2ObjectOpenHashMap<Material, Price> sell = new Object2ObjectOpenHashMap<>();
private static void loadWorth() { //TODO test after removing points private static void loadWorth() { //TODO test after removing points
prices.clear(); loadWorth("buy", buy);
ConfigurationSection worth = config.getConfigurationSection("worth"); loadWorth("sell", sell);
}
private static void loadWorth(String path, Object2ObjectOpenHashMap<Material, Price> map) {
map.clear();
ConfigurationSection worth = config.getConfigurationSection(path);
if (worth == null) { if (worth == null) {
Logger.severe("No worth in worth.yml! Stopping VillagerUI."); Logger.severe("No ? in worth.yml! Stopping VillagerUI.", path);
VillagerUI.getInstance().getServer().getPluginManager().disablePlugin(VillagerUI.getInstance()); VillagerUI.getInstance().getServer().getPluginManager().disablePlugin(VillagerUI.getInstance());
return; return;
} }
@ -43,7 +49,7 @@ public class WorthConfig extends AbstractConfig {
Logger.severe("Null key in worth.yml?"); Logger.severe("Null key in worth.yml?");
continue; continue;
} }
prices.put(Material.getMaterial(key), new Price(Utilities.round(worth.getDouble(key), 2))); map.put(Material.getMaterial(key), new Price(Utilities.round(worth.getDouble(key), 2)));
} }
} }

View File

@ -5,6 +5,7 @@ import com.alttd.config.Config;
import com.alttd.database.Queries; import com.alttd.database.Queries;
import com.alttd.objects.EconUser; import com.alttd.objects.EconUser;
import com.alttd.util.Logger; import com.alttd.util.Logger;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
@ -15,17 +16,18 @@ import java.util.UUID;
public class LoginEvent implements Listener { public class LoginEvent implements Listener {
@EventHandler @EventHandler
public void onPlayerJoin(PlayerJoinEvent event) { public void onPlayerJoin(PlayerJoinEvent event) {
final Player player = event.getPlayer();
new BukkitRunnable() { new BukkitRunnable() {
@Override @Override
public void run() { public void run() {
UUID uuid = event.getPlayer().getUniqueId(); UUID uuid = player.getUniqueId();
EconUser user = EconUser.getUser(uuid); EconUser user = EconUser.getUser(uuid);
int minutes = Queries.getMinutesSinceUpdated(uuid); int minutes = Queries.getMinutesSinceUpdated(uuid);
user.removePoints(minutes * 2); user.removePoints(minutes * 2);
if (Config.DEBUG) if (Config.DEBUG)
Logger.info("Loaded EconUser for % and removed % points", Logger.info("Loaded EconUser for % and removed % points",
event.getPlayer().getName(), String.valueOf(minutes * 2)); player.getName(), String.valueOf(minutes * 2));
} }
}.runTask(VillagerUI.getInstance()); }.runTask(VillagerUI.getInstance());
} }

View File

@ -57,20 +57,27 @@ public class EconUser {
{ {
if (points == 0) if (points == 0)
return; return;
if (points > 0) if (points > 0) {
if (points < remove) if (points < remove)
points = 0; points = 0;
else else
points -= remove; points -= remove;
else } else {
if (-points < remove) if (-points < remove)
points = 0; points = 0;
else else
points += remove; points += remove;
}
pointsMap.put(villagerType, points); pointsMap.put(villagerType, points);
if (Config.DEBUG) if (Config.DEBUG)
Logger.info("Removed % points from villagerType: % for %", Logger.info("Removed % points from villagerType: % for %",
String.valueOf(points), villagerType, uuid.toString()); String.valueOf(remove), villagerType, uuid.toString());
}
private void setPoints(String villagerType, int points) {
if (pointsMap.get(villagerType) < 0)
points *= -1;
pointsMap.put(villagerType, points);
} }
public void removePoints(int remove) { public void removePoints(int remove) {
@ -84,7 +91,8 @@ public class EconUser {
int remove = points; int remove = points;
if (remove < 0) if (remove < 0)
remove *= -1; remove *= -1;
removePoints(villagerType, points, (int) (0.9 * remove) - 10); int i = (int) (0.9 * remove) - 10;
setPoints(villagerType, i < 10 && i > -10 ? 0 : i);
}); });
} }

View File

@ -7,8 +7,8 @@ public final class Price {
private final int points; private final int points;
private static final double[] xMult = {Integer.MIN_VALUE, -4000, -2000, -500, 500, 2000, 4000, Integer.MAX_VALUE}; private static final double[] xMult = {Integer.MIN_VALUE, -4000, -2000, -500, 500, 2000, 4000, Integer.MAX_VALUE};
private static final double[] yMultBuy = {2.5, 1.75, 1.25, 1, 1.5, 2.5, 5}; private static final double[] yMultBuy = {0.4, 0.65, 0.85, 1, 1.5, 2.5, 5};
private static final double[] yMultSell = {5, 2.5, 1.5, 1, 1.25, 1.75, 2.5}; private static final double[] yMultSell = {5, 2.5, 1.5, 1, 0.85, 0.65, 0.4};
public Price(double price) { public Price(double price) {
this.price = price; this.price = price;

View File

@ -28,15 +28,13 @@ public class VillagerType {
private final String displayName; private final String displayName;
private final Set<ItemStack> buying; private final Set<ItemStack> buying;
private final Set<ItemStack> selling; private final Set<ItemStack> selling;
private final double priceModifier;
private final Villager.Profession profession; private final Villager.Profession profession;
public VillagerType(String name, String displayName, Set<ItemStack> buying, Set<ItemStack> selling, double priceModifier, String profession) { public VillagerType(String name, String displayName, Set<ItemStack> buying, Set<ItemStack> selling, String profession) {
this.name = name; this.name = name;
this.displayName = displayName; this.displayName = displayName;
this.buying = buying; this.buying = buying;
this.selling = selling; this.selling = selling;
this.priceModifier = priceModifier;
this.profession = Villager.Profession.valueOf(profession.toUpperCase()); this.profession = Villager.Profession.valueOf(profession.toUpperCase());
} }
@ -56,10 +54,6 @@ public class VillagerType {
return selling; return selling;
} }
public double getPriceModifier() {
return priceModifier;
}
public Villager.Profession getProfession() { public Villager.Profession getProfession() {
return profession; return profession;
} }

View File

@ -2,6 +2,7 @@ package com.alttd.util;
import com.alttd.config.WorthConfig; import com.alttd.config.WorthConfig;
import com.alttd.objects.Price; import com.alttd.objects.Price;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.*; import org.bukkit.inventory.*;
@ -36,14 +37,14 @@ public class Utilities {
* @param item to calculate price for * @param item to calculate price for
* @return price or int < 0 for error * @return price or int < 0 for error
*/ */
public static Price getPrice(ItemStack item) { public static Price getPrice(ItemStack item, Object2ObjectOpenHashMap<Material, Price> map) {
if (WorthConfig.prices.containsKey(item.getType())) if (map.containsKey(item.getType()))
return (WorthConfig.prices.get(item.getType())); return (map.get(item.getType()));
Price price = getWorth(item, null); Price price = getWorth(item, null, map);
if (price == null) if (price == null)
return (null); return (null);
WorthConfig.prices.put(item.getType(), price); map.put(item.getType(), price);
return (WorthConfig.prices.get(item.getType())); return (map.get(item.getType()));
} }
/** /**
@ -53,13 +54,13 @@ public class Utilities {
* @param blockedMaterial Material to ignore set to null on initial call * @param blockedMaterial Material to ignore set to null on initial call
* @return Worth of the item as a double * @return Worth of the item as a double
*/ */
private static Price getWorth(ItemStack item, Material blockedMaterial) { private static Price getWorth(ItemStack item, Material blockedMaterial, Object2ObjectOpenHashMap<Material, Price> map) {
Price price = null; Price price = null;
if (item == null) if (item == null)
return (null); return (null);
if (WorthConfig.prices.containsKey(item.getType())) if (map.containsKey(item.getType()))
return (WorthConfig.prices.get(item.getType())); return (map.get(item.getType()));
List<Recipe> recipes = Bukkit.getRecipesFor(item); List<Recipe> recipes = Bukkit.getRecipesFor(item);
for (Recipe recipe : recipes) { for (Recipe recipe : recipes) {
@ -70,7 +71,7 @@ public class Utilities {
if (!values.isEmpty() && blockedMaterial != null && values.stream() if (!values.isEmpty() && blockedMaterial != null && values.stream()
.anyMatch(itemStack -> itemStack != null && itemStack.getType().equals(blockedMaterial))) .anyMatch(itemStack -> itemStack != null && itemStack.getType().equals(blockedMaterial)))
continue; continue;
possiblePrice = getWorth(values, item.getType()); possiblePrice = getWorth(values, item.getType(), map);
if (possiblePrice == null) if (possiblePrice == null)
continue; continue;
if (price == null || price.getPrice(1) > possiblePrice.getPrice(1)) if (price == null || price.getPrice(1) > possiblePrice.getPrice(1))
@ -79,19 +80,19 @@ public class Utilities {
if (shapelessRecipe.getIngredientList().stream() if (shapelessRecipe.getIngredientList().stream()
.anyMatch(itemStack -> itemStack.getType().equals(blockedMaterial))) .anyMatch(itemStack -> itemStack.getType().equals(blockedMaterial)))
continue; continue;
possiblePrice = getWorth(shapelessRecipe.getIngredientList(), item.getType()); possiblePrice = getWorth(shapelessRecipe.getIngredientList(), item.getType(), map);
if (possiblePrice == null) if (possiblePrice == null)
continue; continue;
if (price == null || price.getPrice(1) > possiblePrice.getPrice(1)) if (price == null || price.getPrice(1) > possiblePrice.getPrice(1))
price = possiblePrice; price = possiblePrice;
} else if (recipe instanceof CampfireRecipe campfireRecipe) { } else if (recipe instanceof CampfireRecipe campfireRecipe) {
possiblePrice = getWorth(campfireRecipe.getInput(), item.getType()); possiblePrice = getWorth(campfireRecipe.getInput(), item.getType(), map);
if (possiblePrice == null) if (possiblePrice == null)
continue; continue;
if (price == null || price.getPrice(1) > possiblePrice.getPrice(1)) if (price == null || price.getPrice(1) > possiblePrice.getPrice(1))
price = possiblePrice; price = possiblePrice;
} else if (recipe instanceof StonecuttingRecipe stonecuttingRecipe) { } else if (recipe instanceof StonecuttingRecipe stonecuttingRecipe) {
possiblePrice = getWorth(stonecuttingRecipe.getInput(), item.getType()); possiblePrice = getWorth(stonecuttingRecipe.getInput(), item.getType(), map);
if (possiblePrice == null) if (possiblePrice == null)
continue; continue;
if (price == null || price.getPrice(1) > possiblePrice.getPrice(1)) if (price == null || price.getPrice(1) > possiblePrice.getPrice(1))
@ -101,7 +102,7 @@ public class Utilities {
!cookingRecipe.getInput().getType().isBlock() && !cookingRecipe.getInput().getType().isBlock() &&
!cookingRecipe.getInput().getType().equals(Material.CLAY_BALL)) //Needs exception for clay ball idk a better way to do it... !cookingRecipe.getInput().getType().equals(Material.CLAY_BALL)) //Needs exception for clay ball idk a better way to do it...
continue; continue;
possiblePrice = getWorth(cookingRecipe.getInput(), item.getType()); possiblePrice = getWorth(cookingRecipe.getInput(), item.getType(), map);
if (possiblePrice == null) if (possiblePrice == null)
continue; continue;
if (price == null || price.getPrice(1) > possiblePrice.getPrice(1)) if (price == null || price.getPrice(1) > possiblePrice.getPrice(1))
@ -118,15 +119,15 @@ public class Utilities {
* @param blockedMaterial Material to ignore set to null on initial call * @param blockedMaterial Material to ignore set to null on initial call
* @return Worth of ItemStack as a double * @return Worth of ItemStack as a double
*/ */
private static Price getWorth(List<ItemStack> items, Material blockedMaterial) { private static Price getWorth(List<ItemStack> items, Material blockedMaterial, Object2ObjectOpenHashMap<Material, Price> map) {
Price price = null; Price price = null;
for (ItemStack item : items) { for (ItemStack item : items) {
if (item == null) if (item == null)
continue; continue;
Price tmp = getWorth(new ItemStack(item.getType()), blockedMaterial); Price tmp = getWorth(new ItemStack(item.getType()), blockedMaterial, map);
if (tmp == null || tmp.getPrice(1) == -1) if (tmp == null || tmp.getPrice(1) == -1)
return null; return null;
WorthConfig.prices.put(item.getType(), tmp); map.put(item.getType(), tmp);
if (price == null) if (price == null)
price = tmp; price = tmp;
else else