From d8b93e976f6d8c0781b9d472c466d84b7ac551a0 Mon Sep 17 00:00:00 2001 From: zOnlyKroks Date: Fri, 28 Jul 2023 21:41:56 +0200 Subject: [PATCH] Implement Testblock Command --- .../features/region/TestblockCommand.java | 114 ++++++++---------- .../region/items/TestblockBauGuiItem.java | 2 +- .../bausystem/region/PasteOptions.java | 2 + .../de/steamwar/bausystem/region/Region.java | 26 +--- 4 files changed, 55 insertions(+), 89 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/TestblockCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/TestblockCommand.java index ec6d5562..67c741ce 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/region/TestblockCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/TestblockCommand.java @@ -52,46 +52,30 @@ public class TestblockCommand extends SWCommand { super("testblock", "tb", "dummy"); } - @Register(description = "REGION_TB_HELP_RESET") - public void genericTestblockCommand(Player p) { - genericTestblockCommand(p, RegionExtensionType.NORMAL); - } - - @Register(description = "REGION_TB_HELP_RESET_EXTENSION") - public void genericTestblockCommand(@Validator Player p, RegionExtensionType regionExtensionType) { - Region region = regionCheck(p); - if (region == null) return; - try { - region.reset(RegionType.TESTBLOCK, regionExtensionType, regionExtensionType == null); - RegionUtils.message(region, "REGION_TB_DONE"); - } catch (IOException e) { - BauSystem.MESSAGE.send("REGION_TB_ERROR", p); - Bukkit.getLogger().log(Level.WARNING, "Failed testblock", e); - } - } - - @Register(description = "REGION_TB_HELP_SCHEMATIC") - public void schematicTestblockCommand(Player p, @Mapper("withPublic") SchematicNode node) { - schematicTestblockCommand(p, node, RegionExtensionType.NORMAL); - } - @Register - public void schematicTestblockCommand(Player p, RegionExtensionType regionExtensionType, @Mapper("withPublic") SchematicNode node) { - schematicTestblockCommand(p, node, regionExtensionType); + public void schematicTestblockCommand(@Validator Player p,@OptionalValue("") @Mapper("withPublic") @AllowNull SchematicNode node,TestblockParameter... testblockParameters) { + boolean isExtension = Arrays.stream(testblockParameters).anyMatch(testblockParameter -> testblockParameter.extension); + boolean isIgnoreAir = Arrays.stream(testblockParameters).anyMatch(testblockParameter -> testblockParameter.ignoreAir); + boolean isOnlyColor = Arrays.stream(testblockParameters).anyMatch(testblockParameter -> testblockParameter.onlyColor); + boolean replaceTNT = Arrays.stream(testblockParameters).anyMatch(testblockParameter -> testblockParameter.tnt); + boolean replaceWater = Arrays.stream(testblockParameters).anyMatch(testblockParameter -> testblockParameter.water); + + resetRegion(p,node,isExtension ? RegionExtensionType.EXTENSION : RegionExtensionType.NORMAL,isIgnoreAir,isOnlyColor,replaceTNT,replaceWater); } - @Register(description = "REGION_TB_HELP_SCHEMATIC_EXTENSION") - public void schematicTestblockCommand(@Validator Player p, @Mapper("withPublic") SchematicNode node, RegionExtensionType regionExtensionType) { + private void resetRegion(Player p,SchematicNode node, RegionExtensionType regionExtensionType,boolean ignoreAir,boolean onlyColors, boolean removeTNT, boolean removeWater) { Region region = regionCheck(p); if (region == null) return; - if(node.isDir()) { - BauSystem.MESSAGE.send("ONLY_SCHEMS", p); - return; - } + if(node != null) { + if(node.isDir()) { + BauSystem.MESSAGE.send("ONLY_SCHEMS", p); + return; + } + } try { - region.reset(node, RegionType.TESTBLOCK, regionExtensionType,false); + region.reset(node,RegionType.TESTBLOCK, regionExtensionType,ignoreAir,onlyColors,removeTNT,removeWater); RegionUtils.message(region, "REGION_TB_DONE"); } catch (IOException e) { BauSystem.MESSAGE.send("REGION_TB_ERROR", p); @@ -99,39 +83,39 @@ public class TestblockCommand extends SWCommand { } } - @ClassMapper(value = TestblockParamaterType.class, local = true) - private TypeMapper testblockParamaterTypeMapper() { - Map> testblockParamaterTypeListMap = new EnumMap<>(TestblockParamaterType.class); - for (TestblockParamaterType value : TestblockParamaterType.values()) { - testblockParamaterTypeListMap.put(value, value.getTabCompletes()); + @ClassMapper(value = TestblockParameterType.class, local = true) + private TypeMapper testblockParameterTypeMapper() { + Map> testblockParameterTypeListMap = new EnumMap<>(TestblockParameterType.class); + for (TestblockParameterType value : TestblockParameterType.values()) { + testblockParameterTypeListMap.put(value, value.getTabCompletes()); } - Map testblockParameterTypesMap = new HashMap<>(); - testblockParamaterTypeListMap.forEach((k, v) -> v.forEach(s -> testblockParameterTypesMap.put(s, k))); + Map testblockParameterTypesMap = new HashMap<>(); + testblockParameterTypeListMap.forEach((k, v) -> v.forEach(s -> testblockParameterTypesMap.put(s, k))); - return new TypeMapper() { + return new TypeMapper() { @Override - public TestblockParamaterType map(CommandSender commandSender, PreviousArguments previousArguments, String s) { + public TestblockParameterType map(CommandSender commandSender, PreviousArguments previousArguments, String s) { return testblockParameterTypesMap.get(s); } @Override public List tabCompletes(CommandSender commandSender, PreviousArguments previousArguments, String s) { - Set testblockParameterTypeSet = new HashSet<>(); - previousArguments.getAll(TestblockParamaterType.class).forEach(showModeType -> { + Set testblockParameterTypeSet = new HashSet<>(); + previousArguments.getAll(TestblockParameterType.class).forEach(showModeType -> { testblockParameterTypeSet.addAll(Arrays.asList(showModeType.removed.get())); }); Arrays.stream(previousArguments.userArgs).map(testblockParameterTypesMap::get).forEach(testblockParameterTypeSet::add); testblockParameterTypeSet.remove(null); - Set removed = testblockParameterTypeSet.stream() - .map(TestblockParamaterType::getRemoved) + Set removed = testblockParameterTypeSet.stream() + .map(TestblockParameterType::getRemoved) .map(Supplier::get) .flatMap(Arrays::stream) .collect(Collectors.toSet()); List tabCompletes = new ArrayList<>(); - for (Map.Entry> entry : testblockParamaterTypeListMap.entrySet()) { + for (Map.Entry> entry : testblockParameterTypeListMap.entrySet()) { if (removed.contains(entry.getKey()) || testblockParameterTypeSet.contains(entry.getKey())) { continue; } @@ -144,9 +128,7 @@ public class TestblockCommand extends SWCommand { @ClassValidator(value = Player.class, local = true) public TypeValidator validator() { - return (commandSender, player, messageSender) -> { - return !messageSender.send(!Permission.hasPermission(player, Permission.WORLD), "REGION_TB_NO_PERMS"); - }; + return (commandSender, player, messageSender) -> !messageSender.send(!Permission.hasPermission(player, Permission.WORLD), "REGION_TB_NO_PERMS"); } private Region regionCheck(Player player) { @@ -179,11 +161,12 @@ public class TestblockCommand extends SWCommand { }; } - private enum TestblockParamaterType { - NORMAL(TestblockParameter::enableWater, Arrays.asList("-n","-normal"), "EXTENSION"), - EXTENSION(TestblockParameter::enableExtension, Arrays.asList("-e", "-extension"), "NORMAL"), - TNT(TestblockParameter::enableTNT, Arrays.asList("-t", "-interpolate-x", "-tnt")), - WATER(TestblockParameter::enableWater, Arrays.asList("-w", "-water")); + private enum TestblockParameterType { + EXTENSION(TestblockParameter::enableExtension, Arrays.asList("-e", "-extension")), + TNT(TestblockParameter::enableTNT, Arrays.asList("-t", "-tnt")), + WATER(TestblockParameter::enableWater, Arrays.asList("-w", "-water")), + IGNORE_AIR(TestblockParameter::enableIgnoreAir, Arrays.asList("-ig", "-ignore_air")), + ONLY_COLOR(TestblockParameter::enableOnlyColor, Arrays.asList("-o","-color","-only_color")); @Getter private final Consumer testblockParameterConsumer; @@ -192,17 +175,17 @@ public class TestblockCommand extends SWCommand { private List tabCompletes; @Getter - private final Supplier removed; - private AtomicReference cached = new AtomicReference<>(); + private final Supplier removed; + private AtomicReference cached = new AtomicReference<>(); - TestblockParamaterType(Consumer testblockParameterConsumer, List tabCompletes, String... removed) { + TestblockParameterType(Consumer testblockParameterConsumer, List tabCompletes, String... removed) { this.testblockParameterConsumer = testblockParameterConsumer; this.tabCompletes = tabCompletes; this.removed = () -> { if (cached.get() == null) { - TestblockParamaterType[] showModeParameterTypes = new TestblockParamaterType[removed.length]; + TestblockParameterType[] showModeParameterTypes = new TestblockParameterType[removed.length]; for (int i = 0; i < removed.length; i++) { - showModeParameterTypes[i] = TestblockParamaterType.valueOf(removed[i]); + showModeParameterTypes[i] = TestblockParameterType.valueOf(removed[i]); } cached.set(showModeParameterTypes); return showModeParameterTypes; @@ -212,12 +195,13 @@ public class TestblockCommand extends SWCommand { } } - @Getter - private class TestblockParameter { + public static class TestblockParameter { private boolean water = false; private boolean tnt = false; private boolean extension = false; - private boolean normal = false; + private boolean onlyColor = false; + + private boolean ignoreAir = false; public void enableWater() { this.water = true; @@ -231,8 +215,8 @@ public class TestblockCommand extends SWCommand { this.extension = true; } - public void enableNormal() { - this.normal = true; - } + public void enableOnlyColor() { this.onlyColor = true; } + + public void enableIgnoreAir() { this.ignoreAir = true; } } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/items/TestblockBauGuiItem.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/items/TestblockBauGuiItem.java index 2b3b863a..07ede055 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/region/items/TestblockBauGuiItem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/items/TestblockBauGuiItem.java @@ -51,7 +51,7 @@ public class TestblockBauGuiItem extends BauGuiItem { public boolean click(ClickType click, Player p) { if (click == ClickType.LEFT) { p.closeInventory(); - testblockCommand.genericTestblockCommand(p); + testblockCommand.schematicTestblockCommand(p,null); } else { SchematicSelector selector = new SchematicSelector(p, SchematicSelector.selectSchematic(), node -> { p.closeInventory(); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/PasteOptions.java b/BauSystem_Main/src/de/steamwar/bausystem/region/PasteOptions.java index ca151b54..ec8186f4 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/PasteOptions.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/PasteOptions.java @@ -47,4 +47,6 @@ public class PasteOptions { private final boolean testBlock; private final boolean removeTNT; + + private final boolean removeWater; } \ No newline at end of file diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java b/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java index 83928d91..040a7f99 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java @@ -413,33 +413,13 @@ public class Region { } } - public void reset(RegionType regionType) throws IOException { - reset(null, regionType); - } - - public void reset(SchematicNode schematic, RegionType regionType) throws IOException { - reset(schematic, regionType, RegionExtensionType.NORMAL, false); - } - - public void reset(RegionType regionType, RegionExtensionType regionExtensionType,boolean removeTNT) throws IOException { - reset(null, regionType, regionExtensionType,removeTNT); - } - - public void reset(SchematicNode schematic, RegionType regionType, RegionExtensionType regionExtensionType,boolean removeTNT) throws IOException { - reset(schematic, regionType, regionExtensionType, false,removeTNT); - } - public void reset(File file) { - EditSession editSession = paste(file, minPoint.add(prototype.getSizeX() / 2, 0, prototype.getSizeZ() / 2), new PasteOptions(false, false, Color.YELLOW, false, false, getMinPoint(RegionType.NORMAL, RegionExtensionType.NORMAL), getMaxPoint(RegionType.NORMAL, RegionExtensionType.NORMAL), waterLevel, false,false)); + EditSession editSession = paste(file, minPoint.add(prototype.getSizeX() / 2, 0, prototype.getSizeZ() / 2), new PasteOptions(false, false, Color.YELLOW, false, false, getMinPoint(RegionType.NORMAL, RegionExtensionType.NORMAL), getMaxPoint(RegionType.NORMAL, RegionExtensionType.NORMAL), waterLevel, false,false,false)); initSessions(); undoSessions.push(editSession); } - public void reset(SchematicNode schematic, RegionType regionType, RegionExtensionType regionExtensionType, boolean ignoreAir, boolean removeTNT) throws IOException { - reset(schematic, regionType, regionExtensionType, ignoreAir, false,removeTNT); - } - - public void reset(SchematicNode schematic, RegionType regionType, RegionExtensionType regionExtensionType, boolean ignoreAir, boolean onlyColors,boolean removeTNT) throws IOException { + public void reset(SchematicNode schematic, RegionType regionType, RegionExtensionType regionExtensionType, boolean ignoreAir, boolean onlyColors,boolean removeTNT, boolean removeWater) throws IOException { if (!hasReset(regionType)) { return; } @@ -447,7 +427,7 @@ public class Region { regionExtensionType = RegionExtensionType.NORMAL; } - PasteOptions pasteOptions = new PasteOptions((schematic != null && (schematic.getSchemtype().fightType() || schematic.getSchemtype().check())), ignoreAir, getPlain(Flag.COLOR, ColorMode.class).getColor(), onlyColors, regionExtensionType == RegionExtensionType.EXTENSION, getMinPoint(regionType, regionExtensionType), getMaxPoint(regionType, regionExtensionType), waterLevel, regionType == RegionType.TESTBLOCK,removeTNT); + PasteOptions pasteOptions = new PasteOptions((schematic != null && (schematic.getSchemtype().fightType() || schematic.getSchemtype().check())), ignoreAir, getPlain(Flag.COLOR, ColorMode.class).getColor(), onlyColors, regionExtensionType == RegionExtensionType.EXTENSION, getMinPoint(regionType, regionExtensionType), getMaxPoint(regionType, regionExtensionType), waterLevel, regionType == RegionType.TESTBLOCK,removeTNT,removeWater); Point pastePoint; File tempFile = null;