diff --git a/pom.xml b/pom.xml index 1b35e9c..41d58a1 100644 --- a/pom.xml +++ b/pom.xml @@ -59,6 +59,7 @@ 3.13.0 4.0.10 1.8.0 + 0.0.67 2.6.2 1.3.0 @@ -159,6 +160,13 @@ true false + + + momirealms-releases + https://repo.momirealms.net/releases/ + true + false + codemc-repo @@ -271,6 +279,30 @@ + + net.momirealms + craft-engine-core + ${craftengine.version} + provided + + + * + * + + + + + net.momirealms + craft-engine-bukkit + ${craftengine.version} + provided + + + * + * + + + diff --git a/src/main/java/world/bentobox/aoneblock/AOneBlock.java b/src/main/java/world/bentobox/aoneblock/AOneBlock.java index 1097865..4c4e4d6 100644 --- a/src/main/java/world/bentobox/aoneblock/AOneBlock.java +++ b/src/main/java/world/bentobox/aoneblock/AOneBlock.java @@ -22,6 +22,7 @@ import world.bentobox.aoneblock.listeners.BossBarListener; import world.bentobox.aoneblock.listeners.HoloListener; import world.bentobox.aoneblock.listeners.InfoListener; +import world.bentobox.aoneblock.listeners.CraftEngineListener; import world.bentobox.aoneblock.listeners.ItemsAdderListener; import world.bentobox.aoneblock.listeners.JoinLeaveListener; import world.bentobox.aoneblock.listeners.NexoListener; @@ -29,6 +30,7 @@ import world.bentobox.aoneblock.listeners.StartSafetyListener; import world.bentobox.aoneblock.oneblocks.OneBlockCustomBlockCreator; import world.bentobox.aoneblock.oneblocks.OneBlocksManager; +import world.bentobox.aoneblock.oneblocks.customblock.CraftEngineCustomBlock; import world.bentobox.aoneblock.oneblocks.customblock.ItemsAdderCustomBlock; import world.bentobox.aoneblock.oneblocks.customblock.NexoCustomBlock; import world.bentobox.aoneblock.requests.IslandStatsHandler; @@ -57,6 +59,8 @@ public class AOneBlock extends GameModeAddon { private boolean hasItemsAdder = false; /** Whether Nexo is present on the server */ private boolean hasNexo = false; + /** Whether CraftEngine is present on the server */ + private boolean hasCraftEngine = false; /** The addon settings */ private Settings settings; @@ -127,6 +131,13 @@ public void onLoad() { OneBlockCustomBlockCreator.register("nexo", NexoCustomBlock::fromMap); hasNexo = true; } + // Check if CraftEngine exists, if yes register listener + if (Bukkit.getPluginManager().getPlugin("CraftEngine") != null) { + registerListener(new CraftEngineListener(this)); + OneBlockCustomBlockCreator.register(CraftEngineCustomBlock::fromId); + OneBlockCustomBlockCreator.register("craftengine", CraftEngineCustomBlock::fromMap); + hasCraftEngine = true; + } // Save the default config from config.yml saveDefaultConfig(); // Load settings from config.yml. This will check if there are any issues with @@ -416,6 +427,13 @@ public boolean hasNexo() { return hasNexo; } + /** + * @return true if CraftEngine is on the server + */ + public boolean hasCraftEngine() { + return hasCraftEngine; + } + /** * Set the addon's world. Used only for testing. * @param world world diff --git a/src/main/java/world/bentobox/aoneblock/listeners/CraftEngineListener.java b/src/main/java/world/bentobox/aoneblock/listeners/CraftEngineListener.java new file mode 100644 index 0000000..4b57bbb --- /dev/null +++ b/src/main/java/world/bentobox/aoneblock/listeners/CraftEngineListener.java @@ -0,0 +1,28 @@ +package world.bentobox.aoneblock.listeners; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +import net.momirealms.craftengine.bukkit.api.event.CraftEngineReloadEvent; +import world.bentobox.aoneblock.AOneBlock; + +/** + * Handles CraftEngineReloadEvent which is fired when CraftEngine loads or reloads its data + */ +public class CraftEngineListener implements Listener { + + private final AOneBlock addon; + + public CraftEngineListener(AOneBlock addon) { + this.addon = addon; + } + + /** + * Handle CraftEngineReloadEvent and reload the addon when triggered + * @param e - CraftEngineReloadEvent + */ + @EventHandler + public void onReload(CraftEngineReloadEvent e) { + addon.loadData(); + } +} diff --git a/src/main/java/world/bentobox/aoneblock/oneblocks/customblock/CraftEngineCustomBlock.java b/src/main/java/world/bentobox/aoneblock/oneblocks/customblock/CraftEngineCustomBlock.java new file mode 100644 index 0000000..7656785 --- /dev/null +++ b/src/main/java/world/bentobox/aoneblock/oneblocks/customblock/CraftEngineCustomBlock.java @@ -0,0 +1,48 @@ +package world.bentobox.aoneblock.oneblocks.customblock; + +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +import org.bukkit.Material; +import org.bukkit.block.Block; + +import net.momirealms.craftengine.bukkit.api.CraftEngineBlocks; +import net.momirealms.craftengine.core.block.CustomBlock; +import net.momirealms.craftengine.core.util.Key; +import world.bentobox.aoneblock.AOneBlock; +import world.bentobox.aoneblock.oneblocks.OneBlockCustomBlock; +import world.bentobox.bentobox.BentoBox; + +public class CraftEngineCustomBlock implements OneBlockCustomBlock { + private final String blockId; + + public CraftEngineCustomBlock(String blockId) { + this.blockId = blockId; + } + + public static Optional fromId(String id) { + CustomBlock block = CraftEngineBlocks.byId(Key.of(id)); + if (block != null) { + return Optional.of(new CraftEngineCustomBlock(id)); + } + return Optional.empty(); + } + + public static Optional fromMap(Map map) { + return Optional + .ofNullable(Objects.toString(map.get("id"), null)) + .flatMap(CraftEngineCustomBlock::fromId); + } + + @Override + public void execute(AOneBlock addon, Block block) { + try { + block.setType(Material.AIR); + CraftEngineBlocks.place(block.getLocation(), Key.of(blockId), false); + } catch (Exception e) { + BentoBox.getInstance().logError("Could not place CraftEngine block " + blockId + ": " + e.getMessage()); + block.setType(Material.STONE); + } + } +} diff --git a/src/test/java/world/bentobox/aoneblock/oneblocks/customblock/CraftEngineCustomBlockTest.java b/src/test/java/world/bentobox/aoneblock/oneblocks/customblock/CraftEngineCustomBlockTest.java new file mode 100644 index 0000000..cd35d31 --- /dev/null +++ b/src/test/java/world/bentobox/aoneblock/oneblocks/customblock/CraftEngineCustomBlockTest.java @@ -0,0 +1,30 @@ +package world.bentobox.aoneblock.oneblocks.customblock; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.junit.jupiter.api.Test; + +/** + * Unit tests for {@link CraftEngineCustomBlock#fromMap(Map)}. + *

+ * Because CraftEngine is an optional runtime dependency, the {@code fromId} + * path cannot be exercised without a live server. These tests cover the + * map-parsing entry point and the factory registration in + * {@link world.bentobox.aoneblock.oneblocks.OneBlockCustomBlockCreator}. + */ +class CraftEngineCustomBlockTest { + + @Test + void fromMapReturnsEmptyWhenIdMissing() { + Map map = new LinkedHashMap<>(); + map.put("type", "craftengine"); + // no "id" key + + var result = CraftEngineCustomBlock.fromMap(map); + + assertTrue(result.isEmpty(), "Should return empty when 'id' is missing"); + } +}