From 4d6b3ef738a741b0a8e10ba39e8db7f298aae2d9 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Thu, 17 Oct 2019 17:31:38 +0200 Subject: [PATCH] Automatic Inventory checking + Multiple Autocheck errors --- .../schematicsystem/CheckSchemType.java | 116 ++++++++++++++++-- .../commands/SchematicCommand.java | 17 ++- 2 files changed, 117 insertions(+), 16 deletions(-) diff --git a/src/de/steamwar/schematicsystem/CheckSchemType.java b/src/de/steamwar/schematicsystem/CheckSchemType.java index ddf544b..bfb3cbc 100644 --- a/src/de/steamwar/schematicsystem/CheckSchemType.java +++ b/src/de/steamwar/schematicsystem/CheckSchemType.java @@ -1,7 +1,9 @@ package de.steamwar.schematicsystem; import com.boydti.fawe.FaweAPI; +import com.sk89q.jnbt.CompoundTag; import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.regions.Region; import de.warking.hunjy.MySQL.Schematic; @@ -21,6 +23,48 @@ public class CheckSchemType { private static final int TNT = Material.TNT.getId(); private static final int SLIME = Material.SLIME_BLOCK.getId(); private static final int DISPENSER = Material.DISPENSER.getId(); + private static final int JUKEBOX = Material.JUKEBOX.getId(); + private static final int CHEST = Material.CHEST.getId(); + private static final Set INVENTORY; + private static final Set FLOWERS; + + static{ + Set inventory = new HashSet<>(); + inventory.add(CHEST); + inventory.add(Material.TRAPPED_CHEST.getId()); + inventory.add(Material.HOPPER.getId()); + inventory.add(Material.FURNACE.getId()); + inventory.add(Material.BURNING_FURNACE.getId()); + inventory.add(JUKEBOX); //RecordItem + inventory.add(DISPENSER); + inventory.add(Material.DROPPER.getId()); + inventory.add(Material.ANVIL.getId()); + inventory.add(Material.BREWING_STAND.getId()); + inventory.add(Material.BLACK_SHULKER_BOX.getId()); + inventory.add(Material.RED_SHULKER_BOX.getId()); + inventory.add(Material.WHITE_SHULKER_BOX.getId()); + inventory.add(Material.ORANGE_SHULKER_BOX.getId()); + inventory.add(Material.BLUE_SHULKER_BOX.getId()); + inventory.add(Material.LIGHT_BLUE_SHULKER_BOX.getId()); + inventory.add(Material.YELLOW_SHULKER_BOX.getId()); + inventory.add(Material.GREEN_SHULKER_BOX.getId()); + inventory.add(Material.BROWN_SHULKER_BOX.getId()); + inventory.add(Material.CYAN_SHULKER_BOX.getId()); + inventory.add(Material.GRAY_SHULKER_BOX.getId()); + inventory.add(Material.SILVER_SHULKER_BOX.getId()); + inventory.add(Material.LIME_SHULKER_BOX.getId()); + inventory.add(Material.MAGENTA_SHULKER_BOX.getId()); + inventory.add(Material.PINK_SHULKER_BOX.getId()); + inventory.add(Material.PURPLE_SHULKER_BOX.getId()); + INVENTORY = inventory; + + Set flowers = new HashSet<>(); + flowers.add(Material.CHORUS_FLOWER); + flowers.add(Material.YELLOW_FLOWER); + flowers.add(Material.RED_ROSE); + flowers.add(Material.DOUBLE_PLANT); + FLOWERS = flowers; + } private static final Map types = new EnumMap<>(SchematicType.class); @@ -34,6 +78,7 @@ public class CheckSchemType { private final int maxSlime; private final int maxTNTSlime; private final int maxDispenser; + private final int maxDispenserItems; private final List forbiddenIds; private final LinkedList checkList; @@ -47,6 +92,7 @@ public class CheckSchemType { maxSlime = section.getInt("maxSlime"); maxTNTSlime = section.getInt("maxTNTSlime"); maxDispenser = section.getInt("maxDispenser"); + maxDispenserItems = section.getInt("maxDispenserItems"); forbiddenIds = section.getIntegerList("forbiddenIds"); checkList = new LinkedList<>(section.getStringList("checkList")); @@ -66,7 +112,7 @@ public class CheckSchemType { return SchematicType.fromDB(name); } - public String autoCheck(Schematic schematic) { + public void autoCheck(Schematic schematic, List errors, List warnings) { Clipboard clipboard; try { @@ -75,13 +121,14 @@ public class CheckSchemType { throw new IOException(); } catch (IOException e) { Bukkit.getLogger().log(Level.SEVERE, "Schematic could not be loaded", e); - return "Die Schematic konnte nicht geladen werden"; + errors.add("Die Schematic konnte nicht geladen werden"); + return; } Vector dimensions = clipboard.getDimensions(); if(dimensions.getBlockX() > width || dimensions.getBlockY() > height || dimensions.getBlockZ() > depth) - return "Das " + name + " überschreitet die Maximalmaße"; + errors.add("Das " + name + " überschreitet die Maximalmaße"); Region region = clipboard.getRegion(); Vector min = region.getMinimumPoint(); @@ -94,8 +141,8 @@ public class CheckSchemType { for(int x = min.getBlockX(); x <= max.getBlockX(); x++){ for(int y = min.getBlockY(); y <= max.getBlockY(); y++){ for(int z = min.getBlockZ(); z <= max.getBlockZ(); z++){ - Vector vector = new Vector(x, y, z); - final int blockId = clipboard.getBlock(vector).getId(); + final BaseBlock block = clipboard.getBlock(new Vector(x, y, z)); + final int blockId = block.getId(); if(blockId == TNT) tnt++; @@ -106,22 +153,67 @@ public class CheckSchemType { if(blockId == DISPENSER) dispenser++; + if(INVENTORY.contains(blockId)){ + checkInventory(errors, warnings, block, blockId); + } + if(forbiddenIds.contains(blockId)) - return "Der Block " + Material.getMaterial(blockId).name() + " ist verboten"; + errors.add("Der Block " + Material.getMaterial(blockId).name() + " ist verboten"); } } } + finalChecks(errors, tnt, slime, dispenser); + } + + private void checkInventory(List errors, List warnings, BaseBlock block, int blockId){ + CompoundTag nbt = block.getNbtData(); + if(nbt == null){ + warnings.add("Ein(e) " + Material.getMaterial(blockId).name() + " ist defekt"); + return; + } + + if(blockId == JUKEBOX && nbt.getValue().containsKey("RecordItem")){ + errors.add("Schallplatten sind auch nicht in Schallplattenspielern gestattet"); + return; + } + + List items = nbt.getList("Items", CompoundTag.class); + if(items.isEmpty()) + return; //Leeres Inventar + + int counter = 0; + for(CompoundTag item : items){ + if(!item.containsKey("id")){ + warnings.add("Ein(e) " + Material.getMaterial(blockId).name() + " ist defekt"); + continue; + } + + Material type = Material.getMaterial(item.getString("id").split(":")[1].toUpperCase()); + if(type == null && item.getString("id").equals("minecraft:fire_charge")) + type = Material.FIREBALL; + if(type == null) //Leere Slots + continue; + + if(blockId == DISPENSER && (type.equals(Material.FIREBALL) || type.equals(Material.ARROW))) + counter += item.getByte("Count"); + else if(!FLOWERS.contains(type) && !(blockId == CHEST && type.equals(Material.TNT))) + errors.add("In einem/r " + Material.getMaterial(blockId).name() + " ist das verbotene Item " + type); + } + + if(counter > maxDispenserItems) + errors.add("Ein Werfer enthält mehr als " + maxDispenserItems + " Pfeile und Feuerbälle"); + } + + private void finalChecks(List errors, int tnt, int slime, int dispenser){ int tntSlime = tnt + slime; if(maxTNT != 0 && tnt > maxTNT) - return "Zu viele TNT-Blöcke"; + errors.add("Zu viele TNT-Blöcke"); if(maxSlime != 0 && slime > maxSlime) - return "Zu viele Schleim-Blöcke"; + errors.add("Zu viele Schleim-Blöcke"); if(maxDispenser != 0 && dispenser > maxDispenser) - return "Zu viele Werfer"; + errors.add("Zu viele Werfer"); if(maxTNTSlime != 0 && tntSlime > maxTNTSlime) - return "Zu viel Schleim+TNT"; - - return null; + errors.add("Zu viel Schleim+TNT"); } } diff --git a/src/de/steamwar/schematicsystem/commands/SchematicCommand.java b/src/de/steamwar/schematicsystem/commands/SchematicCommand.java index ca30f61..cf55a6b 100644 --- a/src/de/steamwar/schematicsystem/commands/SchematicCommand.java +++ b/src/de/steamwar/schematicsystem/commands/SchematicCommand.java @@ -32,6 +32,7 @@ import java.nio.file.Files; import java.nio.file.attribute.PosixFilePermission; import java.util.ArrayList; import java.util.EnumSet; +import java.util.LinkedList; import java.util.List; import java.util.logging.Level; @@ -281,14 +282,22 @@ public class SchematicCommand implements CommandExecutor { return; } - String reason = CheckSchemType.get(type).autoCheck(schematic); - if(reason != null){ - player.sendMessage(SchematicSystem.PREFIX + "§c" + reason); + List errors = new LinkedList<>(); + List warnings = new LinkedList<>(); + CheckSchemType.get(type).autoCheck(schematic, errors, warnings); + for(String warning : warnings){ + player.sendMessage(" §e" + warning); + } + for(String error : errors){ + player.sendMessage(" §c" + error); + } + if(!errors.isEmpty()){ + player.sendMessage(SchematicSystem.PREFIX + "§cDie Schematic ist nicht regelkonform"); return; } schematic.setSchemType(type.checkType()); - player.sendMessage(SchematicSystem.PREFIX + "§aDie Schematic wird zeitnah auf Regelkonformität überprüft"); + player.sendMessage(SchematicSystem.PREFIX + "§aDie Schematic wird zeitnah überprüft"); CheckUtils.sendTeamMembersCSchematics(SchematicSystem.PREFIX + player.getName() + " §7hat ein " + type.name() + " eingesendet"); } }