Skip to content

Commit

Permalink
Add support for Enderchest inventory switching
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Mar 6, 2021
1 parent ecd6a0f commit b9aefaa
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 15 deletions.
35 changes: 24 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
20 changes: 17 additions & 3 deletions src/main/java/com/wasteofplastic/invswitcher/Settings.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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
*/
Expand Down Expand Up @@ -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;
}


}
10 changes: 9 additions & 1 deletion src/main/java/com/wasteofplastic/invswitcher/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<ItemStack> contents = Arrays.asList(player.getEnderChest().getContents());
store.setEnderChest(overworldName, contents);
}
database.saveObjectAsync(store);
}

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class InventoryStorage implements DataObject {
private Map<String, GameMode> gameMode = new HashMap<>();
@Expose
private Map<String, Map<String, List<String>>> advancements = new HashMap<>();
@Expose
private Map<String, List<ItemStack>> enderChest = new HashMap<>();

@Override
public String getUniqueId() {
Expand Down Expand Up @@ -176,5 +178,22 @@ public Map<String, List<String>> getAdvancements(String worldName) {
return advancements.getOrDefault(worldName, Collections.emptyMap());
}

/**
* Get the EnderChest inventory
* @param overworldName - world name
* @return inventory
*/
public List<ItemStack> 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<ItemStack> inventory) {
this.enderChest.put(worldname, inventory);

}
}
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ worlds:
- oneblock_world
- boxed_world
- bskyblock_world
- skyblock-world
- caveblock-world
options:
#
# Per-world settings. Gamemode means Survivial, Creative, etc.
Expand All @@ -15,3 +17,4 @@ options:
gamemode: true
experience: true
location: true
ender-chest: true
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit b9aefaa

Please sign in to comment.