From e3880b3a30d13a7c7f79585da79041e9b347e25e Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 4 Apr 2021 22:50:40 +0200 Subject: [PATCH 1/7] Add Color Add Region.colors Add World config colors.json --- .../steamwar/bausystem/world/Region_15.java | 4 +- .../src/de/steamwar/bausystem/BauSystem.java | 2 + .../de/steamwar/bausystem/world/Color.java | 39 +++++++++++++ .../de/steamwar/bausystem/world/Region.java | 55 ++++++++++++++++++- 4 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/world/Color.java diff --git a/BauSystem_15/src/de/steamwar/bausystem/world/Region_15.java b/BauSystem_15/src/de/steamwar/bausystem/world/Region_15.java index 5fcadac..020e604 100644 --- a/BauSystem_15/src/de/steamwar/bausystem/world/Region_15.java +++ b/BauSystem_15/src/de/steamwar/bausystem/world/Region_15.java @@ -23,18 +23,18 @@ import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.bukkit.BukkitWorld; import com.sk89q.worldedit.extent.clipboard.Clipboard; -import com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat; import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats; import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader; import com.sk89q.worldedit.function.operation.Operations; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.transform.AffineTransform; import com.sk89q.worldedit.session.ClipboardHolder; +import org.bukkit.Bukkit; + import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Objects; -import org.bukkit.Bukkit; class Region_15 { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index f946b96..a3207a2 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -159,6 +159,8 @@ public class BauSystem extends JavaPlugin implements Listener { autoShutdown.cancel(); } CommandTPSLimiter.setTPS(20.0); + System.out.println("SAVING colors"); + Region.saveColors(); autoShutdown = Bukkit.getScheduler().runTaskTimer(this, new Runnable() { int count = 0; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/Color.java b/BauSystem_Main/src/de/steamwar/bausystem/world/Color.java new file mode 100644 index 0000000..eb8d44d --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/Color.java @@ -0,0 +1,39 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.world; + +public enum Color { + WHITE, + ORANGE, + MAGENTA, + LIGHT_BLUE, + YELLOW, + LIME, + PINK, + GRAY, + LIGHT_GRAY, + CYAN, + PURPLE, + BLUE, + BROWN, + GREEN, + RED, + BLACK +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java b/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java index e06c85e..de4dd49 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java @@ -19,6 +19,7 @@ package de.steamwar.bausystem.world; +import com.google.gson.*; import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.extent.clipboard.Clipboard; import de.steamwar.bausystem.commands.CommandTNT.TNTMode; @@ -31,8 +32,7 @@ import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.util.*; import java.util.function.Consumer; import java.util.logging.Level; @@ -42,8 +42,18 @@ public class Region { private static final List regions = new ArrayList<>(); private static boolean buildArea = false; private static boolean extensionArea = false; + private static JsonObject colors = new JsonObject(); static { + File colorsFile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "colors.json"); + if (colorsFile.exists()) { + try { + colors = new JsonParser().parse(new FileReader(colorsFile)).getAsJsonObject(); + } catch (JsonSyntaxException | IOException e) { + Bukkit.getLogger().log(Level.WARNING, "Item JSON error"); + } + } + YamlConfiguration config = new YamlConfiguration(); try { config.load(new File(Bukkit.getWorlds().get(0).getWorldFolder(), "sections.yml")); @@ -78,6 +88,24 @@ public class Region { } return GlobalRegion.getInstance(); } + + public static void saveColors() { + File colorsFile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "colors.json"); + if (!colorsFile.exists()) { + try { + colorsFile.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + return; + } + } + try (FileOutputStream fileOutputStream = new FileOutputStream(colorsFile)) { + fileOutputStream.write(colors.toString().getBytes()); + } catch (IOException e) { + e.printStackTrace(); + // Ignored + } + } private final String name; private final int minX; @@ -93,8 +121,22 @@ public class Region { private boolean freeze = false; private boolean fire = false; + private Color color = Color.YELLOW; + private Region(ConfigurationSection config) { name = config.getName(); + if (colors.has(name)) { + String colorName = colors.getAsJsonPrimitive(name).getAsString(); + try { + color = Color.valueOf(colorName); + } catch (Exception e) { + // Ignored + } + if (color == null) { + color = Color.YELLOW; + } + } + minX = config.getInt("minX"); minY = config.getInt("minY"); minZ = config.getInt("minZ"); @@ -114,6 +156,15 @@ public class Region { tntMode = TNTMode.OFF; } + public Color getColor() { + return color; + } + + public void setColor(Color color) { + this.color = color; + colors.add(name, new JsonPrimitive(color.name())); + } + private void setLinkedRegion(Consumer regionConsumer) { if (optionsLinkedWith == null) { return; From e5c2fe20b079b276af12589d2d6faeaf1f98897f Mon Sep 17 00:00:00 2001 From: Zeanon Date: Mon, 5 Apr 2021 12:08:03 +0200 Subject: [PATCH 2/7] Added CommandColor --- .../src/de/steamwar/bausystem/world/Color.java | 2 +- .../de/steamwar/bausystem/commands/CommandColor.java | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) rename {BauSystem_Main => BauSystem_API}/src/de/steamwar/bausystem/world/Color.java (98%) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/commands/CommandColor.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/Color.java b/BauSystem_API/src/de/steamwar/bausystem/world/Color.java similarity index 98% rename from BauSystem_Main/src/de/steamwar/bausystem/world/Color.java rename to BauSystem_API/src/de/steamwar/bausystem/world/Color.java index eb8d44d..2f06881 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/Color.java +++ b/BauSystem_API/src/de/steamwar/bausystem/world/Color.java @@ -35,5 +35,5 @@ public enum Color { BROWN, GREEN, RED, - BLACK + BLACK; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandColor.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandColor.java new file mode 100644 index 0000000..c0a124b --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandColor.java @@ -0,0 +1,10 @@ +package de.steamwar.bausystem.commands; + +import com.sk89q.worldedit.extent.clipboard.Clipboard; + + +public class CommandColor { + + + +} \ No newline at end of file From 3e9a103018fd1b3c6fe3ab44ba2f4510044794ad Mon Sep 17 00:00:00 2001 From: Zeanon Date: Mon, 5 Apr 2021 12:19:09 +0200 Subject: [PATCH 3/7] added changeColor --- .../steamwar/bausystem/world/Region_15.java | 119 +++++++++++++++++- 1 file changed, 117 insertions(+), 2 deletions(-) diff --git a/BauSystem_15/src/de/steamwar/bausystem/world/Region_15.java b/BauSystem_15/src/de/steamwar/bausystem/world/Region_15.java index 020e604..8b29993 100644 --- a/BauSystem_15/src/de/steamwar/bausystem/world/Region_15.java +++ b/BauSystem_15/src/de/steamwar/bausystem/world/Region_15.java @@ -21,6 +21,7 @@ package de.steamwar.bausystem.world; import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.WorldEdit; +import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.bukkit.BukkitWorld; import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats; @@ -29,12 +30,13 @@ import com.sk89q.worldedit.function.operation.Operations; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.transform.AffineTransform; import com.sk89q.worldedit.session.ClipboardHolder; -import org.bukkit.Bukkit; - +import com.sk89q.worldedit.world.block.BlockType; +import com.sk89q.worldedit.world.block.BlockTypes; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Objects; +import org.bukkit.Bukkit; class Region_15 { @@ -70,4 +72,117 @@ class Region_15 { return e; } } + + static void changeColor(Clipboard clipboard, Color color) throws WorldEditException { + for (int x = 0; x < clipboard.getDimensions().getX(); x++) { + for (int y = 0; y < clipboard.getDimensions().getX(); y++) { + for (int z = 0; z < clipboard.getDimensions().getX(); z++) { + BlockVector3 blockPointer = clipboard.getMinimumPoint().add(x, y, z); + clipboard.setBlock(blockPointer, mapColor(clipboard.getFullBlock(blockPointer).getBlockType(), color).getDefaultState().toBaseBlock()); + } + } + } + } + + private static BlockType mapColor(BlockType original, Color color) { + if (original != BlockTypes.YELLOW_CONCRETE && original != BlockTypes.YELLOW_STAINED_GLASS) { + return original; + } + + switch (color) { + case WHITE: + if (original == BlockTypes.YELLOW_CONCRETE) { + return BlockTypes.WHITE_CONCRETE; + } else { + return BlockTypes.WHITE_STAINED_GLASS; + } + case ORANGE: + if (original == BlockTypes.YELLOW_CONCRETE) { + return BlockTypes.ORANGE_CONCRETE; + } else { + return BlockTypes.ORANGE_STAINED_GLASS; + } + case MAGENTA: + if (original == BlockTypes.YELLOW_CONCRETE) { + return BlockTypes.MAGENTA_CONCRETE; + } else { + return BlockTypes.MAGENTA_STAINED_GLASS; + } + case LIGHT_BLUE: + if (original == BlockTypes.YELLOW_CONCRETE) { + return BlockTypes.LIGHT_BLUE_CONCRETE; + } else { + return BlockTypes.LIGHT_BLUE_STAINED_GLASS; + } + case LIME: + if (original == BlockTypes.YELLOW_CONCRETE) { + return BlockTypes.LIME_CONCRETE; + } else { + return BlockTypes.LIME_STAINED_GLASS; + } + case PINK: + if (original == BlockTypes.YELLOW_CONCRETE) { + return BlockTypes.PINK_CONCRETE; + } else { + return BlockTypes.PINK_STAINED_GLASS; + } + case GRAY: + if (original == BlockTypes.YELLOW_CONCRETE) { + return BlockTypes.GRAY_CONCRETE; + } else { + return BlockTypes.GRAY_STAINED_GLASS; + } + case LIGHT_GRAY: + if (original == BlockTypes.YELLOW_CONCRETE) { + return BlockTypes.LIGHT_GRAY_CONCRETE; + } else { + return BlockTypes.LIGHT_GRAY_STAINED_GLASS; + } + case CYAN: + if (original == BlockTypes.YELLOW_CONCRETE) { + return BlockTypes.CYAN_CONCRETE; + } else { + return BlockTypes.CYAN_STAINED_GLASS; + } + case PURPLE: + if (original == BlockTypes.YELLOW_CONCRETE) { + return BlockTypes.PURPLE_CONCRETE; + } else { + return BlockTypes.PURPLE_STAINED_GLASS; + } + case BLUE: + if (original == BlockTypes.YELLOW_CONCRETE) { + return BlockTypes.BLUE_CONCRETE; + } else { + return BlockTypes.BLUE_STAINED_GLASS; + } + case BROWN: + if (original == BlockTypes.YELLOW_CONCRETE) { + return BlockTypes.BROWN_CONCRETE; + } else { + return BlockTypes.BROWN_STAINED_GLASS; + } + case GREEN: + if (original == BlockTypes.YELLOW_CONCRETE) { + return BlockTypes.GREEN_CONCRETE; + } else { + return BlockTypes.GREEN_STAINED_GLASS; + } + case RED: + if (original == BlockTypes.YELLOW_CONCRETE) { + return BlockTypes.RED_CONCRETE; + } else { + return BlockTypes.RED_STAINED_GLASS; + } + case BLACK: + if (original == BlockTypes.YELLOW_CONCRETE) { + return BlockTypes.BLACK_CONCRETE; + } else { + return BlockTypes.BLACK_STAINED_GLASS; + } + case YELLOW: + default: + return original; + } + } } From cdacf05871461008ae62b3143e7a5cddfeb6f9d5 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 5 Apr 2021 12:32:36 +0200 Subject: [PATCH 4/7] Add CommandColor Fix Region_15 region size --- .../steamwar/bausystem/world/Region_15.java | 14 ++++-- .../src/de/steamwar/bausystem/BauSystem.java | 7 ++- .../bausystem/commands/CommandColor.java | 50 ++++++++++++++++++- .../de/steamwar/bausystem/world/Region.java | 33 +++++++----- 4 files changed, 83 insertions(+), 21 deletions(-) diff --git a/BauSystem_15/src/de/steamwar/bausystem/world/Region_15.java b/BauSystem_15/src/de/steamwar/bausystem/world/Region_15.java index 8b29993..8751c47 100644 --- a/BauSystem_15/src/de/steamwar/bausystem/world/Region_15.java +++ b/BauSystem_15/src/de/steamwar/bausystem/world/Region_15.java @@ -44,7 +44,7 @@ class Region_15 { private Region_15() { } - static EditSession paste(File file, int x, int y, int z, boolean rotate, boolean ignoreAir) { + static EditSession paste(File file, int x, int y, int z, boolean rotate, boolean ignoreAir, Color color) { Clipboard clipboard; try (ClipboardReader reader = Objects.requireNonNull(ClipboardFormats.findByFile(file)).getReader(new FileInputStream(file))) { clipboard = reader.read(); @@ -52,11 +52,13 @@ class Region_15 { throw new SecurityException("Bausystem schematic not found", e); } - return paste(clipboard, x, y, z, rotate, ignoreAir); + return paste(clipboard, x, y, z, rotate, ignoreAir, color); } - static EditSession paste(Clipboard clipboard, int x, int y, int z, boolean rotate, boolean ignoreAir) { + static EditSession paste(Clipboard clipboard, int x, int y, int z, boolean rotate, boolean ignoreAir, Color color) { try (EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(new BukkitWorld(Bukkit.getWorlds().get(0)), -1)) { + changeColor(clipboard, color); + ClipboardHolder ch = new ClipboardHolder(clipboard); BlockVector3 dimensions = clipboard.getDimensions(); BlockVector3 v = BlockVector3.at(x, y, z); @@ -70,13 +72,15 @@ class Region_15 { Operations.completeBlindly(ch.createPaste(e).to(v).ignoreAirBlocks(ignoreAir).build()); return e; + } catch (WorldEditException e) { + throw new SecurityException(e.getMessage(), e); } } static void changeColor(Clipboard clipboard, Color color) throws WorldEditException { for (int x = 0; x < clipboard.getDimensions().getX(); x++) { - for (int y = 0; y < clipboard.getDimensions().getX(); y++) { - for (int z = 0; z < clipboard.getDimensions().getX(); z++) { + for (int y = 0; y < clipboard.getDimensions().getY(); y++) { + for (int z = 0; z < clipboard.getDimensions().getZ(); z++) { BlockVector3 blockPointer = clipboard.getMinimumPoint().add(x, y, z); clipboard.setBlock(blockPointer, mapColor(clipboard.getFullBlock(blockPointer).getBlockType(), color).getDefaultState().toBaseBlock()); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index a3207a2..4dc7e86 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -94,6 +94,12 @@ public class BauSystem extends JavaPlugin implements Listener { new CommandWorldSpawn(); new CommandRegion(); + VersionedRunnable.call(new VersionedRunnable(() -> { + if (Region.buildAreaEnabled()) { + new CommandColor(); + } + }, 15)); + Bukkit.getPluginManager().registerEvents(this, this); Bukkit.getPluginManager().registerEvents(new RegionListener(), this); Bukkit.getPluginManager().registerEvents(new ScriptListener(), this); @@ -159,7 +165,6 @@ public class BauSystem extends JavaPlugin implements Listener { autoShutdown.cancel(); } CommandTPSLimiter.setTPS(20.0); - System.out.println("SAVING colors"); Region.saveColors(); autoShutdown = Bukkit.getScheduler().runTaskTimer(this, new Runnable() { int count = 0; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandColor.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandColor.java index c0a124b..09b25d7 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandColor.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandColor.java @@ -1,10 +1,56 @@ package de.steamwar.bausystem.commands; -import com.sk89q.worldedit.extent.clipboard.Clipboard; +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.world.Color; +import de.steamwar.bausystem.world.Region; +import de.steamwar.command.SWCommand; +import org.bukkit.entity.Player; -public class CommandColor { +public class CommandColor extends SWCommand { + public CommandColor() { + super("color"); + } + @Register(help = true) + public void genericHelp(Player p, String... args) { + p.sendMessage("§8/§ecolor §8[§7Color§8] §8- §7Setze die Farbe der Region"); + p.sendMessage("§8/§ecolor §8[§7Color§8] §8[§7Type§8] §8- §7Setze die Farbe der Region oder Global"); + } + + @Register + public void genericColor(Player p, Color color) { + genericColorSet(p, color, ColorizationType.LOCAL); + } + + @Register + public void genericColorSet(Player p, Color color, ColorizationType colorizationType) { + if (!permissionCheck(p)) { + return; + } + if (colorizationType == ColorizationType.GLOBAL) { + Region.setGlobalColor(color); + p.sendMessage(BauSystem.PREFIX + "Alle Regions farben auf §e" + color.name().toLowerCase() + "§7 gesetzt"); + return; + } + Region region = Region.getRegion(p.getLocation()); + region.setColor(color); + p.sendMessage(BauSystem.PREFIX + "Regions farben auf §e" + color.name().toLowerCase() + "§7 gesetzt"); + } + + private boolean permissionCheck(Player p) { + if (!BauSystem.getOwner().equals(p.getUniqueId())) { + p.sendMessage(BauSystem.PREFIX + "§cDies ist nicht deine Welt!"); + return false; + } else { + return true; + } + } + + public enum ColorizationType { + LOCAL, + GLOBAL + } } \ No newline at end of file diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java b/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java index de4dd49..b42b11d 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java @@ -31,6 +31,7 @@ import org.bukkit.Location; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; import java.io.*; import java.util.*; @@ -88,6 +89,12 @@ public class Region { } return GlobalRegion.getInstance(); } + + public static void setGlobalColor(Color color) { + for (Region region : regions) { + region.setColor(color); + } + } public static void saveColors() { File colorsFile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "colors.json"); @@ -231,7 +238,7 @@ public class Region { public void reset(Schematic schem, boolean ignoreAir) throws IOException, NoClipboardException { initSessions(); - undosessions.push(prototype.reset(this, schem, ignoreAir)); + undosessions.push(prototype.reset(this, schem, ignoreAir, color)); } public boolean hasTestblock() { @@ -240,7 +247,7 @@ public class Region { public void resetTestblock(Schematic schem) throws IOException, NoClipboardException { initSessions(); - undosessions.push(prototype.resetTestblock(this, schem)); + undosessions.push(prototype.resetTestblock(this, schem, color)); } public boolean hasProtection() { @@ -418,14 +425,14 @@ public class Region { inRange(l.getZ(), region.minZ + offsetZ - extensionNegativeZ, sizeZ + extensionNegativeZ + extensionPositiveZ); } - public EditSession reset(Region region, Schematic schem, boolean ignoreAir) throws IOException, NoClipboardException { + public EditSession reset(Region region, Schematic schem, boolean ignoreAir, Color color) throws IOException, NoClipboardException { int x = region.minX + offsetX + sizeX / 2; int y = region.minY + offsetY; int z = region.minZ + offsetZ + sizeZ / 2; if (schem == null) - return paste(new File(schematic), x, y, z, rotate, ignoreAir); + return paste(new File(schematic), x, y, z, rotate, ignoreAir, color); else - return paste(schem.load(), x, y, z, rotate, ignoreAir); + return paste(schem.load(), x, y, z, rotate, ignoreAir, color); } public boolean hasProtection() { @@ -437,31 +444,31 @@ public class Region { int y = region.minY + testblock.offsetY - 1; int z = region.minZ + offsetZ + sizeZ / 2; if (schem == null) - return paste(new File(protectSchematic), x, y, z, rotate, false); + return paste(new File(protectSchematic), x, y, z, rotate, false, Color.YELLOW); else - return paste(schem.load(), x, y, z, rotate, false); + return paste(schem.load(), x, y, z, rotate, false, Color.YELLOW); } public boolean hasTestblock() { return testblock != null; } - public EditSession resetTestblock(Region region, Schematic schem) throws IOException, NoClipboardException { - return testblock.reset(region, schem, false); + public EditSession resetTestblock(Region region, Schematic schem, Color color) throws IOException, NoClipboardException { + return testblock.reset(region, schem, false, color); } private static boolean inRange(double l, int min, int size) { return min <= l && l < min + size; } - private static EditSession paste(File file, int x, int y, int z, boolean rotate, boolean ignoreAir) { //Type of protect + private static EditSession paste(File file, int x, int y, int z, boolean rotate, boolean ignoreAir, Color color) { //Type of protect return (EditSession) VersionedCallable.call(new VersionedCallable(() -> Region_12.paste(file, x, y, z, rotate, ignoreAir), 8), - new VersionedCallable(() -> Region_15.paste(file, x, y, z, rotate, ignoreAir), 15)); + new VersionedCallable(() -> Region_15.paste(file, x, y, z, rotate, ignoreAir, color), 15)); } - private static EditSession paste(Clipboard clipboard, int x, int y, int z, boolean rotate, boolean ignoreAir) { + private static EditSession paste(Clipboard clipboard, int x, int y, int z, boolean rotate, boolean ignoreAir, Color color) { return (EditSession) VersionedCallable.call(new VersionedCallable(() -> Region_12.paste(clipboard, x, y, z, rotate, ignoreAir), 8), - new VersionedCallable(() -> Region_15.paste(clipboard, x, y, z, rotate, ignoreAir), 15)); + new VersionedCallable(() -> Region_15.paste(clipboard, x, y, z, rotate, ignoreAir, color), 15)); } } } From 6491e8a596108565b4ac069c28b9a91471051938 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 5 Apr 2021 12:46:29 +0200 Subject: [PATCH 5/7] Optimize imports in Region --- .../src/de/steamwar/bausystem/world/Region.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java b/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java index b42b11d..a0170bb 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java @@ -19,7 +19,10 @@ package de.steamwar.bausystem.world; -import com.google.gson.*; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSyntaxException; import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.extent.clipboard.Clipboard; import de.steamwar.bausystem.commands.CommandTNT.TNTMode; @@ -31,9 +34,11 @@ import org.bukkit.Location; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; -import java.io.*; +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; import java.util.*; import java.util.function.Consumer; import java.util.logging.Level; From 7c075b18d4ee2c503b45fc05a0d4d00fbc1377f6 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 5 Apr 2021 12:48:47 +0200 Subject: [PATCH 6/7] Optimize imports in Region --- BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index 4dc7e86..8e3570d 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -116,6 +116,11 @@ public class BauSystem extends JavaPlugin implements Listener { TPSUtils.init(); } + @Override + public void onDisable() { + Region.saveColors(); + } + public static BauSystem getPlugin() { return plugin; } @@ -165,7 +170,6 @@ public class BauSystem extends JavaPlugin implements Listener { autoShutdown.cancel(); } CommandTPSLimiter.setTPS(20.0); - Region.saveColors(); autoShutdown = Bukkit.getScheduler().runTaskTimer(this, new Runnable() { int count = 0; From bc93d96787445b982e38d9344776bb3f5c1f466b Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 5 Apr 2021 13:07:10 +0200 Subject: [PATCH 7/7] Add Region persistence --- .../src/de/steamwar/bausystem/BauSystem.java | 2 +- .../de/steamwar/bausystem/world/Region.java | 56 +++++++++++++------ 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index 8e3570d..85430f9 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -118,7 +118,7 @@ public class BauSystem extends JavaPlugin implements Listener { @Override public void onDisable() { - Region.saveColors(); + Region.save(); } public static BauSystem getPlugin() { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java b/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java index a0170bb..76ee3da 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/Region.java @@ -48,13 +48,13 @@ public class Region { private static final List regions = new ArrayList<>(); private static boolean buildArea = false; private static boolean extensionArea = false; - private static JsonObject colors = new JsonObject(); + private static JsonObject regionsObject = new JsonObject(); static { - File colorsFile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "colors.json"); - if (colorsFile.exists()) { + File regionsFile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "regions.json"); + if (regionsFile.exists()) { try { - colors = new JsonParser().parse(new FileReader(colorsFile)).getAsJsonObject(); + regionsObject = new JsonParser().parse(new FileReader(regionsFile)).getAsJsonObject(); } catch (JsonSyntaxException | IOException e) { Bukkit.getLogger().log(Level.WARNING, "Item JSON error"); } @@ -101,8 +101,8 @@ public class Region { } } - public static void saveColors() { - File colorsFile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "colors.json"); + public static void save() { + File colorsFile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "regions.json"); if (!colorsFile.exists()) { try { colorsFile.createNewFile(); @@ -112,7 +112,7 @@ public class Region { } } try (FileOutputStream fileOutputStream = new FileOutputStream(colorsFile)) { - fileOutputStream.write(colors.toString().getBytes()); + fileOutputStream.write(regionsObject.toString().getBytes()); } catch (IOException e) { e.printStackTrace(); // Ignored @@ -129,6 +129,8 @@ public class Region { private SizedStack undosessions; private SizedStack redosessions; + private JsonObject regionOptions = new JsonObject(); + private TNTMode tntMode = Region.buildAreaEnabled() ? TNTMode.ONLY_TB : TNTMode.OFF; private boolean freeze = false; private boolean fire = false; @@ -137,16 +139,35 @@ public class Region { private Region(ConfigurationSection config) { name = config.getName(); - if (colors.has(name)) { - String colorName = colors.getAsJsonPrimitive(name).getAsString(); - try { - color = Color.valueOf(colorName); - } catch (Exception e) { - // Ignored + if (regionsObject.has(name)) { + regionOptions = regionsObject.getAsJsonObject(name); + if (regionOptions.has("tnt")) { + String tntName = regionsObject.getAsJsonObject(name).getAsJsonPrimitive("tnt").getAsString(); + try { + tntMode = TNTMode.valueOf(tntName); + } catch (Exception e) { + // Ignored + } } - if (color == null) { - color = Color.YELLOW; + + if (regionOptions.has("fire")) { + fire = regionOptions.getAsJsonPrimitive("fire").getAsBoolean(); } + + if (regionOptions.has("freeze")) { + freeze = regionOptions.getAsJsonPrimitive("freeze").getAsBoolean(); + } + + if (regionOptions.has("color")) { + String colorName = regionOptions.getAsJsonPrimitive("color").getAsString(); + try { + color = Color.valueOf(colorName); + } catch (Exception e) { + // Ignored + } + } + } else { + regionsObject.add(name, regionOptions); } minX = config.getInt("minX"); @@ -174,7 +195,7 @@ public class Region { public void setColor(Color color) { this.color = color; - colors.add(name, new JsonPrimitive(color.name())); + regionOptions.add("color", new JsonPrimitive(color.name())); } private void setLinkedRegion(Consumer regionConsumer) { @@ -201,6 +222,7 @@ public class Region { public void setTntMode(TNTMode tntMode) { this.tntMode = tntMode; setLinkedRegion(region -> region.tntMode = tntMode); + regionOptions.add("tnt", new JsonPrimitive(tntMode.name())); } public boolean isFreeze() { @@ -210,6 +232,7 @@ public class Region { public void setFreeze(boolean freeze) { this.freeze = freeze; setLinkedRegion(region -> region.freeze = freeze); + regionOptions.add("freeze", new JsonPrimitive(freeze)); } public boolean isFire() { @@ -219,6 +242,7 @@ public class Region { public void setFire(boolean fire) { this.fire = fire; setLinkedRegion(region -> region.fire = fire); + regionOptions.add("fire", new JsonPrimitive(fire)); } public boolean inRegion(Location l) {