Skip to content

Commit

Permalink
Fix issue where seed worlds lost generators with Multiverse on restart
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Dec 19, 2023
1 parent b2f5a44 commit 1ef55a2
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 86 deletions.
53 changes: 30 additions & 23 deletions src/main/java/world/bentobox/bentobox/api/panels/Panel.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,24 @@ public class Panel implements HeadRequester, InventoryHolder {
private World world;

/**
* Various types of Panel that can be created.
* Various types of Panels that can be created that use InventoryTypes.
* <br>
* The current list of inventories that cannot be created are:<br>
* <blockquote>
* {@link Type#INVENTORY}, {@link Type#HOPPER} and
* {@link Type#DROPPER}
* </blockquote>
*
* These relate to the Bukkit inventories with INVENTORY being the standard CHEST inventory.
* See {@link org.bukkit.event.inventory.InventoryType}.
* @since 1.7.0
*/
public enum Type {
INVENTORY,
HOPPER,
DROPPER
INVENTORY, HOPPER, DROPPER
}

public Panel() {}
public Panel() {
}

public Panel(String name, Map<Integer, PanelItem> items, int size, User user, PanelListener listener) {
this(name, items, size, user, listener, Type.INVENTORY);
Expand All @@ -65,28 +73,27 @@ public Panel(PanelBuilder pb) {
pb.getUser(), pb.getListener(), pb.getPanelType());
}

protected void makePanel(String name, Map<Integer, PanelItem> items, int size, User user,
PanelListener listener) {
protected void makePanel(String name, Map<Integer, PanelItem> items, int size, User user, PanelListener listener) {
this.makePanel(name, items, size, user, listener, Type.INVENTORY);
}

/**
* @since 1.7.0
*/
protected void makePanel(String name, Map<Integer, PanelItem> items, int size, User user,
PanelListener listener, Type type) {
protected void makePanel(String name, Map<Integer, PanelItem> items, int size, User user, PanelListener listener,
Type type) {
this.name = name;
this.items = items;

// Create panel
switch (type) {
case INVENTORY -> inventory = Bukkit.createInventory(null, fixSize(size), name);
case HOPPER -> inventory = Bukkit.createInventory(null, InventoryType.HOPPER, name);
case DROPPER -> inventory = Bukkit.createInventory(null, InventoryType.DROPPER, name);
case INVENTORY -> inventory = Bukkit.createInventory(null, fixSize(size), name);
case HOPPER -> inventory = Bukkit.createInventory(null, InventoryType.HOPPER, name);
case DROPPER -> inventory = Bukkit.createInventory(null, InventoryType.DROPPER, name);
}

// Fill the inventory and return
for (Map.Entry<Integer, PanelItem> en: items.entrySet()) {
for (Map.Entry<Integer, PanelItem> en : items.entrySet()) {
if (en.getKey() < 54) {
inventory.setItem(en.getKey(), en.getValue().getItem());
// Get player head async
Expand All @@ -97,11 +104,13 @@ protected void makePanel(String name, Map<Integer, PanelItem> items, int size, U
}
this.listener = listener;
// If the listener is defined, then run setup
if (listener != null) listener.setup();
if (listener != null)
listener.setup();

// If the user is defined, then open panel immediately
this.user = user;
if (user != null) this.open(user);
if (user != null)
this.open(user);
}

private int fixSize(int size) {
Expand All @@ -113,7 +122,8 @@ private int fixSize(int size) {
// Make sure size is a multiple of 9 and is 54 max.
size = size + 8;
size -= (size % 9);
if (size > 54) size = 54;
if (size > 54)
size = 54;
} else {
return 9;
}
Expand Down Expand Up @@ -194,12 +204,10 @@ public void setUser(User user) {
public void setHead(PanelItem item) {
// Update the panel item
// Find panel item index in items and replace it once more in inventory to update it.
this.items.entrySet().stream().
filter(entry -> entry.getValue() == item).
mapToInt(Map.Entry::getKey).findFirst()
.ifPresent(index ->
// Update item inside inventory to change icon only if item is inside panel.
this.inventory.setItem(index, item.getItem()));
this.items.entrySet().stream().filter(entry -> entry.getValue() == item).mapToInt(Map.Entry::getKey).findFirst()
.ifPresent(index ->
// Update item inside inventory to change icon only if item is inside panel.
this.inventory.setItem(index, item.getItem()));
}

/**
Expand All @@ -226,5 +234,4 @@ public void setWorld(World world) {
this.world = world;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ public MultiverseCoreHook() {
public void registerWorld(World world, boolean islandWorld) {
if (islandWorld) {
// Only register generator if one is defined in the addon (is not null)
String generator = BentoBox.getInstance().getIWM().getAddon(world).map(gm -> gm.getDefaultWorldGenerator(world.getName(), "") != null).orElse(false) ? " -g " + BentoBox.getInstance().getName() : "";
String cmd1 = MULTIVERSE_IMPORT + world.getName() + " " + world.getEnvironment().name().toLowerCase(Locale.ENGLISH) + generator;
String generator = BentoBox.getInstance().getIWM().getAddon(world)
.map(gm -> gm.getDefaultWorldGenerator(world.getName(), "") != null).orElse(false)
? " -g " + BentoBox.getInstance().getName()
: "";
String cmd1 = MULTIVERSE_IMPORT + world.getName() + " "
+ world.getEnvironment().name().toLowerCase(Locale.ENGLISH) + generator;
String cmd2 = MULTIVERSE_SET_GENERATOR + BentoBox.getInstance().getName() + " " + world.getName();
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), cmd1);
if (!generator.isEmpty()) {
Expand All @@ -42,7 +46,8 @@ public void registerWorld(World world, boolean islandWorld) {
}
} else {
// Set the generator to null - this will remove any previous registration
String cmd1 = MULTIVERSE_IMPORT + world.getName() + " " + world.getEnvironment().name().toLowerCase(Locale.ENGLISH);
String cmd1 = MULTIVERSE_IMPORT + world.getName() + " "
+ world.getEnvironment().name().toLowerCase(Locale.ENGLISH);
String cmd2 = MULTIVERSE_SET_GENERATOR + "null " + world.getName();
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), cmd1);
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), cmd2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ private void seedWorld(GameModeAddon gameMode, @NonNull World world) {
World w = gameMode.getWorldSettings().isUseOwnGenerator() ? wc.createWorld()
: wc.generator(world.getGenerator()).createWorld();
w.setDifficulty(Difficulty.PEACEFUL);
// Register seed world
plugin.getIWM().addWorld(w, gameMode);
}

/**
Expand Down
Loading

0 comments on commit 1ef55a2

Please sign in to comment.