From b9aefaab1dc9361c580d1d66b039f1d38a2754bf Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 6 Mar 2021 15:14:59 -0800 Subject: [PATCH] Add support for Enderchest inventory switching https://github.com/BentoBoxWorld/InvSwitcher/issues/16 --- README.md | 35 +++++++++++++------ .../wasteofplastic/invswitcher/Settings.java | 20 +++++++++-- .../com/wasteofplastic/invswitcher/Store.java | 10 +++++- .../dataObjects/InventoryStorage.java | 19 ++++++++++ src/main/resources/config.yml | 3 ++ .../wasteofplastic/invswitcher/StoreTest.java | 1 + 6 files changed, 73 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index c9d353d..311ee1c 100644 --- a/README.md +++ b/README.md @@ -9,29 +9,42 @@ The following are switched per-world: * Food level * Experience * Health +* Enderchest contents ## How to use 1. Place the addon jar in the addons folder of the BentoBox plugin 2. Restart the server 3. Done! +4. (Optional) If you would prefer to have achievements not broadcasted in your server chat when players change worlds, run the command `/gamerule announceAdvancements false` in-game, or in your server console by removing the "/" symbol. ## Config.yml -There is no config.yml. +The config allows to define which worlds that InvSwitcher should operate, and what aspects should be kept separate. + +``` +# Worlds to operate. Nether and End worlds are automatically included. +worlds: +- acidisland_world +- oneblock_world +- boxed_world +- bskyblock_world +options: + # + # Per-world settings. Gamemode means Survivial, Creative, etc. + inventory: true + health: true + food: true + advancements: true + gamemode: true + experience: true + location: true + ender-chest: true +``` ## Commands There are no commands. ## What it does -This addon will give players a separate inventory, health, food level, advancements and experience for each gamemode installed and their corresponding worlds. It enables players to play each gamemode independently of each other. - -## An example -**BSkyBlock**'s Inventory, Health, Food level, Advancements and Experience are shared only between its corresponding worlds: -- BSkyBlock_world -- BSkyBlock_world_nether -- BSkyBlock_world_the_end - -**Please note:** -- It is not limited to just BentoBox worlds. It applies to all worlds on the server (right now). +This addon will give players a separate inventory, enderchest, health, food level, advancements and experience for each gamemode installed and their corresponding worlds. It enables players to play each gamemode independently of each other. diff --git a/src/main/java/com/wasteofplastic/invswitcher/Settings.java b/src/main/java/com/wasteofplastic/invswitcher/Settings.java index 6fd6311..8b02678 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/Settings.java +++ b/src/main/java/com/wasteofplastic/invswitcher/Settings.java @@ -1,13 +1,13 @@ package com.wasteofplastic.invswitcher; +import java.util.HashSet; +import java.util.Set; + import world.bentobox.bentobox.api.configuration.ConfigComment; import world.bentobox.bentobox.api.configuration.ConfigEntry; import world.bentobox.bentobox.api.configuration.ConfigObject; import world.bentobox.bentobox.api.configuration.StoreAt; -import java.util.HashSet; -import java.util.Set; - @StoreAt(filename = "config.yml", path = "addons/InvSwitcher") public class Settings implements ConfigObject { @@ -32,6 +32,8 @@ public class Settings implements ConfigObject { private boolean experience = true; @ConfigEntry(path = "options.location") private boolean location = true; + @ConfigEntry(path = "options.ender-chest") + private boolean enderChest = true; /** * @return the worlds */ @@ -128,6 +130,18 @@ public boolean isLocation() { public void setLocation(boolean location) { this.location = location; } + /** + * @return the enderChest + */ + public boolean isEnderChest() { + return enderChest; + } + /** + * @param enderChest the enderChest to set + */ + public void setEnderChest(boolean enderChest) { + this.enderChest = enderChest; + } } diff --git a/src/main/java/com/wasteofplastic/invswitcher/Store.java b/src/main/java/com/wasteofplastic/invswitcher/Store.java index 6b9020b..582c28b 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/Store.java +++ b/src/main/java/com/wasteofplastic/invswitcher/Store.java @@ -122,6 +122,9 @@ public void getInventory(Player player, World world) { // Get Spawn Point store.getLocation(worldName); } + if (addon.getSettings().isEnderChest()) { + player.getEnderChest().setContents(store.getEnderChest(overworldName).toArray(new ItemStack[0])); + } } public void removeFromCache(Player player) { @@ -198,6 +201,11 @@ public void storeAndSave(Player player, World world) { } } } + if (addon.getSettings().isEnderChest()) { + // Copy the player's ender chest items to the store + List contents = Arrays.asList(player.getEnderChest().getContents()); + store.setEnderChest(overworldName, contents); + } database.saveObjectAsync(store); } @@ -211,7 +219,7 @@ private void clearPlayer(Player player) { AdvancementProgress p = player.getAdvancementProgress(a); p.getAwardedCriteria().forEach(p::revokeCriteria); } - + player.getEnderChest().clear(); } //new Exp Math from 1.8 diff --git a/src/main/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorage.java b/src/main/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorage.java index 46b97ab..2586986 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorage.java +++ b/src/main/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorage.java @@ -34,6 +34,8 @@ public class InventoryStorage implements DataObject { private Map gameMode = new HashMap<>(); @Expose private Map>> advancements = new HashMap<>(); + @Expose + private Map> enderChest = new HashMap<>(); @Override public String getUniqueId() { @@ -176,5 +178,22 @@ public Map> getAdvancements(String worldName) { return advancements.getOrDefault(worldName, Collections.emptyMap()); } + /** + * Get the EnderChest inventory + * @param overworldName - world name + * @return inventory + */ + public List getEnderChest(String overworldName) { + return enderChest == null ? new ArrayList<>() : enderChest.getOrDefault(overworldName, new ArrayList<>()); + } + /** + * + * @param worldname the world name + * @param inventory the inventory to set + */ + public void setEnderChest(String worldname, List inventory) { + this.enderChest.put(worldname, inventory); + + } } \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 6a5868e..8ceb951 100755 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -5,6 +5,8 @@ worlds: - oneblock_world - boxed_world - bskyblock_world +- skyblock-world +- caveblock-world options: # # Per-world settings. Gamemode means Survivial, Creative, etc. @@ -15,3 +17,4 @@ options: gamemode: true experience: true location: true + ender-chest: true diff --git a/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java b/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java index 00a2e21..e341afc 100644 --- a/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java +++ b/src/test/java/com/wasteofplastic/invswitcher/StoreTest.java @@ -70,6 +70,7 @@ public void setUp() { ItemStack[] contents = { new ItemStack(Material.ACACIA_BOAT, 1), null, new ItemStack(Material.BAKED_POTATO, 32), null, null, new ItemStack(Material.CAVE_SPIDER_SPAWN_EGG, 2) }; when(inv.getContents()).thenReturn(contents); when(player.getInventory()).thenReturn(inv); + when(player.getEnderChest()).thenReturn(inv); // World mock when(world.getName()).thenReturn("world_the_end_nether");