From edfc331d034ae2df85b6e3a380c91d45b7dbc1e9 Mon Sep 17 00:00:00 2001 From: zOnlyKroks Date: Thu, 27 Jul 2023 15:43:55 +0200 Subject: [PATCH] Use parameters --- .../features/region/TestblockCommand.java | 123 +++++++++++++++--- 1 file changed, 107 insertions(+), 16 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 e7ddb346..ec6d5562 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/region/TestblockCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/TestblockCommand.java @@ -21,6 +21,9 @@ package de.steamwar.bausystem.features.region; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.Permission; +import de.steamwar.bausystem.features.tracer.TraceCommand; +import de.steamwar.bausystem.features.tracer.show.ShowModeParameter; +import de.steamwar.bausystem.features.tracer.show.ShowModeParameterType; import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.RegionUtils; import de.steamwar.bausystem.region.utils.RegionExtensionType; @@ -29,16 +32,18 @@ import de.steamwar.command.*; import de.steamwar.linkage.Linked; import de.steamwar.sql.SchematicNode; import de.steamwar.sql.SteamwarUser; +import lombok.Getter; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; +import java.util.function.Supplier; import java.util.logging.Level; +import java.util.stream.Collectors; @Linked public class TestblockCommand extends SWCommand { @@ -86,7 +91,7 @@ public class TestblockCommand extends SWCommand { } try { - region.reset(node, RegionType.TESTBLOCK, regionExtensionType,true); + region.reset(node, RegionType.TESTBLOCK, regionExtensionType,false); RegionUtils.message(region, "REGION_TB_DONE"); } catch (IOException e) { BauSystem.MESSAGE.send("REGION_TB_ERROR", p); @@ -94,18 +99,47 @@ public class TestblockCommand extends SWCommand { } } - @ClassMapper(value = RegionExtensionType.class, local = true) - private TypeMapper regionExtensionTypeTypeMapper() { - Map showModeParameterTypesMap = new HashMap<>(); - showModeParameterTypesMap.put("-normal", RegionExtensionType.NORMAL); - showModeParameterTypesMap.put("-n", RegionExtensionType.NORMAL); - showModeParameterTypesMap.put("-extension", RegionExtensionType.EXTENSION); - showModeParameterTypesMap.put("-e", RegionExtensionType.EXTENSION); - showModeParameterTypesMap.put("-t", null); - showModeParameterTypesMap.put("-tnt", null); + @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()); + } - List tabCompletes = new ArrayList<>(showModeParameterTypesMap.keySet()); - return SWCommandUtils.createMapper(s -> showModeParameterTypesMap.getOrDefault(s, null), s -> tabCompletes); + Map testblockParameterTypesMap = new HashMap<>(); + testblockParamaterTypeListMap.forEach((k, v) -> v.forEach(s -> testblockParameterTypesMap.put(s, k))); + + return new TypeMapper() { + @Override + public TestblockParamaterType 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 -> { + 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) + .map(Supplier::get) + .flatMap(Arrays::stream) + .collect(Collectors.toSet()); + + List tabCompletes = new ArrayList<>(); + for (Map.Entry> entry : testblockParamaterTypeListMap.entrySet()) { + if (removed.contains(entry.getKey()) || testblockParameterTypeSet.contains(entry.getKey())) { + continue; + } + tabCompletes.addAll(entry.getValue()); + } + return tabCompletes; + } + }; } @ClassValidator(value = Player.class, local = true) @@ -144,4 +178,61 @@ 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")); + + @Getter + private final Consumer testblockParameterConsumer; + + @Getter + private List tabCompletes; + + @Getter + private final Supplier removed; + private AtomicReference cached = new AtomicReference<>(); + + TestblockParamaterType(Consumer testblockParameterConsumer, List tabCompletes, String... removed) { + this.testblockParameterConsumer = testblockParameterConsumer; + this.tabCompletes = tabCompletes; + this.removed = () -> { + if (cached.get() == null) { + TestblockParamaterType[] showModeParameterTypes = new TestblockParamaterType[removed.length]; + for (int i = 0; i < removed.length; i++) { + showModeParameterTypes[i] = TestblockParamaterType.valueOf(removed[i]); + } + cached.set(showModeParameterTypes); + return showModeParameterTypes; + } + return cached.get(); + }; + } + } + + @Getter + private class TestblockParameter { + private boolean water = false; + private boolean tnt = false; + private boolean extension = false; + private boolean normal = false; + + public void enableWater() { + this.water = true; + } + + public void enableTNT() { + this.tnt = true; + } + + public void enableExtension() { + this.extension = true; + } + + public void enableNormal() { + this.normal = true; + } + } }