From adc394e660aca46879ec5fdf9a29d010b2d2809e Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 17 Sep 2023 16:03:51 +0200 Subject: [PATCH] Add LoaderTNT editing Add more filter options Add remove loader element on break block Signed-off-by: yoyosource --- BauSystem_Main/src/BauSystem.properties | 10 +- BauSystem_Main/src/BauSystem_de.properties | 9 +- .../bausystem/features/loader/Loader.java | 238 ++++++++++++------ .../features/loader/LoaderCommand.java | 2 +- .../features/loader/LoaderRecorder.java | 27 +- .../elements/LoaderInteractionElement.java | 2 + .../loader/elements/impl/LoaderTNT.java | 50 +++- 7 files changed, 251 insertions(+), 87 deletions(-) diff --git a/BauSystem_Main/src/BauSystem.properties b/BauSystem_Main/src/BauSystem.properties index 9d941a9a..4299211b 100644 --- a/BauSystem_Main/src/BauSystem.properties +++ b/BauSystem_Main/src/BauSystem.properties @@ -544,6 +544,7 @@ LOADER_PAUSE = §7Pause LOADER_END = §8Finished LOADER_MESSAGE_INTERACT=§e{0} added {1} +LOADER_MESSAGE_UNINTERACT=§eRemoved Element LOADER_BUTTON_TNT=TNT LOADER_BUTTON_SWITCH=Lever LOADER_BUTTON_WOOD_BUTTON=Wooden Button @@ -576,9 +577,11 @@ LOADER_PERMS=§cYou are not allowed to use the Loader here LOADER_NOTHING_RECORDED=§cYou have not recorded anything yet! LOADER_GUI_TITLE=Loader GUI -LOADER_GUI_SHOW_INTERACTIONS=§eShow only Interactions -LOADER_GUI_SHOW_WAITS=§eShow only Waits -LOADER_GUI_SHOW_WAITS_SET_BETWEEN_TNT=§7Wait Time between TNT +LOADER_GUI_SHOW_ALL=Show all +LOADER_GUI_SHOW_INTERACTIONS=Show only Interactions +LOADER_GUI_SHOW_WAITS=Show only Waits +LOADER_GUI_SHOW_WAITS_BETWEEN_TNT=Show only Waits between TNT +LOADER_GUI_SHOW_TNT=Show TNT LOADER_GUI_SHOW_WAITS_SET_ALL=§7Wait Time all LOADER_GUI_SHOW_WAITS_TITLE=§7Wait Time LOADER_GUI_SETTINGS_TITLE=Settings @@ -608,6 +611,7 @@ LOADER_SETTING_TNT_X=§7X§8: §e{0} LOADER_SETTING_TNT_Y=§7Y§8: §e{0} LOADER_SETTING_TNT_Z=§7Z§8: §e{0} LOADER_INTERACTION_NOOP=NOOP +LOADER_INTERACTION_PLACE=Place LOADER_INTERACTION_INTERACT=Interact LOADER_INTERACTION_POWERED=Powered LOADER_INTERACTION_UNPOWERED=Unpowered diff --git a/BauSystem_Main/src/BauSystem_de.properties b/BauSystem_Main/src/BauSystem_de.properties index d43aca3b..8dbab2b9 100644 --- a/BauSystem_Main/src/BauSystem_de.properties +++ b/BauSystem_Main/src/BauSystem_de.properties @@ -544,9 +544,11 @@ LOADER_PERMS=§cDu darfst hier nicht den Detonator nutzen LOADER_NOTHING_RECORDED=§cEs wurden keine Elemente aufgenommen! LOADER_GUI_TITLE=Loader Einstellungen -LOADER_GUI_SHOW_INTERACTIONS=§eZeige Interaktionen -LOADER_GUI_SHOW_WAITS=§eZeige Wartezeiten -LOADER_GUI_SHOW_WAITS_SET_BETWEEN_TNT=§7Wait Time zwischen TNT +LOADER_GUI_SHOW_ALL=Zeige alles +LOADER_GUI_SHOW_INTERACTIONS=Zeige Interaktionen +LOADER_GUI_SHOW_WAITS=Zeige Wartezeiten +LOADER_GUI_SHOW_WAITS_BETWEEN_TNT=Zeige Wartezeiten zwischen TNT +LOADER_GUI_SHOW_TNT=Zeige TNT LOADER_GUI_SHOW_WAITS_SET_ALL=§7Wait Time alle LOADER_GUI_SHOW_WAITS_TITLE=§7Wartezeit LOADER_GUI_SETTINGS_TITLE=Einstellungen @@ -576,6 +578,7 @@ LOADER_SETTING_TNT_X=§7X§8: §e{0} LOADER_SETTING_TNT_Y=§7Y§8: §e{0} LOADER_SETTING_TNT_Z=§7Z§8: §e{0} LOADER_INTERACTION_NOOP=NOOP +LOADER_INTERACTION_PLACE=Platzieren LOADER_INTERACTION_INTERACT=Interagiere LOADER_INTERACTION_POWERED=Aktiviert LOADER_INTERACTION_UNPOWERED=Deaktiviert diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/loader/Loader.java b/BauSystem_Main/src/de/steamwar/bausystem/features/loader/Loader.java index e2bbb9fd..b50d3c08 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/loader/Loader.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/loader/Loader.java @@ -35,9 +35,11 @@ import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.inventory.ClickType; import org.bukkit.event.player.PlayerQuitEvent; import java.util.*; +import java.util.concurrent.atomic.AtomicBoolean; public class Loader implements Listener { @@ -124,25 +126,46 @@ public class Loader implements Listener { public void gui(SettingsSorting settingsSorting) { List> list = new ArrayList<>(); + AtomicBoolean allWait = new AtomicBoolean(true); Runnable updateRunnable = () -> { list.clear(); - for (LoaderElement element : elements) { - if (settingsSorting != null) { - if (settingsSorting == SettingsSorting.WAIT && !(element instanceof LoaderWait)) { - continue; - } - if (settingsSorting == SettingsSorting.INTERACTIONS && (element instanceof LoaderWait || element instanceof LoaderTNT)) { - continue; - } + for (int i = 0; i < elements.size(); i++) { + LoaderElement previous = i > 0 ? elements.get(i - 1) : null; + LoaderElement current = elements.get(i); + LoaderElement next = i < elements.size() - 1 ? elements.get(i + 1) : null; + + if (!settingsSorting.shouldShow(previous, current, next)) { + continue; } - SWItem item = element.menu(p); - if (element instanceof LoaderInteractionElement) { - LoaderInteractionElement interactionElement = (LoaderInteractionElement) element; - item.setLore(Arrays.asList(BauSystem.MESSAGE.parse("LOADER_SETTING_MODES", p, interactionElement.size()), "§8", BauSystem.MESSAGE.parse("LOADER_GUI_CLICK_TO_EDIT", p))); + + if ((!(current instanceof LoaderWait))) { + allWait.set(false); + } + + SWItem item = current.menu(p); + if (current instanceof LoaderInteractionElement) { + LoaderInteractionElement interactionElement = (LoaderInteractionElement) current; + List lore = new ArrayList<>(); + if (item.getItemMeta() != null && item.getItemMeta().getLore() != null) { + lore.addAll(item.getItemMeta().getLore()); + lore.add("§8"); + } + lore.add(BauSystem.MESSAGE.parse("LOADER_SETTING_MODES", p, interactionElement.size())); + lore.add(BauSystem.MESSAGE.parse("LOADER_GUI_CLICK_TO_EDIT", p)); + item.setLore(lore); } else { - item.setLore(Arrays.asList(BauSystem.MESSAGE.parse("LOADER_GUI_CLICK_TO_EDIT", p))); + List lore = new ArrayList<>(); + if (item.getItemMeta() != null && item.getItemMeta().getLore() != null) { + lore.addAll(item.getItemMeta().getLore()); + lore.add("§8"); + } + lore.add(BauSystem.MESSAGE.parse("LOADER_GUI_CLICK_TO_EDIT", p)); + item.setLore(lore); } - list.add(new SWListInv.SWListEntry<>(item, element)); + list.add(new SWListInv.SWListEntry<>(item, current)); + } + if (list.isEmpty()) { + allWait.set(false); } }; updateRunnable.run(); @@ -153,65 +176,51 @@ public class Loader implements Listener { swListInv.open(); })); - SWItem onlyInteractionsElements = new SWItem(Material.REPEATER, BauSystem.MESSAGE.parse("LOADER_GUI_SHOW_INTERACTIONS", p), clickType -> { - gui(settingsSorting == SettingsSorting.INTERACTIONS ? null : SettingsSorting.INTERACTIONS); + SWItem settingItem = new SWItem(settingsSorting.getMaterial(), "§e" + BauSystem.MESSAGE.parse(settingsSorting.getName(), p), clickType -> { + if (clickType == ClickType.LEFT) { + int index = settingsSorting.ordinal() + 1; + if (index >= SettingsSorting.LENGTH) { + index = 0; + } + gui(SettingsSorting.values()[index]); + } else if (clickType == ClickType.RIGHT) { + int index = settingsSorting.ordinal() - 1; + if (index < 0) { + index = SettingsSorting.LENGTH - 1; + } + gui(SettingsSorting.values()[index]); + } }); - if (settingsSorting == SettingsSorting.INTERACTIONS) onlyInteractionsElements.setEnchanted(true); - swListInv.setItem(47, onlyInteractionsElements); - - SWItem onlyWaitElements = new SWItem(Material.CLOCK, BauSystem.MESSAGE.parse("LOADER_GUI_SHOW_WAITS", p), clickType -> { - gui(settingsSorting == SettingsSorting.WAIT ? null : SettingsSorting.WAIT); - }); - if (settingsSorting == SettingsSorting.WAIT) onlyWaitElements.setEnchanted(true); - swListInv.setItem(48, onlyWaitElements); - - if (settingsSorting == SettingsSorting.WAIT) { - SWItem waitBetweenTNT = new SWItem(Material.TNT, BauSystem.MESSAGE.parse("LOADER_GUI_SHOW_WAITS_SET_BETWEEN_TNT", p), clickType -> { - SWAnvilInv swAnvilInv = new SWAnvilInv(p, BauSystem.MESSAGE.parse("LOADER_GUI_SHOW_WAITS_TITLE", p), ""); - swAnvilInv.setCallback(s -> { - try { - long delay = Long.parseLong(s); - if (delay < 0) delay = 0; - for (int i = 1; i < elements.size() - 1; i++) { - if (!(elements.get(i - 1) instanceof LoaderTNT)) continue; - if (!(elements.get(i + 1) instanceof LoaderTNT)) continue; - if (!(elements.get(i) instanceof LoaderWait)) continue; - ((LoaderWait) elements.get(i)).setDelay(delay); - } - } catch (NumberFormatException ignored) { - } - gui(settingsSorting); - }); - updateRunnable.run(); - swAnvilInv.open(); - }); - swListInv.setItem(50, waitBetweenTNT); - - SWItem waitTime = new SWItem(Material.PAPER, BauSystem.MESSAGE.parse("LOADER_GUI_SHOW_WAITS_SET_ALL", p), clickType -> { - SWAnvilInv swAnvilInv = new SWAnvilInv(p, BauSystem.MESSAGE.parse("LOADER_GUI_SHOW_WAITS_TITLE", p), ""); - swAnvilInv.setCallback(s -> { - try { - long delay = Long.parseLong(s); - if (delay < 0) delay = 0; - long finalDelay = delay; - elements.stream() - .filter(LoaderWait.class::isInstance) - .map(LoaderWait.class::cast) - .forEach(loaderWait -> loaderWait.setDelay(finalDelay)); - } catch (NumberFormatException ignored) { - } - gui(settingsSorting); - }); - updateRunnable.run(); - swAnvilInv.open(); - }); - swListInv.setItem(51, waitTime); - } else { - SWItem empty = new SWItem(Material.STRUCTURE_VOID, "§7", clickType -> {}); - swListInv.setItem(50, empty); - swListInv.setItem(51, empty); + List strings = new ArrayList<>(); + for (SettingsSorting setting : SettingsSorting.values()) { + if (setting == settingsSorting) { + strings.add("§e> §7" + BauSystem.MESSAGE.parse(setting.getName(), p)); + } else { + strings.add("§8> §7" + BauSystem.MESSAGE.parse(setting.getName(), p)); + } } + settingItem.setLore(strings); + swListInv.setItem(48, settingItem); + if (allWait.get()) { + SWItem setWait = new SWItem(Material.PAPER, BauSystem.MESSAGE.parse("LOADER_GUI_SHOW_WAITS_SET_ALL", p), clickType -> { + SWAnvilInv swAnvilInv = new SWAnvilInv(p, BauSystem.MESSAGE.parse("LOADER_GUI_SHOW_WAITS_TITLE", p), ""); + swAnvilInv.setCallback(s -> { + try { + long delay = Math.max(Long.parseLong(s), 0); + list.forEach(loaderElementSWListEntry -> { + ((LoaderWait) loaderElementSWListEntry.getObject()).setDelay(delay); + }); + } catch (NumberFormatException ignored) { + } + gui(settingsSorting); + }); + swAnvilInv.open(); + }); + swListInv.setItem(50, setWait); + } else { + swListInv.setItem(50, new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§8")); + } swListInv.open(); } @@ -226,8 +235,93 @@ public class Loader implements Listener { } public enum SettingsSorting { - WAIT, - INTERACTIONS, + ALL { + @Override + public Material getMaterial() { + return Material.STRUCTURE_VOID; + } + + @Override + public String getName() { + return "LOADER_GUI_SHOW_ALL"; + } + + @Override + public boolean shouldShow(LoaderElement previous, LoaderElement current, LoaderElement next) { + return true; + } + }, + WAIT { + @Override + public Material getMaterial() { + return Material.CLOCK; + } + + @Override + public String getName() { + return "LOADER_GUI_SHOW_WAITS"; + } + + @Override + public boolean shouldShow(LoaderElement previous, LoaderElement current, LoaderElement next) { + return current instanceof LoaderWait; + } + }, + WAIT_BETWEEN_TNT { + @Override + public Material getMaterial() { + return Material.REDSTONE_BLOCK; + } + + @Override + public String getName() { + return "LOADER_GUI_SHOW_WAITS_BETWEEN_TNT"; + } + + @Override + public boolean shouldShow(LoaderElement previous, LoaderElement current, LoaderElement next) { + return previous instanceof LoaderTNT && current instanceof LoaderWait && next instanceof LoaderTNT; + } + }, + INTERACTIONS { + @Override + public Material getMaterial() { + return Material.REPEATER; + } + + @Override + public String getName() { + return "LOADER_GUI_SHOW_INTERACTIONS"; + } + + @Override + public boolean shouldShow(LoaderElement previous, LoaderElement current, LoaderElement next) { + return current instanceof LoaderInteractionElement && !(current instanceof LoaderTNT); + } + }, + TNT { + @Override + public Material getMaterial() { + return Material.TNT; + } + + @Override + public String getName() { + return "LOADER_GUI_SHOW_TNT"; + } + + @Override + public boolean shouldShow(LoaderElement previous, LoaderElement current, LoaderElement next) { + return current instanceof LoaderTNT; + } + }, + ; + + public static int LENGTH = SettingsSorting.values().length; + + public abstract Material getMaterial(); + public abstract String getName(); + public abstract boolean shouldShow(LoaderElement previous, LoaderElement current, LoaderElement next); } @AllArgsConstructor diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/loader/LoaderCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/loader/LoaderCommand.java index ee03be16..7ced38b8 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/loader/LoaderCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/loader/LoaderCommand.java @@ -80,7 +80,7 @@ public class LoaderCommand extends SWCommand { public void guiLoader(@Validator Player player) { Loader loader = Loader.getLoader(player); if (loaderNullCheck(loader, player)) return; - loader.gui(null); + loader.gui(Loader.SettingsSorting.ALL); } @ClassValidator(value = Player.class, local = true) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/loader/LoaderRecorder.java b/BauSystem_Main/src/de/steamwar/bausystem/features/loader/LoaderRecorder.java index a0734231..e179d6e4 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/loader/LoaderRecorder.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/loader/LoaderRecorder.java @@ -22,6 +22,7 @@ package de.steamwar.bausystem.features.loader; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.SWUtils; import de.steamwar.bausystem.features.loader.elements.LoaderElement; +import de.steamwar.bausystem.features.loader.elements.LoaderInteractionElement; import de.steamwar.bausystem.features.loader.elements.impl.*; import de.steamwar.bausystem.features.tpslimit.TPSUtils; import org.bukkit.Bukkit; @@ -33,6 +34,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; +import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerMoveEvent; @@ -86,7 +88,30 @@ public class LoaderRecorder implements Listener { } @EventHandler - public void onPlayerInteractEntity(PlayerInteractEvent event) { + public void onBlockBreak(BlockBreakEvent event) { + if (event.getPlayer() != player) return; + if (event.getBlock().getType() != Material.TNT) { + event.setCancelled(true); + } + + for (int i = 0; i < loaderElementList.size(); i++) { + LoaderElement element = loaderElementList.get(i); + if (!(element instanceof LoaderInteractionElement)) continue; + LoaderInteractionElement interactionElement = (LoaderInteractionElement) element; + if (interactionElement.getLocation().equals(event.getBlock().getLocation())) { + loaderElementList.remove(i); + if (i > 0) { + loaderElementList.remove(i - 1); + } + break; + } + } + + SWUtils.sendToActionbar(player, BauSystem.MESSAGE.parse("LOADER_MESSAGE_UNINTERACT", player)); + } + + @EventHandler + public void onPlayerInteract(PlayerInteractEvent event) { if (event.getPlayer() != player) return; if (player.isSneaking()) return; if (event.getAction() != Action.RIGHT_CLICK_BLOCK && event.getAction() != Action.PHYSICAL) return; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/loader/elements/LoaderInteractionElement.java b/BauSystem_Main/src/de/steamwar/bausystem/features/loader/elements/LoaderInteractionElement.java index 43f56a4f..67539fd8 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/loader/elements/LoaderInteractionElement.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/loader/elements/LoaderInteractionElement.java @@ -24,6 +24,7 @@ import de.steamwar.inventory.SWAnvilInv; import de.steamwar.inventory.SWInventory; import de.steamwar.inventory.SWItem; import de.steamwar.inventory.SWListInv; +import lombok.Getter; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -40,6 +41,7 @@ import java.util.function.Consumer; public abstract class LoaderInteractionElement & LoaderSettingsEnum> implements LoaderElement { + @Getter protected final Location location; protected int currentShot = 0; protected T defaultSetting; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/loader/elements/impl/LoaderTNT.java b/BauSystem_Main/src/de/steamwar/bausystem/features/loader/elements/impl/LoaderTNT.java index 319b8bb3..f0ee034e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/loader/elements/impl/LoaderTNT.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/loader/elements/impl/LoaderTNT.java @@ -20,22 +20,57 @@ package de.steamwar.bausystem.features.loader.elements.impl; import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.features.loader.elements.LoaderElement; +import de.steamwar.bausystem.features.loader.elements.LoaderInteractionElement; +import de.steamwar.bausystem.features.loader.elements.LoaderSettingsEnum; import de.steamwar.inventory.SWItem; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.data.BlockData; import org.bukkit.entity.Player; import java.util.Arrays; import java.util.function.Consumer; -public class LoaderTNT implements LoaderElement { - - private Location location; +public class LoaderTNT extends LoaderInteractionElement { public LoaderTNT(Location location) { - this.location = location; + super(location, TNTSettingsEnum.PLACE, TNTSettingsEnum.NOOP, TNTSettingsEnum.values()); + } + + public enum TNTSettingsEnum implements LoaderSettingsEnum { + NOOP { + @Override + public int getPos() { + return 2; + } + + @Override + public SWItem menu(Player player, LoaderTNT parent, int power, long ticks) { + return new SWItem(Material.STRUCTURE_VOID, translateItemName("LOADER_BUTTON_TNT", "LOADER_INTERACTION_NOOP", player)); + } + + @Override + public void execute(Location location, BlockData blockData, LoaderTNT parent, int power, long ticks, Consumer delay) { + } + }, + + PLACE { + @Override + public int getPos() { + return 3; + } + + @Override + public SWItem menu(Player player, LoaderTNT parent, int power, long ticks) { + return new SWItem(Material.TNT, translateItemName("LOADER_BUTTON_TNT", "LOADER_INTERACTION_PLACE", player)); + } + + @Override + public void execute(Location location, BlockData blockData, LoaderTNT parent, int power, long ticks, Consumer delay) { + location.getBlock().setType(Material.TNT); + } + } } @Override @@ -53,10 +88,11 @@ public class LoaderTNT implements LoaderElement { return; } - block.setType(Material.TNT); + super.execute(delay); } @Override - public void click(Player player, Runnable backAction) { + public boolean checkBlockInWorld() { + return true; } }