Changed upgrading to require points and use tracks
This commit is contained in:
parent
e50bf5a4b2
commit
f7088a89f3
|
|
@ -7,16 +7,17 @@ import com.alttd.fishingevent.npc.NPCType;
|
||||||
import com.alttd.fishingevent.npc.types.PrizeNPC;
|
import com.alttd.fishingevent.npc.types.PrizeNPC;
|
||||||
import com.alttd.fishingevent.npc.types.TutorialNPC;
|
import com.alttd.fishingevent.npc.types.TutorialNPC;
|
||||||
import com.alttd.fishingevent.npc.types.UpgradeNPC;
|
import com.alttd.fishingevent.npc.types.UpgradeNPC;
|
||||||
|
import com.alttd.fishingevent.objects.EnchantmentData;
|
||||||
|
import com.alttd.fishingevent.objects.EnchantmentTrack;
|
||||||
import com.alttd.fishingevent.objects.Prize;
|
import com.alttd.fishingevent.objects.Prize;
|
||||||
import com.alttd.fishingevent.objects.Rarity;
|
import com.alttd.fishingevent.objects.Rarity;
|
||||||
import com.alttd.fishingevent.util.Logger;
|
import com.alttd.fishingevent.util.Logger;
|
||||||
import com.alttd.fishingevent.util.NPCCreateData;
|
import com.alttd.fishingevent.util.NPCCreateData;
|
||||||
import com.alttd.fishingevent.util.Skin;
|
import com.alttd.fishingevent.util.Skin;
|
||||||
import dev.sergiferry.playernpc.api.NPCLib;
|
import dev.sergiferry.playernpc.api.NPCLib;
|
||||||
import net.kyori.adventure.inventory.Book;
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.NamespacedKey;
|
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
import org.bukkit.enchantments.Enchantment;
|
import org.bukkit.enchantments.Enchantment;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
@ -68,25 +69,6 @@ public class Config extends AbstractConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class UPGRADES {
|
|
||||||
private static final String prefix = "upgrades.";
|
|
||||||
public static HashSet<Enchantment> ENCHANTMENTS = new HashSet<>();
|
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
private static void load() {
|
|
||||||
ENCHANTMENTS.clear();
|
|
||||||
ENCHANTMENTS.addAll(config.getList(prefix, "enchantments", List.of()).stream().map(enchantment -> {
|
|
||||||
NamespacedKey namespacedKey = NamespacedKey.fromString(enchantment);
|
|
||||||
if (namespacedKey == null) {
|
|
||||||
config.logger.warning("Found invalid enchantment [%]", enchantment);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return Enchantment.getByKey(namespacedKey);
|
|
||||||
}).collect(Collectors.toSet()));
|
|
||||||
ENCHANTMENTS.remove(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class NPC_DATA {
|
public static class NPC_DATA {
|
||||||
private static final String prefix = "npc.";
|
private static final String prefix = "npc.";
|
||||||
private static final MiniMessage minimessage = MiniMessage.miniMessage();
|
private static final MiniMessage minimessage = MiniMessage.miniMessage();
|
||||||
|
|
@ -133,10 +115,15 @@ public class Config extends AbstractConfig {
|
||||||
NPC npc;
|
NPC npc;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case UPGRADE -> {
|
case UPGRADE -> {
|
||||||
|
Optional<List<EnchantmentTrack>> upgrades = getUpgrades(npcPrefix);
|
||||||
|
if (upgrades.isEmpty()) {
|
||||||
|
config.logger.warning("Unable to create valid upgrades for Upgrade npc: [%]", key);
|
||||||
|
return;
|
||||||
|
}
|
||||||
npc = new UpgradeNPC(
|
npc = new UpgradeNPC(
|
||||||
config.logger,
|
config.logger,
|
||||||
npcCreateData,
|
npcCreateData,
|
||||||
getUpgrades(npcPrefix)
|
upgrades.get()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
case TUTORIAL -> {
|
case TUTORIAL -> {
|
||||||
|
|
@ -167,16 +154,36 @@ public class Config extends AbstractConfig {
|
||||||
NPC_MAP.put(key, npc);
|
NPC_MAP.put(key, npc);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<Enchantment> getUpgrades(String npcPrefix) {
|
private static Optional<List<EnchantmentTrack>> getUpgrades(String npcPrefix) {
|
||||||
ArrayList<Enchantment> enchantments = new ArrayList<>();
|
ArrayList<EnchantmentTrack> enchantmentTracks = new ArrayList<>();
|
||||||
List<String> enchants = config.getStringList(npcPrefix, "enchants", List.of(Enchantment.LUCK.toString()));
|
ConfigurationSection primSection = config.getConfigurationSection(npcPrefix + "enchants");
|
||||||
for (String enchantString : enchants) {
|
if (primSection == null)
|
||||||
Enchantment enchant = Enchantment.getByName(enchantString);
|
return Optional.empty();
|
||||||
if (enchant == null)
|
for (String trackKey : primSection.getKeys(false)) {
|
||||||
|
Int2ObjectOpenHashMap<EnchantmentData> enchantmentDataMap = new Int2ObjectOpenHashMap<>();
|
||||||
|
String enchantPrefix = npcPrefix + "enchants." + trackKey + ".";
|
||||||
|
|
||||||
|
ConfigurationSection secSection = config.getConfigurationSection(enchantPrefix + "enchants");
|
||||||
|
if (secSection == null) {
|
||||||
|
config.logger.warning("Invalid section for enchant track [%]", trackKey);
|
||||||
continue;
|
continue;
|
||||||
enchantments.add(enchant);
|
}
|
||||||
|
for (String enchantmentKey : secSection.getKeys(false)) {
|
||||||
|
String enchantString = config.getString(enchantPrefix + enchantmentKey + ".", "type", Enchantment.LUCK.getName());
|
||||||
|
Enchantment enchantment = Enchantment.getByName(enchantString);
|
||||||
|
if (enchantment == null) {
|
||||||
|
config.logger.warning("Found invalid enchant [%]", enchantString);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
new EnchantmentData(enchantment,
|
||||||
|
config.getInt(enchantPrefix + enchantmentKey + ".", "level", 1),
|
||||||
|
config.getInt(enchantPrefix + enchantmentKey + ".", "price", 1));
|
||||||
|
}
|
||||||
|
enchantmentTracks.add(new EnchantmentTrack(config.getString(enchantPrefix, "internal-name", "ex1"),
|
||||||
|
MiniMessage.miniMessage().deserialize(config.getString(enchantPrefix, "display-name", "<red>Ex1</red>")),
|
||||||
|
enchantmentDataMap));
|
||||||
}
|
}
|
||||||
return enchantments;
|
return Optional.of(enchantmentTracks);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ItemStack getBook(String npcPrefix) {
|
private static ItemStack getBook(String npcPrefix) {
|
||||||
|
|
@ -219,8 +226,9 @@ public class Config extends AbstractConfig {
|
||||||
|
|
||||||
prizes.add(
|
prizes.add(
|
||||||
new Prize(itemStack,
|
new Prize(itemStack,
|
||||||
config.getString(prefix, "permission", "example.permission"),
|
config.getString(prefix, "permission", "example.permission"),
|
||||||
config.getString(prefix, "prize-name", "Prize Name"))
|
config.getString(prefix, "prize-name", "Prize Name"),
|
||||||
|
config.getInt(prefix, "prize-price", 1))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return Optional.of(prizes);
|
return Optional.of(prizes);
|
||||||
|
|
|
||||||
|
|
@ -47,11 +47,17 @@ public class Messages extends AbstractConfig {
|
||||||
|
|
||||||
public static class GUI {
|
public static class GUI {
|
||||||
private static final String prefix = "gui.";
|
private static final String prefix = "gui.";
|
||||||
public static String TITLE = "Fishing Event turn in";
|
public static String NO_FISHING_ROD = "<red>You need to have a fishing rod to use this NPC</red>";
|
||||||
|
public static String UPGRADE_GUI_NAME = "<green>Upgrade GUI</green>";
|
||||||
|
public static String PRIZES_GUI_NAME = "<green>Prizes GUI</green>";
|
||||||
|
public static String NOT_INITIALIZED = "<red>There was an error initializing this GUI, please contact staff</red>";
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private static void load() {
|
private static void load() {
|
||||||
TITLE = config.getString(prefix, "title", TITLE);
|
NO_FISHING_ROD = config.getString(prefix, "no-fishing-rod", NO_FISHING_ROD);
|
||||||
|
UPGRADE_GUI_NAME = config.getString(prefix, "upgrade-gui-name", UPGRADE_GUI_NAME);
|
||||||
|
PRIZES_GUI_NAME = config.getString(prefix, "prizes-gui-name", PRIZES_GUI_NAME);
|
||||||
|
NOT_INITIALIZED = config.getString(prefix, "not-initialized", NOT_INITIALIZED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -66,10 +72,12 @@ public class Messages extends AbstractConfig {
|
||||||
public static class NPC {
|
public static class NPC {
|
||||||
private static final String prefix = "npc.";
|
private static final String prefix = "npc.";
|
||||||
public static String ALREADY_HAVE_ROD = "<red>You already have a fishing rod</red>";
|
public static String ALREADY_HAVE_ROD = "<red>You already have a fishing rod</red>";
|
||||||
|
public static String UPGRADE_NPC_LEFT_CLICK_MESSAGE = "<green>Hey <player>. If you want to catch better fish I can upgrade your fishing rod for you! I don't work free though, if you need <gold>Fish Points</gold> you can get some by selling your catches!</green>";
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private static void load() {
|
private static void load() {
|
||||||
ALREADY_HAVE_ROD = config.getString(prefix, "already-have-rod", ALREADY_HAVE_ROD);
|
ALREADY_HAVE_ROD = config.getString(prefix, "already-have-rod", ALREADY_HAVE_ROD);
|
||||||
|
UPGRADE_NPC_LEFT_CLICK_MESSAGE = config.getString(prefix, "upgrade-npc-left-click-message", UPGRADE_NPC_LEFT_CLICK_MESSAGE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -86,4 +94,14 @@ public class Messages extends AbstractConfig {
|
||||||
ROD_LORE = config.getStringList(prefix, "rod-lore", ROD_LORE);
|
ROD_LORE = config.getStringList(prefix, "rod-lore", ROD_LORE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class OTHER_ERRORS {
|
||||||
|
private static final String prefix = "other-errors.";
|
||||||
|
public static String UNABLE_TO_CREATE_FISH = "<red>Unable to create fish, please contact a staff member</red>";
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
private static void load() {
|
||||||
|
UNABLE_TO_CREATE_FISH = config.getString(prefix, "unable-to-create-fish", UNABLE_TO_CREATE_FISH);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,35 @@
|
||||||
package com.alttd.fishingevent.fish;
|
package com.alttd.fishingevent.fish;
|
||||||
|
|
||||||
|
import com.alttd.fishingevent.FishingEvent;
|
||||||
import com.alttd.fishingevent.config.Config;
|
import com.alttd.fishingevent.config.Config;
|
||||||
import com.alttd.fishingevent.config.Fishes;
|
import com.alttd.fishingevent.config.Fishes;
|
||||||
import com.alttd.fishingevent.objects.Rarity;
|
import com.alttd.fishingevent.objects.Rarity;
|
||||||
|
import com.alttd.fishingevent.util.Logger;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||||
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||||
|
import org.bukkit.NamespacedKey;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.bukkit.persistence.PersistentDataType;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public abstract class Fish {
|
public abstract class Fish {
|
||||||
|
|
||||||
|
protected final FishingEvent fishingEvent;
|
||||||
|
protected final Logger logger;
|
||||||
|
|
||||||
|
Fish(FishingEvent fishingEvent, Logger logger) {
|
||||||
|
this.fishingEvent = fishingEvent;
|
||||||
|
this.logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an the fish item for a specific player
|
* Get an the fish item for a specific player
|
||||||
* @param player Player to get the item for
|
* @param player Player to get the item for
|
||||||
|
|
@ -42,7 +56,7 @@ public abstract class Fish {
|
||||||
|
|
||||||
public abstract Rarity getRarity();
|
public abstract Rarity getRarity();
|
||||||
|
|
||||||
public ItemStack createItem(Player player, double length) {
|
public Optional<ItemStack> createItem(Player player, double length, int points) {
|
||||||
ItemStack fishItem = getFishItem(player);
|
ItemStack fishItem = getFishItem(player);
|
||||||
ItemMeta itemMeta = fishItem.getItemMeta();
|
ItemMeta itemMeta = fishItem.getItemMeta();
|
||||||
TagResolver resolver = TagResolver.resolver(
|
TagResolver resolver = TagResolver.resolver(
|
||||||
|
|
@ -53,9 +67,21 @@ public abstract class Fish {
|
||||||
|
|
||||||
itemMeta.lore(fishLore(resolver));
|
itemMeta.lore(fishLore(resolver));
|
||||||
itemMeta.displayName(fishName());
|
itemMeta.displayName(fishName());
|
||||||
|
NamespacedKey namespacedKey;
|
||||||
|
try {
|
||||||
|
namespacedKey = NamespacedKey.fromString("points", fishingEvent);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.warning("Unable to create namespaced key for fish");
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
if (namespacedKey == null) {
|
||||||
|
logger.warning("Created null namespaced key for fish");
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
itemMeta.getPersistentDataContainer().set(namespacedKey, PersistentDataType.INTEGER, points);
|
||||||
|
|
||||||
fishItem.setItemMeta(itemMeta);
|
fishItem.setItemMeta(itemMeta);
|
||||||
return fishItem;
|
return Optional.of(fishItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -35,8 +35,13 @@ public abstract class GUI {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void open(Player player) {
|
public abstract boolean canOpen();
|
||||||
|
|
||||||
|
public boolean open(Player player) {
|
||||||
|
if (!canOpen())
|
||||||
|
return false;
|
||||||
player.openInventory(inventory);
|
player.openInventory(inventory);
|
||||||
GUIByUUID.put(player.getUniqueId(), this);
|
GUIByUUID.put(player.getUniqueId(), this);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
package com.alttd.fishingevent.gui.windows;
|
||||||
|
|
||||||
|
import com.alttd.fishingevent.config.Messages;
|
||||||
|
import com.alttd.fishingevent.gui.GUI;
|
||||||
|
import com.alttd.fishingevent.objects.Prize;
|
||||||
|
import com.alttd.fishingevent.util.Logger;
|
||||||
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.inventory.InventoryType;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class PrizesWindow extends GUI {
|
||||||
|
|
||||||
|
private final Logger logger;
|
||||||
|
|
||||||
|
public PrizesWindow(Player player, Logger logger, List<Prize> prizes) {
|
||||||
|
super(InventoryType.CHEST, MiniMessage.miniMessage().deserialize(Messages.GUI.PRIZES_GUI_NAME));
|
||||||
|
this.logger = logger;
|
||||||
|
int i = 0;
|
||||||
|
for (Prize prize : prizes) {
|
||||||
|
setItem(i, prize.itemStack().clone(), clickingPlayer -> buy(clickingPlayer, prize));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void buy(Player player, Prize prize) {
|
||||||
|
player.sendMiniMessage("<red>Buying is not implemented yet</red>", null);
|
||||||
|
//TODO implement selling
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canOpen() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,10 +1,17 @@
|
||||||
package com.alttd.fishingevent.gui.windows;
|
package com.alttd.fishingevent.gui.windows;
|
||||||
|
|
||||||
|
import com.alttd.fishingevent.FishingEvent;
|
||||||
import com.alttd.fishingevent.config.Config;
|
import com.alttd.fishingevent.config.Config;
|
||||||
|
import com.alttd.fishingevent.config.Messages;
|
||||||
import com.alttd.fishingevent.gui.GUI;
|
import com.alttd.fishingevent.gui.GUI;
|
||||||
|
import com.alttd.fishingevent.objects.EnchantmentData;
|
||||||
|
import com.alttd.fishingevent.objects.EnchantmentTrack;
|
||||||
|
import com.alttd.fishingevent.points.PointsManagement;
|
||||||
import com.alttd.fishingevent.util.Logger;
|
import com.alttd.fishingevent.util.Logger;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.NamespacedKey;
|
||||||
import org.bukkit.enchantments.Enchantment;
|
import org.bukkit.enchantments.Enchantment;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.inventory.InventoryType;
|
import org.bukkit.event.inventory.InventoryType;
|
||||||
|
|
@ -12,32 +19,51 @@ import org.bukkit.inventory.Inventory;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.PlayerInventory;
|
import org.bukkit.inventory.PlayerInventory;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.bukkit.persistence.PersistentDataContainer;
|
||||||
|
import org.bukkit.persistence.PersistentDataType;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public class UpgradeWindow extends GUI {
|
public class UpgradeWindow extends GUI {
|
||||||
|
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
|
private boolean canOpen = false;
|
||||||
|
private final FishingEvent fishingEvent;
|
||||||
|
|
||||||
public UpgradeWindow(Player player, Component name, Logger logger) {
|
public UpgradeWindow(FishingEvent fishingEvent, Player player, Logger logger, List<EnchantmentTrack> enchantmentTracks) {
|
||||||
super(InventoryType.CHEST, name);
|
super(InventoryType.CHEST, MiniMessage.miniMessage().deserialize(Messages.GUI.UPGRADE_GUI_NAME));
|
||||||
|
this.fishingEvent = fishingEvent;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
Optional<ItemStack> optionalFishingRod = getFishingRod(player);
|
Optional<ItemStack> optionalFishingRod = getFishingRod(player);
|
||||||
if (optionalFishingRod.isEmpty()) {
|
if (optionalFishingRod.isEmpty()) {
|
||||||
logger.debug("[%] has no fishing rod", player.getName());
|
player.sendMiniMessage(Messages.GUI.NO_FISHING_ROD, null);
|
||||||
return; //TODO do something to tell the player they need a rod
|
return;
|
||||||
}
|
}
|
||||||
ItemStack fishingRod = optionalFishingRod.get();
|
ItemStack fishingRod = optionalFishingRod.get();
|
||||||
for (Enchantment enchantment : Config.UPGRADES.ENCHANTMENTS) {
|
for (EnchantmentTrack enchantmentTrack : enchantmentTracks) {
|
||||||
Optional<ItemStack> optionalEnchantmentItem = getEnchantmentItem(enchantment, fishingRod);
|
int trackLevel = getTrackLevel(enchantmentTrack, fishingRod);
|
||||||
|
Optional<ItemStack> optionalEnchantmentItem = getEnchantmentItem(enchantmentTrack, trackLevel);
|
||||||
if (optionalEnchantmentItem.isEmpty())
|
if (optionalEnchantmentItem.isEmpty())
|
||||||
continue;
|
continue;
|
||||||
setItem(i, optionalEnchantmentItem.get(), clickingPlayer -> upgrade(clickingPlayer, enchantment));
|
setItem(i, optionalEnchantmentItem.get(), clickingPlayer -> upgrade(clickingPlayer, enchantmentTrack, trackLevel));
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
canOpen = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getTrackLevel(EnchantmentTrack enchantmentTrack, ItemStack fishingRod) {
|
||||||
|
Optional<NamespacedKey> namespacedKey = getNamespacedKey(fishingEvent, enchantmentTrack.getInternalName());
|
||||||
|
if (namespacedKey.isEmpty()) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
Integer trackLevel = fishingRod.getItemMeta().getPersistentDataContainer().get(namespacedKey.get(), PersistentDataType.INTEGER);
|
||||||
|
if (trackLevel == null)
|
||||||
|
trackLevel = 0;
|
||||||
|
return trackLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<ItemStack> getFishingRod(Player player) {
|
private Optional<ItemStack> getFishingRod(Player player) {
|
||||||
|
|
@ -47,17 +73,46 @@ public class UpgradeWindow extends GUI {
|
||||||
.findFirst();
|
.findFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<ItemStack> getEnchantmentItem(Enchantment enchantment, ItemStack fishingRod) {
|
private Optional<ItemStack> getEnchantmentItem(EnchantmentTrack enchantmentTrack, int trackLevel) {
|
||||||
int enchantmentLevel = fishingRod.getEnchantmentLevel(enchantment);
|
if (enchantmentTrack.getMaxLevel() == trackLevel) {
|
||||||
if (enchantment.getMaxLevel() == enchantmentLevel) {
|
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
ItemStack itemStack = new ItemStack(Material.ENCHANTED_BOOK, 1);
|
ItemStack itemStack = new ItemStack(Material.ENCHANTED_BOOK, 1);
|
||||||
itemStack.addEnchantment(enchantment, enchantmentLevel + 1);
|
Optional<EnchantmentData> optionalEnchantmentData = enchantmentTrack.nextEnchantment(trackLevel);
|
||||||
|
if (optionalEnchantmentData.isEmpty())
|
||||||
|
return Optional.empty();
|
||||||
|
EnchantmentData enchantmentData = optionalEnchantmentData.get();
|
||||||
|
itemStack.addEnchantment(enchantmentData.enchantment(), enchantmentData.level());
|
||||||
return Optional.of(itemStack);
|
return Optional.of(itemStack);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void upgrade(Player player, Enchantment enchantment) {
|
private Optional<NamespacedKey> getNamespacedKey(FishingEvent fishingEvent, String name) {
|
||||||
|
try {
|
||||||
|
NamespacedKey namespacedKey = NamespacedKey.fromString(name, fishingEvent);
|
||||||
|
if (namespacedKey != null)
|
||||||
|
return Optional.of(namespacedKey);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.warning("Error while creating namespaced key for enchant [%]", name);
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
logger.warning("Error while creating namespaced key for enchant [%]", name);
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void upgrade(Player player, EnchantmentTrack enchantmentTrack, int trackLevel) {
|
||||||
|
int playerPoints = PointsManagement.getInstance().getPoints(player.getUniqueId());
|
||||||
|
Optional<EnchantmentData> optionalEnchantmentData = enchantmentTrack.nextEnchantment(trackLevel);
|
||||||
|
if (optionalEnchantmentData.isEmpty()) {
|
||||||
|
//TODO error no next enchant prob?
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
EnchantmentData enchantmentData = optionalEnchantmentData.get();
|
||||||
|
if (playerPoints < enchantmentData.price()) {
|
||||||
|
//TODO tell player not enough points
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
PlayerInventory inventory = player.getInventory();
|
PlayerInventory inventory = player.getInventory();
|
||||||
int fishingRodSlot = findFishingRodSlot(inventory);
|
int fishingRodSlot = findFishingRodSlot(inventory);
|
||||||
ItemStack fishingRod = inventory.getItem(fishingRodSlot);
|
ItemStack fishingRod = inventory.getItem(fishingRodSlot);
|
||||||
|
|
@ -65,11 +120,35 @@ public class UpgradeWindow extends GUI {
|
||||||
logger.warning("Fishing rod became null");
|
logger.warning("Fishing rod became null");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int enchantmentLevel = fishingRod.getEnchantmentLevel(enchantment);
|
|
||||||
fishingRod.addEnchantment(enchantment, enchantmentLevel + 1);
|
try {
|
||||||
|
PointsManagement.getInstance().removePoints(player.getUniqueId(), enchantmentData.price());
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
//TODO tell player not enough points
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!updateEnchantment(fishingRod, enchantmentData, enchantmentTrack, trackLevel + 1)) {
|
||||||
|
PointsManagement.getInstance().addPoints(player.getUniqueId(), enchantmentData.price());
|
||||||
|
//TODO error
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
inventory.setItem(fishingRodSlot, fishingRod);
|
inventory.setItem(fishingRodSlot, fishingRod);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean updateEnchantment(ItemStack fishingRod, EnchantmentData enchantmentData, EnchantmentTrack enchantmentTrack, int newTrackLevel) {
|
||||||
|
fishingRod.addEnchantment(enchantmentData.enchantment(), enchantmentData.level());
|
||||||
|
ItemMeta itemMeta = fishingRod.getItemMeta();
|
||||||
|
PersistentDataContainer persistentDataContainer = itemMeta.getPersistentDataContainer();
|
||||||
|
Optional<NamespacedKey> namespacedKey = getNamespacedKey(fishingEvent, enchantmentTrack.getInternalName());
|
||||||
|
if (namespacedKey.isEmpty())
|
||||||
|
return false;
|
||||||
|
persistentDataContainer.set(namespacedKey.get(), PersistentDataType.INTEGER, newTrackLevel);
|
||||||
|
fishingRod.setItemMeta(itemMeta);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
private int findFishingRodSlot(Inventory inventory) {
|
private int findFishingRodSlot(Inventory inventory) {
|
||||||
ItemStack[] contents = inventory.getContents();
|
ItemStack[] contents = inventory.getContents();
|
||||||
|
|
||||||
|
|
@ -84,4 +163,8 @@ public class UpgradeWindow extends GUI {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canOpen() {
|
||||||
|
return canOpen;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package com.alttd.fishingevent.listeners;
|
package com.alttd.fishingevent.listeners;
|
||||||
|
|
||||||
|
import com.alttd.fishingevent.config.Messages;
|
||||||
import com.alttd.fishingevent.fish.Fish;
|
import com.alttd.fishingevent.fish.Fish;
|
||||||
import com.alttd.fishingevent.fish_generator.FishGenerator;
|
import com.alttd.fishingevent.fish_generator.FishGenerator;
|
||||||
import com.alttd.fishingevent.points.PointsManagement;
|
import com.alttd.fishingevent.points.PointsManagement;
|
||||||
|
|
@ -82,11 +83,17 @@ public class CatchFish implements Listener {
|
||||||
Fish fish = optionalFish.get();
|
Fish fish = optionalFish.get();
|
||||||
|
|
||||||
float length = fish.getLength();
|
float length = fish.getLength();
|
||||||
item.setItemStack(fish.createItem(player, length));
|
int pointsValue = pointsManagement.calculatePoints(fish.getRarity(), length);
|
||||||
|
Optional<ItemStack> fishItem = fish.createItem(player, length, pointsValue);
|
||||||
|
if (fishItem.isEmpty()) {
|
||||||
|
player.sendMiniMessage(Messages.OTHER_ERRORS.UNABLE_TO_CREATE_FISH, null);
|
||||||
|
event.setCancelled(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
item.setItemStack(fishItem.get());
|
||||||
item.setOwner(player.getUniqueId());
|
item.setOwner(player.getUniqueId());
|
||||||
int addedPoints = pointsManagement.caughtFish(player.getUniqueId(), fish.getRarity(), length);
|
|
||||||
logger.debug("[%] caught a [%] with length [%] and rarity [%] for [%] points",
|
logger.debug("[%] caught a [%] with length [%] and rarity [%] for [%] points",
|
||||||
player.getName(), fish.normalFishName(), String.format("%.2f", length),
|
player.getName(), fish.normalFishName(), String.format("%.2f", length),
|
||||||
fish.getRarity().displayName(), String.valueOf(addedPoints));
|
fish.getRarity().displayName(), String.valueOf(pointsValue));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,19 @@
|
||||||
package com.alttd.fishingevent.npc.types;
|
package com.alttd.fishingevent.npc.types;
|
||||||
|
|
||||||
import com.alttd.fishingevent.FishingEvent;
|
import com.alttd.fishingevent.FishingEvent;
|
||||||
|
import com.alttd.fishingevent.config.Messages;
|
||||||
|
import com.alttd.fishingevent.gui.windows.PrizesWindow;
|
||||||
|
import com.alttd.fishingevent.gui.windows.UpgradeWindow;
|
||||||
import com.alttd.fishingevent.npc.LibNPC;
|
import com.alttd.fishingevent.npc.LibNPC;
|
||||||
import com.alttd.fishingevent.npc.NPC;
|
import com.alttd.fishingevent.npc.NPC;
|
||||||
import com.alttd.fishingevent.objects.Prize;
|
import com.alttd.fishingevent.objects.Prize;
|
||||||
import com.alttd.fishingevent.util.Logger;
|
import com.alttd.fishingevent.util.Logger;
|
||||||
import com.alttd.fishingevent.util.NPCCreateData;
|
import com.alttd.fishingevent.util.NPCCreateData;
|
||||||
|
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
@ -17,10 +22,13 @@ public class PrizeNPC extends LibNPC implements NPC {
|
||||||
private final NPCCreateData npcCreateData;
|
private final NPCCreateData npcCreateData;
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
private boolean isSpawned = false;
|
private boolean isSpawned = false;
|
||||||
|
private final List<Prize> prizes;
|
||||||
|
private FishingEvent fishingEvent = null;
|
||||||
|
|
||||||
public PrizeNPC(Logger logger, NPCCreateData npcCreateData, List<Prize> prizes) {
|
public PrizeNPC(Logger logger, NPCCreateData npcCreateData, List<Prize> prizes) {
|
||||||
this.npcCreateData = npcCreateData;
|
this.npcCreateData = npcCreateData;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
|
this.prizes = prizes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -31,17 +39,28 @@ public class PrizeNPC extends LibNPC implements NPC {
|
||||||
@Override
|
@Override
|
||||||
public void spawnNPC(FishingEvent fishingEvent, Location location) {
|
public void spawnNPC(FishingEvent fishingEvent, Location location) {
|
||||||
defaultSpawnNPC(fishingEvent, location, npcCreateData, logger, this);
|
defaultSpawnNPC(fishingEvent, location, npcCreateData, logger, this);
|
||||||
|
this.fishingEvent = fishingEvent;
|
||||||
isSpawned = true;
|
isSpawned = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void leftClick(Player player) {
|
public void leftClick(Player player) {
|
||||||
|
player.sendMiniMessage(Messages.NPC.UPGRADE_NPC_LEFT_CLICK_MESSAGE, Placeholder.component("player", player.name()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void rightClick(Player player) {
|
public void rightClick(Player player) {
|
||||||
|
if (fishingEvent == null) {
|
||||||
|
player.sendMiniMessage(Messages.GUI.NOT_INITIALIZED, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
PrizesWindow prizesWindow = new PrizesWindow(player, logger, prizes);
|
||||||
|
new BukkitRunnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
prizesWindow.open(player);
|
||||||
|
}
|
||||||
|
}.runTaskAsynchronously(fishingEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
package com.alttd.fishingevent.npc.types;
|
||||||
|
|
||||||
|
public class SellNPC {
|
||||||
|
}
|
||||||
|
|
@ -44,7 +44,7 @@ public class TutorialNPC extends LibNPC implements NPC {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void leftClick(Player player) {
|
public void leftClick(Player player) {
|
||||||
|
//Leave empty since we're overwriting the left click action when spawning the npc
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,17 @@
|
||||||
package com.alttd.fishingevent.npc.types;
|
package com.alttd.fishingevent.npc.types;
|
||||||
|
|
||||||
import com.alttd.fishingevent.FishingEvent;
|
import com.alttd.fishingevent.FishingEvent;
|
||||||
|
import com.alttd.fishingevent.config.Messages;
|
||||||
import com.alttd.fishingevent.gui.windows.UpgradeWindow;
|
import com.alttd.fishingevent.gui.windows.UpgradeWindow;
|
||||||
import com.alttd.fishingevent.npc.LibNPC;
|
import com.alttd.fishingevent.npc.LibNPC;
|
||||||
import com.alttd.fishingevent.npc.NPC;
|
import com.alttd.fishingevent.npc.NPC;
|
||||||
|
import com.alttd.fishingevent.objects.EnchantmentTrack;
|
||||||
import com.alttd.fishingevent.util.Logger;
|
import com.alttd.fishingevent.util.Logger;
|
||||||
import com.alttd.fishingevent.util.NPCCreateData;
|
import com.alttd.fishingevent.util.NPCCreateData;
|
||||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.enchantments.Enchantment;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
@ -18,10 +20,13 @@ public class UpgradeNPC extends LibNPC implements NPC {
|
||||||
private final NPCCreateData npcCreateData;
|
private final NPCCreateData npcCreateData;
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
private boolean isSpawned = false;
|
private boolean isSpawned = false;
|
||||||
|
private final List<EnchantmentTrack> enchantmentTracks;
|
||||||
|
private FishingEvent fishingEvent = null;
|
||||||
|
|
||||||
public UpgradeNPC(Logger logger, NPCCreateData npcCreateData, List<Enchantment> upgrades) {
|
public UpgradeNPC(Logger logger, NPCCreateData npcCreateData, List<EnchantmentTrack> enchantmentTracks) {
|
||||||
this.npcCreateData = npcCreateData;
|
this.npcCreateData = npcCreateData;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
|
this.enchantmentTracks = enchantmentTracks;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -32,17 +37,32 @@ public class UpgradeNPC extends LibNPC implements NPC {
|
||||||
@Override
|
@Override
|
||||||
public void spawnNPC(FishingEvent fishingEvent, Location location) {
|
public void spawnNPC(FishingEvent fishingEvent, Location location) {
|
||||||
defaultSpawnNPC(fishingEvent, location, npcCreateData, logger, this);
|
defaultSpawnNPC(fishingEvent, location, npcCreateData, logger, this);
|
||||||
|
this.fishingEvent = fishingEvent;
|
||||||
isSpawned = true;
|
isSpawned = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void leftClick(Player player) {
|
public void leftClick(Player player) {
|
||||||
|
player.sendMiniMessage(Messages.NPC.UPGRADE_NPC_LEFT_CLICK_MESSAGE, Placeholder.component("player", player.name()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void rightClick(Player player) {
|
public void rightClick(Player player) {
|
||||||
|
if (fishingEvent == null) {
|
||||||
|
player.sendMiniMessage(Messages.GUI.NOT_INITIALIZED, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
UpgradeWindow upgradeWindow = new UpgradeWindow(fishingEvent, player, logger, enchantmentTracks);
|
||||||
|
if (!upgradeWindow.canOpen()) {
|
||||||
|
player.sendMiniMessage(Messages.GUI.NO_FISHING_ROD, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
new BukkitRunnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
upgradeWindow.open(player);
|
||||||
|
}
|
||||||
|
}.runTaskAsynchronously(fishingEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
package com.alttd.fishingevent.objects;
|
||||||
|
|
||||||
|
import org.bukkit.enchantments.Enchantment;
|
||||||
|
|
||||||
|
public record EnchantmentData(Enchantment enchantment, int level, int price) {
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
package com.alttd.fishingevent.objects;
|
||||||
|
|
||||||
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class EnchantmentTrack {
|
||||||
|
|
||||||
|
private final Component name;
|
||||||
|
private final Int2ObjectOpenHashMap<EnchantmentData> enchantmentMap;
|
||||||
|
private final String internalName;
|
||||||
|
|
||||||
|
public EnchantmentTrack(String internalName, Component name, Int2ObjectOpenHashMap<EnchantmentData> enchantmentMap) {
|
||||||
|
this.internalName = internalName.toLowerCase();
|
||||||
|
this.name = name;
|
||||||
|
this.enchantmentMap = enchantmentMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<EnchantmentData> nextEnchantment(int trackLevel) {
|
||||||
|
if (enchantmentMap.containsKey(trackLevel + 1)) {
|
||||||
|
return Optional.of(enchantmentMap.get(trackLevel + 1));
|
||||||
|
}
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Component getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getInternalName() {
|
||||||
|
return internalName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMaxLevel() {
|
||||||
|
return enchantmentMap.keySet().stream().max(Integer::compare).orElse(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,5 +2,5 @@ package com.alttd.fishingevent.objects;
|
||||||
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
public record Prize(ItemStack itemStack, String permission, String name) {
|
public record Prize(ItemStack itemStack, String permission, String name, int price) {
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,10 +23,12 @@ public class PointsManagement {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized int caughtFish(UUID uuid, Rarity rarity, float length) {
|
public int calculatePoints(Rarity rarity, float length) {
|
||||||
int pointsToAdd = (int) (Config.RARITY_MULTIPLIERS.MULTIPLIER_MAP.get(rarity) * length);
|
return (int) (Config.RARITY_MULTIPLIERS.MULTIPLIER_MAP.get(rarity) * length);
|
||||||
pointsMap.addTo(uuid, pointsToAdd);
|
}
|
||||||
return pointsToAdd;
|
|
||||||
|
public synchronized int addPoints(UUID uuid, int points) {
|
||||||
|
return pointsMap.addTo(uuid, points);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized int getPoints(UUID uuid) {
|
public synchronized int getPoints(UUID uuid) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user