From 382ebb67679106ba3e63715742240f32798800e0 Mon Sep 17 00:00:00 2001 From: Esoteric Enderman Date: Wed, 30 Oct 2024 00:02:23 +0000 Subject: [PATCH] Use custom block feature --- .../plugins/template/PaperTemplatePlugin.java | 15 +-- .../PlaceCustomMultiblockCommand.java | 48 ------- .../multiblocks/AbstractCustomMultiblock.java | 55 -------- .../custom/multiblocks/CustomMultiblock.java | 9 -- .../multiblocks/CustomMultiblockManager.java | 125 ------------------ .../multiblocks/StoredCustomMultiblock.java | 29 ---- .../multiblocks/StoredCustomMultiblocks.java | 16 --- .../plugins/template/language/Message.java | 4 +- 8 files changed, 5 insertions(+), 296 deletions(-) delete mode 100644 src/main/java/foundation/esoteric/minecraft/plugins/template/commands/PlaceCustomMultiblockCommand.java delete mode 100644 src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/AbstractCustomMultiblock.java delete mode 100644 src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/CustomMultiblock.java delete mode 100644 src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/CustomMultiblockManager.java delete mode 100644 src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/StoredCustomMultiblock.java delete mode 100644 src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/StoredCustomMultiblocks.java diff --git a/src/main/java/foundation/esoteric/minecraft/plugins/template/PaperTemplatePlugin.java b/src/main/java/foundation/esoteric/minecraft/plugins/template/PaperTemplatePlugin.java index 2e6930ed..c43488bf 100644 --- a/src/main/java/foundation/esoteric/minecraft/plugins/template/PaperTemplatePlugin.java +++ b/src/main/java/foundation/esoteric/minecraft/plugins/template/PaperTemplatePlugin.java @@ -2,6 +2,7 @@ import dev.jorel.commandapi.CommandAPI; import dev.jorel.commandapi.CommandAPIBukkitConfig; +import foundation.esoteric.minecraft.plugins.library.block.CustomBlockManager; import foundation.esoteric.minecraft.plugins.library.commands.GiveCustomItemCommand; import foundation.esoteric.minecraft.plugins.library.entity.CustomEntityManager; import foundation.esoteric.minecraft.plugins.library.file.FileManagedPlugin; @@ -12,10 +13,7 @@ import foundation.esoteric.minecraft.plugins.library.resourcepack.ResourcePackManager; import foundation.esoteric.minecraft.plugins.library.resourcepack.ResourcePackPlugin; import org.bukkit.plugin.java.JavaPlugin; -import foundation.esoteric.minecraft.plugins.template.commands.PlaceCustomMultiblockCommand; import foundation.esoteric.minecraft.plugins.template.commands.SetLanguageCommand; -import foundation.esoteric.minecraft.plugins.template.custom.multiblocks.CustomMultiblock; -import foundation.esoteric.minecraft.plugins.template.custom.multiblocks.CustomMultiblockManager; import foundation.esoteric.minecraft.plugins.template.data.player.PlayerDataManager; import foundation.esoteric.minecraft.plugins.template.language.LanguageManager; import foundation.esoteric.minecraft.plugins.template.language.Message; @@ -27,7 +25,7 @@ public final class PaperTemplatePlugin extends JavaPlugin implements FileManaged private LanguageManager languageManager; private ResourcePackManager resourcePackManager; private CustomItemManager customItemManager; - private CustomMultiblockManager customMultiblockManager; + private CustomBlockManager customMultiblockManager; public FileManager getFileManager() { return fileManager; @@ -49,7 +47,7 @@ public CustomItemManager getCustomItemManager() { return customItemManager; } - public CustomMultiblockManager getCustomMultiblockManager() { + public CustomBlockManager getCustomMultiblockManager() { return customMultiblockManager; } @@ -73,17 +71,12 @@ public void onEnable() { new HttpServerManager(this); customItemManager = new CustomItemManager(this); new CustomEntityManager(this); - if (CustomMultiblock.isEnabled()) { - customMultiblockManager = new CustomMultiblockManager(this); - } + customMultiblockManager = new CustomBlockManager(this); if (Message.isEnabled()) { new SetLanguageCommand(this); } new GiveCustomItemCommand(this); - if (CustomMultiblock.isEnabled()) { - new PlaceCustomMultiblockCommand(this); - } } @Override diff --git a/src/main/java/foundation/esoteric/minecraft/plugins/template/commands/PlaceCustomMultiblockCommand.java b/src/main/java/foundation/esoteric/minecraft/plugins/template/commands/PlaceCustomMultiblockCommand.java deleted file mode 100644 index c88e9326..00000000 --- a/src/main/java/foundation/esoteric/minecraft/plugins/template/commands/PlaceCustomMultiblockCommand.java +++ /dev/null @@ -1,48 +0,0 @@ -package foundation.esoteric.minecraft.plugins.template.commands; - -import dev.jorel.commandapi.CommandAPICommand; -import dev.jorel.commandapi.CommandPermission; -import dev.jorel.commandapi.arguments.Argument; -import dev.jorel.commandapi.arguments.ArgumentSuggestions; -import dev.jorel.commandapi.arguments.CustomArgument; -import dev.jorel.commandapi.arguments.CustomArgument.CustomArgumentException; -import dev.jorel.commandapi.arguments.StringArgument; -import foundation.esoteric.minecraft.plugins.template.PaperTemplatePlugin; -import foundation.esoteric.minecraft.plugins.template.custom.multiblocks.CustomMultiblock; -import foundation.esoteric.minecraft.plugins.template.language.Message; - -import java.util.stream.Stream; - -public class PlaceCustomMultiblockCommand extends CommandAPICommand { - - public PlaceCustomMultiblockCommand(PaperTemplatePlugin plugin) { - super("place-custom-multiblock"); - - String customMultiblockArgumentNodeName = "custom-multiblock-id"; - - String[] customMultiblockIds = Stream.of(CustomMultiblock.values()).map(Enum::name).toArray(String[]::new); - - Argument customMultiblockArgument = new CustomArgument<>(new StringArgument(customMultiblockArgumentNodeName), (info) -> { - String input = info.currentInput(); - - try { - return CustomMultiblock.valueOf(input); - } catch (IllegalArgumentException exception) { - assert plugin.getLanguageManager() != null; - throw CustomArgumentException.fromAdventureComponent(plugin.getLanguageManager().getMessage(Message.UNKNOWN_CUSTOM_MULTIBLOCK, info.sender(), input)); - } - }).includeSuggestions(ArgumentSuggestions.strings(customMultiblockIds)); - - executesPlayer((info) -> { - CustomMultiblock multiblock = (CustomMultiblock) info.args().get(customMultiblockArgumentNodeName); - - assert plugin.getCustomMultiblockManager() != null; - plugin.getCustomMultiblockManager().placeCustomMultiblock(multiblock, info.sender().getLocation()); - }); - - withPermission(CommandPermission.OP); - withArguments(customMultiblockArgument); - - register(plugin); - } -} diff --git a/src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/AbstractCustomMultiblock.java b/src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/AbstractCustomMultiblock.java deleted file mode 100644 index a51a2b31..00000000 --- a/src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/AbstractCustomMultiblock.java +++ /dev/null @@ -1,55 +0,0 @@ -package foundation.esoteric.minecraft.plugins.template.custom.multiblocks; - -import foundation.esoteric.minecraft.plugins.library.utility.types.BlockLocation; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.block.Block; -import org.bukkit.event.Listener; -import foundation.esoteric.minecraft.plugins.template.PaperTemplatePlugin; -import org.jetbrains.annotations.NotNull; - -import java.util.ArrayList; -import java.util.List; - -public abstract class AbstractCustomMultiblock implements Listener { - - protected final PaperTemplatePlugin plugin; - - private final List> multiblocks = new ArrayList<>(); - - public List> getMultiblocks() { - return multiblocks; - } - - public void addMultiblocks(List> addedMultiblocks) { - multiblocks.addAll(addedMultiblocks); - } - - public AbstractCustomMultiblock(@NotNull CustomMultiblockManager customMultiblockManager, CustomMultiblock multiblockId) { - this.plugin = customMultiblockManager.getPlugin(); - - Bukkit.getPluginManager().registerEvents(this, plugin); - - customMultiblockManager.addCustomMultiblock(multiblockId, this); - } - - protected abstract List generateCustomMultiblock(Location placeLocation); - - public List getCustomMultiblock(Location placeLocation) { - List multiblock = generateCustomMultiblock(placeLocation); - - multiblocks.add(multiblock.stream().map(BlockLocation::new).toList()); - - return multiblock; - } - - public boolean isBlock(Block block) { - for (List multiblock : multiblocks) { - if (multiblock.contains(new BlockLocation(block))) { - return true; - } - } - - return false; - } -} diff --git a/src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/CustomMultiblock.java b/src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/CustomMultiblock.java deleted file mode 100644 index 171509e8..00000000 --- a/src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/CustomMultiblock.java +++ /dev/null @@ -1,9 +0,0 @@ -package foundation.esoteric.minecraft.plugins.template.custom.multiblocks; - -public enum CustomMultiblock { - ; - - public static boolean isEnabled() { - return values().length != 0; - } -} diff --git a/src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/CustomMultiblockManager.java b/src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/CustomMultiblockManager.java deleted file mode 100644 index ac04fe0b..00000000 --- a/src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/CustomMultiblockManager.java +++ /dev/null @@ -1,125 +0,0 @@ -package foundation.esoteric.minecraft.plugins.template.custom.multiblocks; - -import com.google.gson.Gson; -import org.bukkit.Location; -import foundation.esoteric.minecraft.plugins.template.PaperTemplatePlugin; -import org.jetbrains.annotations.NotNull; - -import java.io.*; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -public class CustomMultiblockManager { - - private final PaperTemplatePlugin plugin; - - private final String multiblocksFileExtension = ".json"; - private final String multiblocksResourceFilePath = "data" + File.separator + "multiblocks" + multiblocksFileExtension; - private final String multiblocksFilePath; - private final File multiblocksFile; - - private final Map customMultiblockMap = new HashMap<>(); - - public PaperTemplatePlugin getPlugin() { - return plugin; - } - - public CustomMultiblockManager(@NotNull PaperTemplatePlugin plugin) { - this.plugin = plugin; - - multiblocksFilePath = plugin.getDataFolder() + File.separator + multiblocksResourceFilePath; - multiblocksFile = new File(multiblocksFilePath); - - load(); - } - - public void addCustomMultiblock(CustomMultiblock multiblockId, AbstractCustomMultiblock abstractCustomMultiblock) { - customMultiblockMap.put(multiblockId, abstractCustomMultiblock); - } - - public AbstractCustomMultiblock getAbstractCustomMultiblock(CustomMultiblock multiblockId) { - return customMultiblockMap.get(multiblockId); - } - - public void placeCustomMultiblock(CustomMultiblock multiblockId, Location location) { - customMultiblockMap.get(multiblockId).getCustomMultiblock(location); - } - - public List getAllMultiblocks() { - List allMultiblocks = new ArrayList<>(); - - for (Entry multiblockInfo : customMultiblockMap.entrySet()) { - allMultiblocks.add(new StoredCustomMultiblock(multiblockInfo)); - } - - return allMultiblocks; - } - - private void load() { - if (!multiblocksFile.exists()) { - return; - } - - StoredCustomMultiblocks multiblocks; - - Gson gson = new Gson(); - try { - Reader reader = new FileReader(multiblocksFile); - - multiblocks = gson.fromJson(reader, StoredCustomMultiblocks.class); - - reader.close(); - } catch (IOException exception) { - exception.printStackTrace(); - return; - } - - loadStoredCustomMultiblocks(multiblocks); - } - - public void save() { - StoredCustomMultiblocks dataToSave = getStoredCustomMultiblocks(); - - if (dataToSave.getStoredCustomMultiblocks().isEmpty()) { - return; - } - - if (!multiblocksFile.exists()) { - plugin.saveResource(multiblocksResourceFilePath, false); - } - - Gson gson = new Gson(); - - try { - Writer writer = new FileWriter(multiblocksFile); - - String json = gson.toJson(dataToSave); - - writer.write(json); - - writer.flush(); - writer.close(); - } catch (IOException exception) { - exception.printStackTrace(); - } - } - - public void loadStoredCustomMultiblocks(@NotNull StoredCustomMultiblocks multiblocks) { - for (StoredCustomMultiblock multiblock : multiblocks.getStoredCustomMultiblocks()) { - customMultiblockMap.get(multiblock.getMultiblockId()).addMultiblocks(multiblock.getBlockLocations()); - } - } - - public StoredCustomMultiblocks getStoredCustomMultiblocks() { - StoredCustomMultiblocks output = new StoredCustomMultiblocks(); - - for (Entry multiblockInfo : customMultiblockMap.entrySet()) { - output.addCustomMultiblock(new StoredCustomMultiblock(multiblockInfo)); - } - - return output; - } -} diff --git a/src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/StoredCustomMultiblock.java b/src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/StoredCustomMultiblock.java deleted file mode 100644 index a7aa21bc..00000000 --- a/src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/StoredCustomMultiblock.java +++ /dev/null @@ -1,29 +0,0 @@ -package foundation.esoteric.minecraft.plugins.template.custom.multiblocks; - -import foundation.esoteric.minecraft.plugins.library.utility.types.BlockLocation; -import org.jetbrains.annotations.NotNull; - -import java.util.List; -import java.util.Map.Entry; - -public class StoredCustomMultiblock { - private final List> blockLocations; - private final CustomMultiblock multiblockId; - - public List> getBlockLocations() { - return blockLocations; - } - - public CustomMultiblock getMultiblockId() { - return multiblockId; - } - - public StoredCustomMultiblock(List> blockLocations, CustomMultiblock multiblockId) { - this.blockLocations = blockLocations; - this.multiblockId = multiblockId; - } - - public StoredCustomMultiblock(@NotNull Entry multiblockInfo) { - this(multiblockInfo.getValue().getMultiblocks(), multiblockInfo.getKey()); - } -} diff --git a/src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/StoredCustomMultiblocks.java b/src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/StoredCustomMultiblocks.java deleted file mode 100644 index 7d50b8a8..00000000 --- a/src/main/java/foundation/esoteric/minecraft/plugins/template/custom/multiblocks/StoredCustomMultiblocks.java +++ /dev/null @@ -1,16 +0,0 @@ -package foundation.esoteric.minecraft.plugins.template.custom.multiblocks; - -import java.util.ArrayList; -import java.util.List; - -public class StoredCustomMultiblocks { - private List storedCustomMultiblocks = new ArrayList<>(); - - public List getStoredCustomMultiblocks() { - return storedCustomMultiblocks; - } - - public void addCustomMultiblock(StoredCustomMultiblock addedCustomMultiblocks) { - storedCustomMultiblocks.add(addedCustomMultiblocks); - } -} diff --git a/src/main/java/foundation/esoteric/minecraft/plugins/template/language/Message.java b/src/main/java/foundation/esoteric/minecraft/plugins/template/language/Message.java index 4fe56ec4..debe8fb1 100644 --- a/src/main/java/foundation/esoteric/minecraft/plugins/template/language/Message.java +++ b/src/main/java/foundation/esoteric/minecraft/plugins/template/language/Message.java @@ -1,7 +1,5 @@ package foundation.esoteric.minecraft.plugins.template.language; -import foundation.esoteric.minecraft.plugins.template.custom.multiblocks.CustomMultiblock; - import java.util.function.BooleanSupplier; public enum Message { @@ -10,7 +8,7 @@ public enum Message { UNKNOWN_CUSTOM_ITEM(), UNKNOWN_CUSTOM_MULTIENTITY(), - UNKNOWN_CUSTOM_MULTIBLOCK(CustomMultiblock::isEnabled); + UNKNOWN_CUSTOM_MULTIBLOCK(); private final BooleanSupplier isMessageInUseSupplier;