From 6cb3ee22ef603a67b197e9951b30718dc3fcb4df Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sat, 2 Jan 2021 18:38:17 +0000 Subject: [PATCH] Added talisman inventories --- build.gradle | 2 +- eco-core/build.gradle | 2 +- .../com/willfp/talismans/TalismansPlugin.java | 12 +++- .../talismans/commands/CommandTalismans.java | 28 +++++++++ .../willfp/talismans/config/configs/Data.java | 9 ++- .../com/willfp/talismans/data/PlayerData.java | 7 ++- .../inventory/TalismanInventoryListeners.java | 63 +++++++++++++++++++ .../inventory/TalismanInventoryUtils.java | 25 ++++++++ .../core-plugin/src/main/resources/plugin.yml | 9 ++- 9 files changed, 148 insertions(+), 9 deletions(-) create mode 100644 eco-core/core-plugin/src/main/java/com/willfp/talismans/commands/CommandTalismans.java create mode 100644 eco-core/core-plugin/src/main/java/com/willfp/talismans/data/inventory/TalismanInventoryListeners.java create mode 100644 eco-core/core-plugin/src/main/java/com/willfp/talismans/data/inventory/TalismanInventoryUtils.java diff --git a/build.gradle b/build.gradle index 234cbc8..2d6fe9d 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ plugins { dependencies { implementation project(":eco-core").getSubprojects() - implementation 'com.willfp:eco:1.0.2' + implementation 'com.willfp:eco:1.0.3' } allprojects { diff --git a/eco-core/build.gradle b/eco-core/build.gradle index 70805d6..35647ae 100644 --- a/eco-core/build.gradle +++ b/eco-core/build.gradle @@ -3,6 +3,6 @@ version rootProject.version subprojects { dependencies { - compileOnly 'com.willfp:eco:1.0.2' + compileOnly 'com.willfp:eco:1.0.3' } } \ No newline at end of file diff --git a/eco-core/core-plugin/src/main/java/com/willfp/talismans/TalismansPlugin.java b/eco-core/core-plugin/src/main/java/com/willfp/talismans/TalismansPlugin.java index 215616e..8ad7d85 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/talismans/TalismansPlugin.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/talismans/TalismansPlugin.java @@ -6,8 +6,11 @@ import com.willfp.eco.util.interfaces.EcoRunnable; import com.willfp.eco.util.plugin.AbstractEcoPlugin; import com.willfp.eco.util.protocollib.AbstractPacketAdapter; import com.willfp.talismans.commands.CommandTaldebug; +import com.willfp.talismans.commands.CommandTalismans; import com.willfp.talismans.commands.CommandTalreload; import com.willfp.talismans.config.TalismansConfigs; +import com.willfp.talismans.data.PlayerData; +import com.willfp.talismans.data.inventory.TalismanInventoryListeners; import com.willfp.talismans.display.packets.PacketChat; import com.willfp.talismans.display.packets.PacketOpenWindowMerchant; import com.willfp.talismans.display.packets.PacketSetCreativeSlot; @@ -62,6 +65,7 @@ public class TalismansPlugin extends AbstractEcoPlugin { @Override public void disable() { this.getExtensionLoader().unloadExtensions(); + PlayerData.save(); } /** @@ -90,6 +94,8 @@ public class TalismansPlugin extends AbstractEcoPlugin { } }, 1); }); + + PlayerData.save(); } /** @@ -121,7 +127,8 @@ public class TalismansPlugin extends AbstractEcoPlugin { public List getCommands() { return Arrays.asList( new CommandTaldebug(this), - new CommandTalreload(this) + new CommandTalreload(this), + new CommandTalismans(this) ); } @@ -151,7 +158,8 @@ public class TalismansPlugin extends AbstractEcoPlugin { return Arrays.asList( new WatcherTriggers(this), new BlockPlaceListener(), - new TalismanCraftListener() + new TalismanCraftListener(), + new TalismanInventoryListeners() ); } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/talismans/commands/CommandTalismans.java b/eco-core/core-plugin/src/main/java/com/willfp/talismans/commands/CommandTalismans.java new file mode 100644 index 0000000..802955e --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/talismans/commands/CommandTalismans.java @@ -0,0 +1,28 @@ +package com.willfp.talismans.commands; + +import com.willfp.eco.util.command.AbstractCommand; +import com.willfp.eco.util.plugin.AbstractEcoPlugin; +import com.willfp.talismans.data.inventory.TalismanInventoryUtils; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public class CommandTalismans extends AbstractCommand { + /** + * Instantiate a new /talismans command handler. + * + * @param plugin The plugin for the commands to listen for. + */ + public CommandTalismans(@NotNull final AbstractEcoPlugin plugin) { + super(plugin, "talismans", "talismans.inventory", true); + } + + @Override + public void onExecute(@NotNull final CommandSender sender, + @NotNull final List args) { + Player player = (Player) sender; + TalismanInventoryUtils.showInventory(player); + } +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/talismans/config/configs/Data.java b/eco-core/core-plugin/src/main/java/com/willfp/talismans/config/configs/Data.java index 7ea706f..335bffd 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/talismans/config/configs/Data.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/talismans/config/configs/Data.java @@ -9,10 +9,15 @@ public class Data extends BaseConfig { * data.yml. */ public Data() { - super("data.yml", false); + super("data", false); } + /** + * Get the config file. + * + * @return The file. + */ public File getFile() { - return super.configFile; + return this.getConfigFile(); } } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/talismans/data/PlayerData.java b/eco-core/core-plugin/src/main/java/com/willfp/talismans/data/PlayerData.java index 04fd8e4..f016d75 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/talismans/data/PlayerData.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/talismans/data/PlayerData.java @@ -4,6 +4,7 @@ import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import com.willfp.talismans.config.TalismansConfigs; import lombok.Getter; +import lombok.Setter; import org.bukkit.OfflinePlayer; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; @@ -29,7 +30,8 @@ public final class PlayerData { * The player's talisman inventory. */ @Getter - private final Map inventory = new HashMap<>(); + @Setter + private Map inventory = new HashMap<>(); private PlayerData(@NotNull final UUID uuid) { this.uuid = uuid; @@ -76,6 +78,7 @@ public final class PlayerData { return data; } - return new PlayerData(player.getUniqueId()); + PLAYER_DATA.put(player.getUniqueId(), new PlayerData(player.getUniqueId())); + return get(player); } } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/talismans/data/inventory/TalismanInventoryListeners.java b/eco-core/core-plugin/src/main/java/com/willfp/talismans/data/inventory/TalismanInventoryListeners.java new file mode 100644 index 0000000..2cab341 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/talismans/data/inventory/TalismanInventoryListeners.java @@ -0,0 +1,63 @@ +package com.willfp.talismans.data.inventory; + +import com.willfp.talismans.data.PlayerData; +import com.willfp.talismans.talismans.util.TalismanChecks; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +public class TalismanInventoryListeners implements Listener { + /** + * On inventory close. + * + * @param event The event to listen for. + */ + @EventHandler + public void onTalismanInventoryClose(@NotNull final InventoryCloseEvent event) { + if (!event.getView().getTitle().toLowerCase().contains("talisman")) { + return; + } + Map contents = new HashMap<>(); + for (int i = 0; i < event.getInventory().getSize(); i++) { + contents.put(i, event.getInventory().getItem(i)); + } + + PlayerData.get((Player) event.getPlayer()).setInventory(contents); + } + + /** + * On add item to talisman inventory. + * + * @param event The event to listen for. + */ + @EventHandler + public void onPutItemInTalismanInventory(@NotNull final InventoryClickEvent event) { + if (!event.getView().getTitle().toLowerCase().contains("talisman")) { + return; + } + + + if (event.getClick() == ClickType.SHIFT_LEFT || event.getClick() == ClickType.SHIFT_RIGHT) { + ItemStack item = event.getCurrentItem(); + if (TalismanChecks.getTalismanOnItem(item) == null) { + event.setCancelled(true); + } + } + + if (Objects.equals(event.getClickedInventory(), event.getView().getTopInventory())) { + ItemStack item = event.getCursor(); + if (TalismanChecks.getTalismanOnItem(item) == null) { + event.setCancelled(true); + } + } + } +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/talismans/data/inventory/TalismanInventoryUtils.java b/eco-core/core-plugin/src/main/java/com/willfp/talismans/data/inventory/TalismanInventoryUtils.java new file mode 100644 index 0000000..4b141c5 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/talismans/data/inventory/TalismanInventoryUtils.java @@ -0,0 +1,25 @@ +package com.willfp.talismans.data.inventory; + +import com.willfp.talismans.data.PlayerData; +import lombok.experimental.UtilityClass; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.jetbrains.annotations.NotNull; + +@UtilityClass +public class TalismanInventoryUtils { + /** + * Show talisman inventory to player. + * + * @param player The player to show the inventory to. + * @return The created inventory. + */ + public Inventory showInventory(@NotNull final Player player) { + Inventory inventory = Bukkit.createInventory(null, 54, "Talisman Inventory"); + PlayerData.get(player).getInventory().forEach(inventory::setItem); + player.openInventory(inventory); + + return inventory; + } +} diff --git a/eco-core/core-plugin/src/main/resources/plugin.yml b/eco-core/core-plugin/src/main/resources/plugin.yml index 5e6593f..c159caa 100644 --- a/eco-core/core-plugin/src/main/resources/plugin.yml +++ b/eco-core/core-plugin/src/main/resources/plugin.yml @@ -2,7 +2,7 @@ name: Talismans version: ${projectVersion} main: com.willfp.talismans.TalismansPlugin api-version: 1.15 -authors: [Auxilor] +authors: [ Auxilor ] website: willfp.com load: STARTUP depend: @@ -28,6 +28,9 @@ commands: taldebug: description: Debug information permission: talismans.taldebug + talismans: + description: Shows talisman inventory + permission: talismans.inventory permissions: talismans.*: @@ -36,6 +39,7 @@ permissions: children: talismans.reload: true talismans.taldebug: true + talismans.talismans: true talismans.fromtable.*: true talismans.reload: @@ -44,6 +48,9 @@ permissions: talismans.taldebug: description: Allows the use of /taldebug to print verbose debug information to console default: op + talismans.inventory: + description: Allows the use of /talismans to show the player's talisman inventory. + default: true talismans.fromtable.*: description: Allows crafting all talismans default: true \ No newline at end of file