From e3d9780df59f8a29d1dc3f057ea289ce1caf593b Mon Sep 17 00:00:00 2001 From: Lixfel Date: Fri, 27 Dec 2019 08:48:14 +0100 Subject: [PATCH 1/2] Refactoring of AutoChecker to reduce chat spam and usability of error messages --- .../schematicsystem/CheckSchemType_10.java | 6 +- .../schematicsystem/CheckSchemType_12.java | 42 ++--- .../schematicsystem/CheckSchemType_14.java | 6 +- .../schematicsystem/CheckSchemType_15.java | 42 ++--- .../schematicsystem/CheckSchemType_8.java | 6 +- .../schematicsystem/CheckSchemType_9.java | 6 +- .../schematicsystem/AutoCheckResult.java | 151 ++++++++++++++++++ .../schematicsystem/ICheckSchemType.java | 6 +- .../schematicsystem/CheckSchemType.java | 52 +++--- .../commands/SchematicCommand.java | 13 +- 10 files changed, 242 insertions(+), 88 deletions(-) create mode 100644 SchematicSystem_API/src/de/steamwar/schematicsystem/AutoCheckResult.java diff --git a/SchematicSystem_10/src/de/steamwar/schematicsystem/CheckSchemType_10.java b/SchematicSystem_10/src/de/steamwar/schematicsystem/CheckSchemType_10.java index 4ba671d..c48847e 100644 --- a/SchematicSystem_10/src/de/steamwar/schematicsystem/CheckSchemType_10.java +++ b/SchematicSystem_10/src/de/steamwar/schematicsystem/CheckSchemType_10.java @@ -1,11 +1,9 @@ package de.steamwar.schematicsystem; -import java.util.List; - class CheckSchemType_10 { private CheckSchemType_10(){} - static void autoCheck(List errors) { - errors.add("In der 1.10 können keine Schematics eingesendet werden"); + static AutoCheckResult autoCheck() { + return new AutoCheckResult(); } } diff --git a/SchematicSystem_12/src/de/steamwar/schematicsystem/CheckSchemType_12.java b/SchematicSystem_12/src/de/steamwar/schematicsystem/CheckSchemType_12.java index a6a51a3..b853c1c 100644 --- a/SchematicSystem_12/src/de/steamwar/schematicsystem/CheckSchemType_12.java +++ b/SchematicSystem_12/src/de/steamwar/schematicsystem/CheckSchemType_12.java @@ -16,6 +16,7 @@ import java.util.List; import java.util.Set; import java.util.logging.Level; +@SuppressWarnings("deprecation") class CheckSchemType_12 { private static final int TNT = Material.TNT.getId(); private static final int SLIME = Material.SLIME_BLOCK.getId(); @@ -66,23 +67,24 @@ class CheckSchemType_12 { private CheckSchemType_12(){} - static void autoCheck(Schematic schematic, List errors, List warnings, ICheckSchemType type) { + static AutoCheckResult autoCheck(Schematic schematic, ICheckSchemType type) { + AutoCheckResult result = new AutoCheckResult(type); Clipboard clipboard; try { clipboard = schematic.load(); } catch (Schematic.WrongVersionException e) { - errors.add("Der Schematictyp dieser Schematic kann nicht in dieser Version geändert werden."); - return; + result.setWrongVersionException(); + return result; } catch (IOException | NoClipboardException e) { Bukkit.getLogger().log(Level.SEVERE, "Schematic could not be loaded", e); - errors.add("Die Schematic konnte nicht geladen werden"); - return; + result.setErrorLoadingSchematic(); + return result; } Vector dimensions = clipboard.getDimensions(); - - if(dimensions.getBlockX() > type.getWidth() || dimensions.getBlockY() > type.getHeight() || dimensions.getBlockZ() > type.getDepth()) - errors.add("Das " + type.getName() + " überschreitet die Maximalmaße"); + result.setLength(dimensions.getBlockX()); + result.setHeight(dimensions.getBlockY()); + result.setWidth(dimensions.getBlockZ()); Region region = clipboard.getRegion(); Vector min = region.getMinimumPoint(); @@ -108,27 +110,30 @@ class CheckSchemType_12 { dispenser++; if(INVENTORY.contains(blockId)){ - checkInventory(errors, warnings, block, blockId, type); + checkInventory(result, block, blockId, type); } - if(type.getForbidden().contains(Material.getMaterial(blockId).name())) - errors.add("Der Block " + Material.getMaterial(blockId).name() + " ist verboten"); + result.checkMaterial(Material.getMaterial(blockId).name()); } } } - type.finalChecks(errors, tnt, slime, dispenser); + result.setTNT(tnt); + result.setSlime(slime); + result.setDispenser(dispenser); + + return result; } - private static void checkInventory(List errors, List warnings, BaseBlock block, int blockId, ICheckSchemType type){ + private static void checkInventory(AutoCheckResult result, BaseBlock block, int blockId, ICheckSchemType type){ CompoundTag nbt = block.getNbtData(); if(nbt == null){ - warnings.add("Ein(e) " + Material.getMaterial(blockId).name() + " ist defekt"); + result.defunctNbt(Material.getMaterial(blockId).name()); return; } if(blockId == JUKEBOX && nbt.getValue().containsKey("RecordItem")){ - errors.add("Schallplatten sind auch nicht in Schallplattenspielern gestattet"); + result.foundRecord(); return; } @@ -139,7 +144,7 @@ class CheckSchemType_12 { int counter = 0; for(CompoundTag item : items){ if(!item.containsKey("id")){ - warnings.add("Ein(e) " + Material.getMaterial(blockId).name() + " ist defekt"); + result.defunctNbt(Material.getMaterial(blockId).name()); continue; } @@ -152,10 +157,9 @@ class CheckSchemType_12 { if(blockId == DISPENSER && (itemType.equals(Material.FIREBALL) || itemType.equals(Material.ARROW))) counter += item.getByte("Count"); else if(!FLOWERS.contains(itemType) && !(blockId == CHEST && itemType.equals(Material.TNT))) - errors.add("In einem/r " + Material.getMaterial(blockId).name() + " ist das verbotene Item " + itemType); + result.foundForbiddenItem(Material.getMaterial(blockId).name(), itemType.name(), item.getByte("Count")); } - if(counter > type.getMaxDispenserItems()) - errors.add("Ein Werfer enthält mehr als " + type.getMaxDispenserItems() + " Pfeile und Feuerbälle"); + result.dispenserItems(counter); } } diff --git a/SchematicSystem_14/src/de/steamwar/schematicsystem/CheckSchemType_14.java b/SchematicSystem_14/src/de/steamwar/schematicsystem/CheckSchemType_14.java index bae671f..5630d66 100644 --- a/SchematicSystem_14/src/de/steamwar/schematicsystem/CheckSchemType_14.java +++ b/SchematicSystem_14/src/de/steamwar/schematicsystem/CheckSchemType_14.java @@ -1,11 +1,9 @@ package de.steamwar.schematicsystem; -import java.util.List; - class CheckSchemType_14 { private CheckSchemType_14(){} - static void autoCheck(List errors) { - errors.add("In der 1.14 können keine Schematics eingesendet werden"); + static AutoCheckResult autoCheck() { + return new AutoCheckResult(); } } diff --git a/SchematicSystem_15/src/de/steamwar/schematicsystem/CheckSchemType_15.java b/SchematicSystem_15/src/de/steamwar/schematicsystem/CheckSchemType_15.java index bf6605c..119e2aa 100644 --- a/SchematicSystem_15/src/de/steamwar/schematicsystem/CheckSchemType_15.java +++ b/SchematicSystem_15/src/de/steamwar/schematicsystem/CheckSchemType_15.java @@ -68,23 +68,24 @@ class CheckSchemType_15 { private CheckSchemType_15(){} - static void autoCheck(Schematic schematic, List errors, List warnings, ICheckSchemType type) { + static AutoCheckResult autoCheck(Schematic schematic, ICheckSchemType type) { + AutoCheckResult result = new AutoCheckResult(type); Clipboard clipboard; try { clipboard = schematic.load(); } catch (Schematic.WrongVersionException e) { - errors.add("Der Schematictyp dieser Schematic kann nicht in dieser Version geändert werden."); - return; + result.setWrongVersionException(); + return result; } catch (IOException | NoClipboardException e) { + result.setErrorLoadingSchematic(); Bukkit.getLogger().log(Level.SEVERE, "Schematic could not be loaded", e); - errors.add("Die Schematic konnte nicht geladen werden"); - return; + return result; } BlockVector3 dimensions = clipboard.getDimensions(); - - if(dimensions.getBlockX() > type.getWidth() || dimensions.getBlockY() > type.getHeight() || dimensions.getBlockZ() > type.getDepth()) - errors.add("Das " + type.getName() + " überschreitet die Maximalmaße"); + result.setLength(dimensions.getBlockX()); + result.setHeight(dimensions.getBlockY()); + result.setWidth(dimensions.getBlockZ()); Region region = clipboard.getRegion(); BlockVector3 min = region.getMinimumPoint(); @@ -110,27 +111,31 @@ class CheckSchemType_15 { dispenser++; if(INVENTORY.contains(blockMaterial)){ - checkInventory(errors, warnings, block, blockMaterial, type); + checkInventory(result, block, blockMaterial, type); } - if(type.getForbidden().contains(blockMaterial.name())) - errors.add("Der Block " + blockMaterial.name() + " ist verboten"); + assert blockMaterial != null; + result.checkMaterial(blockMaterial.name()); } } } - type.finalChecks(errors, tnt, slime, dispenser); + result.setTNT(tnt); + result.setSlime(slime); + result.setDispenser(dispenser); + + return result; } - private static void checkInventory(List errors, List warnings, BaseBlock block, Material blockMaterial, ICheckSchemType type){ + private static void checkInventory(AutoCheckResult result, BaseBlock block, Material blockMaterial, ICheckSchemType type){ CompoundTag nbt = block.getNbtData(); if(nbt == null){ - warnings.add("Ein(e) " + blockMaterial.name() + " ist defekt"); + result.defunctNbt(blockMaterial.name()); return; } if(blockMaterial == Material.JUKEBOX && nbt.getValue().containsKey("RecordItem")){ - errors.add("Schallplatten sind auch nicht in Schallplattenspielern gestattet"); + result.foundRecord(); return; } @@ -141,7 +146,7 @@ class CheckSchemType_15 { int counter = 0; for(CompoundTag item : items){ if(!item.containsKey("id")){ - warnings.add("Ein(e) " + blockMaterial.name() + " ist defekt"); + result.defunctNbt(blockMaterial.name()); continue; } @@ -152,10 +157,9 @@ class CheckSchemType_15 { if(blockMaterial == Material.DISPENSER && (itemType.equals(Material.FIRE_CHARGE) || itemType.equals(Material.ARROW))) counter += item.getByte("Count"); else if(!FLOWERS.contains(itemType) && !(blockMaterial == Material.CHEST && itemType.equals(Material.TNT))) - errors.add("In einem/r " + blockMaterial.name() + " ist das verbotene Item " + itemType); + result.foundForbiddenItem(blockMaterial.name(), itemType.name(), item.getByte("Count")); } - if(counter > type.getMaxDispenserItems()) - errors.add("Ein Werfer enthält mehr als " + type.getMaxDispenserItems() + " Pfeile und Feuerbälle"); + result.dispenserItems(counter); } } diff --git a/SchematicSystem_8/src/de/steamwar/schematicsystem/CheckSchemType_8.java b/SchematicSystem_8/src/de/steamwar/schematicsystem/CheckSchemType_8.java index aad4abc..38244c2 100644 --- a/SchematicSystem_8/src/de/steamwar/schematicsystem/CheckSchemType_8.java +++ b/SchematicSystem_8/src/de/steamwar/schematicsystem/CheckSchemType_8.java @@ -1,11 +1,9 @@ package de.steamwar.schematicsystem; -import java.util.List; - class CheckSchemType_8 { private CheckSchemType_8(){} - static void autoCheck(List errors) { - errors.add("In der 1.8 können keine Schematics eingesendet werden"); + static AutoCheckResult autoCheck() { + return new AutoCheckResult(); } } diff --git a/SchematicSystem_9/src/de/steamwar/schematicsystem/CheckSchemType_9.java b/SchematicSystem_9/src/de/steamwar/schematicsystem/CheckSchemType_9.java index cf18f33..6ac3141 100644 --- a/SchematicSystem_9/src/de/steamwar/schematicsystem/CheckSchemType_9.java +++ b/SchematicSystem_9/src/de/steamwar/schematicsystem/CheckSchemType_9.java @@ -1,11 +1,9 @@ package de.steamwar.schematicsystem; -import java.util.List; - class CheckSchemType_9 { private CheckSchemType_9(){} - static void autoCheck(List errors) { - errors.add("In der 1.9 können keine Schematics eingesendet werden"); + static AutoCheckResult autoCheck() { + return new AutoCheckResult(); } } diff --git a/SchematicSystem_API/src/de/steamwar/schematicsystem/AutoCheckResult.java b/SchematicSystem_API/src/de/steamwar/schematicsystem/AutoCheckResult.java new file mode 100644 index 0000000..1b55480 --- /dev/null +++ b/SchematicSystem_API/src/de/steamwar/schematicsystem/AutoCheckResult.java @@ -0,0 +1,151 @@ +package de.steamwar.schematicsystem; + +import de.steamwar.core.Core; + +import java.util.*; + +public class AutoCheckResult { + + private final ICheckSchemType type; + + private final boolean noChecksInThisVersion; + private boolean wrongVersionException = false; // Schematic nicht in der Version ladbar + private boolean errorLoadingSchematic = false; // Schematic irgendwie invalide + + private int width = 0; + private int height = 0; + private int length = 0; + + private int tnt = 0; + private int slime = 0; + private int dispenser = 0; + + private Map forbiddenMaterials = new HashMap<>(); // Anzahl verbotener Blöcke nach Material + + private Map defunctNbt = new HashMap<>(); // Anzahl an defekten NBT-Blöcken nach Materialname + private int records = 0; // Gefundene Schallplatten + private Map> forbiddenItems = new HashMap<>(); // Anzahl verbotener Items nach Inventartyp + private int tooManyDispenserItems = 0; // Gefundene Überschreitungen von DispenserItems + + AutoCheckResult(ICheckSchemType type){ + this.type = type; + noChecksInThisVersion = false; + } + + AutoCheckResult(){ + type = null; + noChecksInThisVersion = true; + } + + public Collection errors(){ + List errors = new LinkedList<>(); + + if(noChecksInThisVersion){ + errors.add("In der 1." + Core.getVersion() + " können keine Schematics eingesendet werden"); + return errors; + } + if(wrongVersionException) + errors.add("Diese Schematic kann nicht in dieser Version geändert werden"); + if(errorLoadingSchematic) + errors.add("Die Schematic konnte nicht geladen werden"); + + assert type != null; + if(width > type.getDepth()) + errors.add("Die Schematic ist zu breit (" + width + " > " + type.getDepth() + ")"); + if(length > type.getWidth()) + errors.add("Die Schematic ist zu lang (" + length + " > " + type.getWidth() + ")"); + if(height > type.getHeight()) + errors.add("Die Schematic ist zu hoch (" + height + " > " + type.getHeight() + ")"); + + int errorTNTSlime = slime + tnt; + if(type.getMaxTNT() != 0 && tnt > type.getMaxTNT()) + errors.add("Zu viele TNT-Blöcke (" + tnt + " > " + type.getMaxTNT() + ")"); + if(type.getMaxSlime() != 0 && slime > type.getMaxSlime()) + errors.add("Zu viele Schleim-Blöcke (" + slime + " > " + type.getMaxSlime() + ")"); + if(type.getMaxDispenser() != 0 && dispenser > type.getMaxDispenser()) + errors.add("Zu viele Werfer (" + dispenser + " > " + type.getMaxDispenser() + ")"); + if(type.getMaxTNTSlime() != 0 && errorTNTSlime > type.getMaxTNTSlime()) + errors.add("Zu viel Schleim+TNT" + errorTNTSlime + " > " + type.getMaxTNTSlime() + ")"); + + for(Map.Entry block : forbiddenMaterials.entrySet()) + errors.add("Der Block " + block.getKey() + " ist verboten (" + block.getValue() + " verbaut)"); + + if(records > 0) + errors.add("Keine Schallplatten erlaubt (" + records + " gefunden)"); + for(Map.Entry> block : forbiddenItems.entrySet()) + for(Map.Entry item : block.getValue().entrySet()) + errors.add("In " + block.getKey() + "s wurde das verbotene Item " + item.getKey() + " " + item.getValue() + " mal gefunden"); + if(tooManyDispenserItems == 1) + errors.add("Ein Werfer enthält mehr als " + type.getMaxDispenserItems() + " Pfeile und Feuerbälle"); + else if(tooManyDispenserItems > 1) + errors.add(tooManyDispenserItems + " Werfer enthalten mehr als " + type.getMaxDispenserItems() + " Pfeile und Feuerbälle"); + + return errors; + } + + public Collection warnings(){ + List warnings = new LinkedList<>(); + + for(Map.Entry nbtBlock : defunctNbt.entrySet()){ + if(nbtBlock.getValue() > 1) + warnings.add(nbtBlock.getValue() + " " + nbtBlock.getKey() + "s enthalten keine oder inkorrekte NBT-Daten"); + else + warnings.add("Ein(e) " + nbtBlock.getKey() + " enthält keine oder inkorrekte NBT-Daten"); + } + + return warnings; + } + + void setWrongVersionException(){ + wrongVersionException = true; + } + void setErrorLoadingSchematic(){ + errorLoadingSchematic = true; + } + + void setWidth(int width){ + this.width = width; + } + void setLength(int length){ + this.length = length; + } + void setHeight(int height){ + this.height = height; + } + + void setTNT(int tnt){ + this.tnt = tnt; + } + void setSlime(int slime){ + this.slime = slime; + } + void setDispenser(int dispenser){ + this.dispenser = dispenser; + } + + void checkMaterial(String name){ + assert type != null; + if(type.getForbidden().contains(name)) + forbiddenMaterials.compute(name, (k, v) -> v == null ? 1 : v+1); + } + + void defunctNbt(String name){ + defunctNbt.compute(name, (k, v) -> v == null ? 1 : v+1); + } + void foundRecord(){ + records++; + } + void foundForbiddenItem(String material, String item, int count){ + forbiddenItems.compute(material, (k1, v) -> { + if(v == null) + v = new HashMap<>(); + v.compute(item, (k2, v2) -> v2 == null ? count : v2 + count); + return v; + }); + } + void dispenserItems(int counter){ + assert type != null; + if(counter > type.getMaxDispenserItems()) + tooManyDispenserItems++; + } +} diff --git a/SchematicSystem_API/src/de/steamwar/schematicsystem/ICheckSchemType.java b/SchematicSystem_API/src/de/steamwar/schematicsystem/ICheckSchemType.java index 8ec60dc..e6c9c63 100644 --- a/SchematicSystem_API/src/de/steamwar/schematicsystem/ICheckSchemType.java +++ b/SchematicSystem_API/src/de/steamwar/schematicsystem/ICheckSchemType.java @@ -9,8 +9,10 @@ interface ICheckSchemType { int getHeight(); int getDepth(); int getMaxDispenserItems(); + int getMaxDispenser(); + int getMaxTNT(); + int getMaxTNTSlime(); + int getMaxSlime(); List getForbidden(); - - void finalChecks(List errors, int tnt, int slime, int dispenser); } diff --git a/SchematicSystem_Main/src/de/steamwar/schematicsystem/CheckSchemType.java b/SchematicSystem_Main/src/de/steamwar/schematicsystem/CheckSchemType.java index 85d78f5..046dbf3 100644 --- a/SchematicSystem_Main/src/de/steamwar/schematicsystem/CheckSchemType.java +++ b/SchematicSystem_Main/src/de/steamwar/schematicsystem/CheckSchemType.java @@ -58,41 +58,23 @@ public class CheckSchemType implements ICheckSchemType { return SchematicType.fromDB(name); } - public void autoCheck(Schematic schematic, List errors, List warnings) { + public AutoCheckResult autoCheck(Schematic schematic) { switch(Core.getVersion()){ case 15: - CheckSchemType_15.autoCheck(schematic, errors, warnings, this); - break; + return CheckSchemType_15.autoCheck(schematic, this); case 14: - CheckSchemType_14.autoCheck(errors); - break; + return CheckSchemType_14.autoCheck(); case 10: - CheckSchemType_10.autoCheck(errors); - break; + return CheckSchemType_10.autoCheck(); case 9: - CheckSchemType_9.autoCheck(errors); - break; + return CheckSchemType_9.autoCheck(); case 8: - CheckSchemType_8.autoCheck(errors); - break; + return CheckSchemType_8.autoCheck(); default: - CheckSchemType_12.autoCheck(schematic, errors, warnings, this); + return CheckSchemType_12.autoCheck(schematic, this); } } - @Override - public void finalChecks(List errors, int tnt, int slime, int dispenser){ - int tntSlime = tnt + slime; - if(maxTNT != 0 && tnt > maxTNT) - errors.add("Zu viele TNT-Blöcke"); - if(maxSlime != 0 && slime > maxSlime) - errors.add("Zu viele Schleim-Blöcke"); - if(maxDispenser != 0 && dispenser > maxDispenser) - errors.add("Zu viele Werfer"); - if(maxTNTSlime != 0 && tntSlime > maxTNTSlime) - errors.add("Zu viel Schleim+TNT"); - } - @Override public String getName() { return name; @@ -118,6 +100,26 @@ public class CheckSchemType implements ICheckSchemType { return maxDispenserItems; } + @Override + public int getMaxDispenser() { + return maxDispenser; + } + + @Override + public int getMaxTNT() { + return maxTNT; + } + + @Override + public int getMaxTNTSlime() { + return maxTNTSlime; + } + + @Override + public int getMaxSlime() { + return maxSlime; + } + @Override public List getForbidden() { return forbiddenMaterials; diff --git a/SchematicSystem_Main/src/de/steamwar/schematicsystem/commands/SchematicCommand.java b/SchematicSystem_Main/src/de/steamwar/schematicsystem/commands/SchematicCommand.java index d8be893..1b9fcfc 100644 --- a/SchematicSystem_Main/src/de/steamwar/schematicsystem/commands/SchematicCommand.java +++ b/SchematicSystem_Main/src/de/steamwar/schematicsystem/commands/SchematicCommand.java @@ -1,5 +1,6 @@ package de.steamwar.schematicsystem.commands; +import de.steamwar.schematicsystem.AutoCheckResult; import de.steamwar.schematicsystem.CheckSchemType; import de.steamwar.schematicsystem.SchematicSystem; import de.steamwar.schematicsystem.check.CheckUtils; @@ -17,7 +18,7 @@ import org.bukkit.entity.Player; import java.io.IOException; import java.util.ArrayList; -import java.util.LinkedList; +import java.util.Collection; import java.util.List; import java.util.logging.Level; @@ -245,13 +246,12 @@ public class SchematicCommand implements CommandExecutor { return; } - List errors = new LinkedList<>(); - List warnings = new LinkedList<>(); - CheckSchemType.get(type).autoCheck(schematic, errors, warnings); - for(String warning : warnings){ + AutoCheckResult result = CheckSchemType.get(type).autoCheck(schematic); + Collection errors = result.errors(); + for(String warning : errors){ player.sendMessage(" §e" + warning); } - for(String error : errors){ + for(String error : result.warnings()){ player.sendMessage(" §c" + error); } if(!errors.isEmpty()){ @@ -261,7 +261,6 @@ public class SchematicCommand implements CommandExecutor { schematic.setSchemType(type.checkType()); player.sendMessage(SchematicSystem.PREFIX + "§aDie Schematic wird zeitnah überprüft"); - CheckUtils.sendTeamMembersCSchematics(SchematicSystem.PREFIX + player.getName() + " §7hat ein " + type.name() + " eingesendet"); } } From 8478d2059ece7737417be6b4539039bf6ebb85af Mon Sep 17 00:00:00 2001 From: Lixfel Date: Fri, 27 Dec 2019 08:51:49 +0100 Subject: [PATCH 2/2] Fixing compilation errors --- SchematicSystem_API/pom.xml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/SchematicSystem_API/pom.xml b/SchematicSystem_API/pom.xml index 9cbe30a..dafc6b5 100644 --- a/SchematicSystem_API/pom.xml +++ b/SchematicSystem_API/pom.xml @@ -25,4 +25,13 @@ + + + + steamwar + Spigot + 1.12 + provided + + \ No newline at end of file