diff --git a/src/main/java/world/bentobox/aoneblock/AOneBlock.java b/src/main/java/world/bentobox/aoneblock/AOneBlock.java index 0f64671..2a63c99 100644 --- a/src/main/java/world/bentobox/aoneblock/AOneBlock.java +++ b/src/main/java/world/bentobox/aoneblock/AOneBlock.java @@ -24,7 +24,9 @@ import world.bentobox.aoneblock.listeners.ItemsAdderListener; import world.bentobox.aoneblock.listeners.JoinLeaveListener; import world.bentobox.aoneblock.listeners.NoBlockHandler; +import world.bentobox.aoneblock.oneblocks.OneBlockCustomBlockCreator; import world.bentobox.aoneblock.oneblocks.OneBlocksManager; +import world.bentobox.aoneblock.oneblocks.customblock.ItemsAdderCustomBlock; import world.bentobox.aoneblock.requests.IslandStatsHandler; import world.bentobox.aoneblock.requests.LocationStatsHandler; import world.bentobox.bentobox.api.addons.GameModeAddon; @@ -57,6 +59,8 @@ public void onLoad() { // Check if ItemsAdder exists, if yes register listener if (Bukkit.getPluginManager().getPlugin("ItemsAdder") != null) { registerListener(new ItemsAdderListener(this)); + OneBlockCustomBlockCreator.register(ItemsAdderCustomBlock::fromId); + OneBlockCustomBlockCreator.register("itemsadder", ItemsAdderCustomBlock::fromMap); hasItemsAdder = true; } // Save the default config from config.yml diff --git a/src/main/java/world/bentobox/aoneblock/listeners/BlockListener.java b/src/main/java/world/bentobox/aoneblock/listeners/BlockListener.java index 663c120..4ba143d 100644 --- a/src/main/java/world/bentobox/aoneblock/listeners/BlockListener.java +++ b/src/main/java/world/bentobox/aoneblock/listeners/BlockListener.java @@ -41,7 +41,6 @@ import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; -import dev.lone.itemsadder.api.CustomBlock; import world.bentobox.aoneblock.AOneBlock; import world.bentobox.aoneblock.dataobjects.OneBlockIslands; import world.bentobox.aoneblock.events.MagicBlockEntityEvent; @@ -435,13 +434,6 @@ private void breakBlock(@Nullable Player player, Block block, @NonNull OneBlockO private void spawnBlock(@NonNull OneBlockObject nextBlock, @NonNull Block block) { if (nextBlock.isCustomBlock()) { nextBlock.getCustomBlock().execute(addon, block); - } else if (nextBlock.isItemsAdderBlock()) { - // Get Custom Block from ItemsAdder and place it - CustomBlock cBlock = CustomBlock.getInstance(nextBlock.getItemsAdderBlock()); - if (cBlock != null) { - block.getLocation().getBlock().setType(Material.AIR); - cBlock.place(block.getLocation()); - } } else { @NonNull Material type = nextBlock.getMaterial(); diff --git a/src/main/java/world/bentobox/aoneblock/oneblocks/OneBlockObject.java b/src/main/java/world/bentobox/aoneblock/oneblocks/OneBlockObject.java index 64646f8..b86eb69 100644 --- a/src/main/java/world/bentobox/aoneblock/oneblocks/OneBlockObject.java +++ b/src/main/java/world/bentobox/aoneblock/oneblocks/OneBlockObject.java @@ -42,7 +42,6 @@ public enum Rarity { private Map chest; private Rarity rarity; private OneBlockCustomBlock customBlock; - private String itemsAdderBlock; private int prob; /** @@ -78,18 +77,6 @@ public OneBlockObject(OneBlockCustomBlock customBlock, int prob) { this.prob = prob; } - /** - * An ItemsAdder block - * - * @param namedSpaceID - ItemsAdder block - * @param prob - relative probability - */ - - public OneBlockObject(String namedSpaceID, int prob) { - this.itemsAdderBlock = namedSpaceID; - this.prob = prob; - } - /** * A chest * @@ -113,7 +100,6 @@ public OneBlockObject(OneBlockObject ob) { this.rarity = ob.getRarity(); this.prob = ob.getProb(); this.customBlock = ob.getCustomBlock(); - this.itemsAdderBlock = ob.getItemsAdderBlock(); } /** @@ -147,11 +133,6 @@ public OneBlockCustomBlock getCustomBlock() { return customBlock; } - /** - * @return the itemsAdderBlock - */ - public String getItemsAdderBlock() { return itemsAdderBlock; } - /** * @return the isMaterial @@ -176,13 +157,6 @@ public boolean isCustomBlock() { return customBlock != null; } - /** - * @return the isItemsAdderBlock - */ - public boolean isItemsAdderBlock() { - return itemsAdderBlock != null; - } - /** * @return the rarity */ diff --git a/src/main/java/world/bentobox/aoneblock/oneblocks/OneBlockPhase.java b/src/main/java/world/bentobox/aoneblock/oneblocks/OneBlockPhase.java index 9f9efbe..af59553 100644 --- a/src/main/java/world/bentobox/aoneblock/oneblocks/OneBlockPhase.java +++ b/src/main/java/world/bentobox/aoneblock/oneblocks/OneBlockPhase.java @@ -165,17 +165,6 @@ public void addCustomBlock(OneBlockCustomBlock customBlock, int prob) { probMap.put(total, new OneBlockObject(customBlock, prob)); } - /** - * Adds a ItemsAdder's custom block and associated probability - * @param namedSpaceID - name space and ID - * @param prob - probability - */ - public void addItemsAdderCustomBlock(String namedSpaceID, int prob) { - total += prob; - blockTotal += prob; - probMap.put(total, new OneBlockObject(namedSpaceID, prob)); - } - /** * Adds an entity type and associated probability * diff --git a/src/main/java/world/bentobox/aoneblock/oneblocks/OneBlocksManager.java b/src/main/java/world/bentobox/aoneblock/oneblocks/OneBlocksManager.java index b028f3c..e551650 100644 --- a/src/main/java/world/bentobox/aoneblock/oneblocks/OneBlocksManager.java +++ b/src/main/java/world/bentobox/aoneblock/oneblocks/OneBlocksManager.java @@ -31,8 +31,6 @@ import com.google.common.base.Enums; import com.google.common.io.Files; -import dev.lone.itemsadder.api.CustomBlock; -import dev.lone.itemsadder.api.ItemsAdder; import world.bentobox.aoneblock.AOneBlock; import world.bentobox.aoneblock.dataobjects.OneBlockIslands; import world.bentobox.aoneblock.oneblocks.OneBlockObject.Rarity; @@ -489,22 +487,7 @@ void addBlocks(OneBlockPhase obPhase, ConfigurationSection phase) { if (phase.isConfigurationSection(BLOCKS)) { ConfigurationSection blocks = phase.getConfigurationSection(BLOCKS); for (String material : blocks.getKeys(false)) { - if (Material.getMaterial(material) != null) { - addMaterial(obPhase, material, Objects.toString(blocks.get(material))); - } else { - if (addon.hasItemsAdder()) { - CustomBlock block = CustomBlock.getInstance(material); - if (block != null) { - addItemsAdderBlock(obPhase, material, Objects.toString(blocks.get(material))); - } else if (ItemsAdder.getAllItems() != null) { - if (ItemsAdder.getAllItems().size() != 0) { - addon.logError("Bad block material in " + obPhase.getPhaseName() + ": " + material); - } - } - } else { - addon.logError("Bad block material in " + obPhase.getPhaseName() + ": " + material); - } - } + addMaterial(obPhase, material, Objects.toString(blocks.get(material))); } } else if (phase.isList(BLOCKS)) { List> blocks = phase.getMapList(BLOCKS); @@ -559,22 +542,6 @@ private boolean addMaterial(OneBlockPhase obPhase, String material, String proba return true; } - private void addItemsAdderBlock(OneBlockPhase obPhase, String block, String probability) { - int prob; - try { - prob = Integer.parseInt(probability); - if (prob < 1) { - addon.logWarning("Bad item weight for " + obPhase.getPhaseName() + ": " + block - + ". Must be positive number above 1."); - } else { - obPhase.addItemsAdderCustomBlock(block, prob); - } - } catch (Exception e) { - addon.logError("Bad item weight for " + obPhase.getPhaseName() + ": " + block + ". Must be a number."); - } - - } - /** * Return the current phase for the block count * diff --git a/src/main/java/world/bentobox/aoneblock/oneblocks/customblock/BlockDataCustomBlock.java b/src/main/java/world/bentobox/aoneblock/oneblocks/customblock/BlockDataCustomBlock.java index 1f77702..0acea87 100644 --- a/src/main/java/world/bentobox/aoneblock/oneblocks/customblock/BlockDataCustomBlock.java +++ b/src/main/java/world/bentobox/aoneblock/oneblocks/customblock/BlockDataCustomBlock.java @@ -1,16 +1,15 @@ package world.bentobox.aoneblock.oneblocks.customblock; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; - import org.bukkit.Bukkit; import org.bukkit.block.Block; - import world.bentobox.aoneblock.AOneBlock; import world.bentobox.aoneblock.oneblocks.OneBlockCustomBlock; import world.bentobox.bentobox.BentoBox; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + /** * A custom block that is defined by a block data value. * diff --git a/src/main/java/world/bentobox/aoneblock/oneblocks/customblock/ItemsAdderCustomBlock.java b/src/main/java/world/bentobox/aoneblock/oneblocks/customblock/ItemsAdderCustomBlock.java new file mode 100644 index 0000000..f2ca848 --- /dev/null +++ b/src/main/java/world/bentobox/aoneblock/oneblocks/customblock/ItemsAdderCustomBlock.java @@ -0,0 +1,41 @@ +package world.bentobox.aoneblock.oneblocks.customblock; + +import dev.lone.itemsadder.api.CustomBlock; +import org.bukkit.Material; +import org.bukkit.block.Block; +import world.bentobox.aoneblock.AOneBlock; +import world.bentobox.aoneblock.oneblocks.OneBlockCustomBlock; +import world.bentobox.bentobox.BentoBox; + +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +public class ItemsAdderCustomBlock implements OneBlockCustomBlock { + private final CustomBlock customBlock; + + public ItemsAdderCustomBlock(CustomBlock customBlock) { + this.customBlock = customBlock; + } + + public static Optional fromId(String id) { + return Optional.ofNullable(CustomBlock.getInstance(id)).map(ItemsAdderCustomBlock::new); + } + + public static Optional fromMap(Map map) { + return Optional + .ofNullable(Objects.toString(map.get("id"), null)) + .flatMap(ItemsAdderCustomBlock::fromId); + } + + @Override + public void execute(AOneBlock addon, Block block) { + try { + block.setType(Material.AIR); + customBlock.place(block.getLocation()); + } catch (Exception e) { + BentoBox.getInstance().logError("Could not place custom block " + customBlock.getId() + " for block " + block.getType()); + block.setType(Material.STONE); + } + } +} diff --git a/src/main/java/world/bentobox/aoneblock/oneblocks/customblock/MobCustomBlock.java b/src/main/java/world/bentobox/aoneblock/oneblocks/customblock/MobCustomBlock.java index 6e95a96..ce03a8c 100644 --- a/src/main/java/world/bentobox/aoneblock/oneblocks/customblock/MobCustomBlock.java +++ b/src/main/java/world/bentobox/aoneblock/oneblocks/customblock/MobCustomBlock.java @@ -42,7 +42,7 @@ public static Optional fromMap(Map map) { EntityType entityType = maybeEntity(entityTypeValue); Material underlyingBlock = Material.getMaterial(underlyingBlockValue); - if(underlyingBlock == null){ + if (underlyingBlock == null) { BentoBox.getInstance().logWarning("Underlying block " + underlyingBlockValue + " does not exist and will be replaced with STONE."); }