From 315750475a7108428a68898e56dc08f896837873 Mon Sep 17 00:00:00 2001
From: lars <larslukasneuhaus@gmx.de>
Date: Sat, 20 Jul 2024 15:23:36 +0200
Subject: [PATCH] added config file

---
 .../minecraft/pixelblocks/PixelBlocks.java    | 20 +++++++-
 .../listeners/BlockBreakListener.java         |  2 +-
 .../listeners/BlockPlaceListener.java         |  2 +-
 .../pixelblocks/pixelblock/PixelBlock.java    | 50 ++++++++++---------
 4 files changed, 47 insertions(+), 27 deletions(-)

diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlocks.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlocks.java
index 693d96f..3a57b10 100644
--- a/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlocks.java
+++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlocks.java
@@ -3,9 +3,11 @@ package eu.mhsl.minecraft.pixelblocks;
 import eu.mhsl.minecraft.pixelblocks.commands.CreatePixelBlockCommand;
 import eu.mhsl.minecraft.pixelblocks.commands.ExitWorldCommand;
 import eu.mhsl.minecraft.pixelblocks.listeners.*;
+import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
 import org.bukkit.Bukkit;
 import org.bukkit.Material;
 import org.bukkit.NamespacedKey;
+import org.bukkit.configuration.file.FileConfiguration;
 import org.bukkit.event.Listener;
 import org.bukkit.inventory.ItemStack;
 import org.bukkit.inventory.ShapedRecipe;
@@ -18,13 +20,29 @@ import java.util.Objects;
 public final class PixelBlocks extends JavaPlugin {
     public static PixelBlocks plugin;
     public static DataBase dataBase;
-    public static String pathSeparator = File.separator;
+    public static String pathSeparator;
+    public FileConfiguration config;
 
     @Override
     public void onEnable() {
         PixelBlocks.plugin = this;
         this.getLogger().info("PixelBlocks Plugin was enabled.");
 
+        pathSeparator = File.separator;
+
+        config = this.getConfig();
+        config.addDefault("pixelsPerBlock", 16);
+        config.addDefault("liveUpdate", true);
+        config.addDefault("worldGrassBorderWidth", 5);
+        config.addDefault("hitboxOffset", 0.005);
+        config.options().copyDefaults(true);
+        saveConfig();
+
+        PixelBlock.pixelsPerBlock = config.getInt("pixelsPerBlock");
+        PixelBlock.liveUpdate = config.getBoolean("liveUpdate");
+        PixelBlock.worldGrassBorderWidth = config.getInt("worldGrassBorderWidth");
+        PixelBlock.hitboxOffset = (float) config.getDouble("hitboxOffset");
+
         dataBase = new DataBase("jdbc:sqlite:pixelblocks.db");
         dataBase.loadPixelBlocks();
 
diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockBreakListener.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockBreakListener.java
index 7bb0120..bfab9a0 100644
--- a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockBreakListener.java
+++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockBreakListener.java
@@ -12,7 +12,7 @@ public class BlockBreakListener implements Listener {
             PixelBlock pixelBlock = PixelBlock.getPixelBlockFromWorld(event.getBlock().getLocation().getWorld());
 
             assert pixelBlock != null;
-            pixelBlock.handleBlockBreak(event, true);
+            pixelBlock.handleBlockBreak(event);
         }
     }
 }
diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockPlaceListener.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockPlaceListener.java
index 989ca09..4ce2ddc 100644
--- a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockPlaceListener.java
+++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockPlaceListener.java
@@ -46,7 +46,7 @@ public class BlockPlaceListener implements Listener {
                 PixelBlock pixelBlock = PixelBlock.getPixelBlockFromWorld(event.getBlock().getLocation().getWorld());
 
                 assert pixelBlock != null;
-                pixelBlock.handleBlockPlace(event, true);
+                pixelBlock.handleBlockPlace(event);
             }
         }
     }
diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlock.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlock.java
index c0b2e3e..b27c6f5 100644
--- a/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlock.java
+++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlock.java
@@ -22,6 +22,7 @@ public class PixelBlock {
     public static float hitboxOffset = 0.005F;
     public static int worldGrassBorderWidth = 5;
     public static int pixelsPerBlock = 16;
+    public static boolean liveUpdate = true;
 
     public Location pixelBlockLocation;
     public ArrayList<Pixel> pixels = new ArrayList<>();
@@ -78,9 +79,9 @@ public class PixelBlock {
 
     void createPixelWorld() {
         File file = new File(getWorldPathFromPixelblock(this));
+        final WorldCreator worldCreator = new WorldCreator(getWorldPathFromPixelblock(this));
 
         if(!file.exists() || !file.isDirectory()) {
-            final WorldCreator worldCreator = new WorldCreator(getWorldPathFromPixelblock(this));
             worldCreator.type(WorldType.FLAT);
             worldCreator.generator(new EmptyChunkGenerator());
             World newWorld = Bukkit.createWorld(worldCreator);
@@ -97,22 +98,23 @@ public class PixelBlock {
             Location grassStartLocation = borderStartLocation.clone().subtract(worldGrassBorderWidth, 0, worldGrassBorderWidth);
 
             Bukkit.getScheduler().runTask(plugin, () -> {
-                for (int x = 0; x < 18 + 2 * worldGrassBorderWidth; x++) {
-                    for (int z = 0; z < 18 + 2 * worldGrassBorderWidth; z++) {
+                for (int x = 0; x < (pixelsPerBlock+2) + 2 * worldGrassBorderWidth; x++) {
+                    for (int z = 0; z < (pixelsPerBlock+2) + 2 * worldGrassBorderWidth; z++) {
                         grassStartLocation.clone().add(x, 0, z).getBlock().setType(Material.GRASS_BLOCK);
                     }
                 }
-                for (int x = 0; x < 18 + 2 * worldGrassBorderWidth; x++) {
-                    for (int z = 0; z < 18 + 2 * worldGrassBorderWidth; z++) {
+                for (int x = 0; x < (pixelsPerBlock+2) + 2 * worldGrassBorderWidth; x++) {
+                    for (int z = 0; z < (pixelsPerBlock+2) + 2 * worldGrassBorderWidth; z++) {
                         grassStartLocation.clone().add(x, -1, z).getBlock().setType(Material.DIRT);
                     }
                 }
-                for (int x = 0; x < 18; x++) {
-                    for (int z = 0; z < 18; z++) {
+                for (int x = 0; x < (pixelsPerBlock+2); x++) {
+                    for (int z = 0; z < (pixelsPerBlock+2); z++) {
                         Location currentLocation = borderStartLocation.clone().add(x, 0, z);
+
                         if (currentLocation.x() == borderStartLocation.x() || currentLocation.z() == borderStartLocation.z()) {
                             currentLocation.getBlock().setType(Material.RED_CONCRETE);
-                        } else if (currentLocation.x() == borderStartLocation.x() + 17 || currentLocation.z() == borderStartLocation.z() + 17) {
+                        } else if (currentLocation.x() == borderStartLocation.x() + (pixelsPerBlock+1) || currentLocation.z() == borderStartLocation.z() + (pixelsPerBlock+1)) {
                             currentLocation.getBlock().setType(Material.RED_CONCRETE);
                         }
                     }
@@ -130,7 +132,7 @@ public class PixelBlock {
                 }
             });
         } else {
-            new WorldCreator(getWorldPathFromPixelblock(this)).createWorld();
+            worldCreator.createWorld();
         }
     }
 
@@ -181,21 +183,21 @@ public class PixelBlock {
         this.pixelBlockLocation.getWorld().dropItem(this.pixelBlockLocation, this.getAsItem());
     }
 
-    public void handleBlockBreak(BlockBreakEvent event, boolean liveUpdate) {
+    public void handleBlockBreak(BlockBreakEvent event) {
         Location blockLocation = event.getBlock().getLocation();
         Location spawnLocation = blockLocation.getWorld().getSpawnLocation();
 
         if(blockLocation.x() < spawnLocation.x() || blockLocation.z() < spawnLocation.z() || blockLocation.y() < spawnLocation.y()) {
             event.setCancelled(true);
             return;
-        } else if(blockLocation.x() > spawnLocation.x()+15 || blockLocation.z() > spawnLocation.z()+15 || blockLocation.y() > spawnLocation.y()+15) {
+        } else if(blockLocation.x() > spawnLocation.x()+(pixelsPerBlock-1) || blockLocation.z() > spawnLocation.z()+(pixelsPerBlock-1) || blockLocation.y() > spawnLocation.y()+(pixelsPerBlock-1)) {
             event.setCancelled(true);
             return;
         }
 
         Bukkit.getScheduler().runTask(plugin, () -> {
             if(liveUpdate) {
-                Location relativeLocation = blockLocation.subtract(spawnLocation.x(), spawnLocation.y(), spawnLocation.z());
+                Location relativeLocation = blockLocation.subtract(spawnLocation.getX(), spawnLocation.getY(), spawnLocation.getZ());
                 relativeLocation.setWorld(this.pixelBlockLocation.getWorld());
                 List<Pixel> pixels = this.pixels.stream().filter(pixel -> pixel.relativeLocation.equals(relativeLocation)).toList();
                 if(!pixels.isEmpty()) {
@@ -207,21 +209,21 @@ public class PixelBlock {
         });
     }
 
-    public void handleBlockPlace(BlockPlaceEvent event, boolean liveUpdate) {
+    public void handleBlockPlace(BlockPlaceEvent event) {
         Location blockLocation = event.getBlock().getLocation();
         Location spawnLocation = blockLocation.getWorld().getSpawnLocation();
 
         if(blockLocation.x() < spawnLocation.x() || blockLocation.z() < spawnLocation.z() || blockLocation.y() < spawnLocation.y()) {
             event.setCancelled(true);
             return;
-        } else if(blockLocation.x() > spawnLocation.x()+15 || blockLocation.z() > spawnLocation.z()+15 || blockLocation.y() > spawnLocation.y()+15) {
+        } else if(blockLocation.x() > spawnLocation.x()+(pixelsPerBlock-1) || blockLocation.z() > spawnLocation.z()+(pixelsPerBlock-1) || blockLocation.y() > spawnLocation.y()+(pixelsPerBlock-1)) {
             event.setCancelled(true);
             return;
         }
 
         Bukkit.getScheduler().runTask(PixelBlocks.plugin, () -> {
             if(liveUpdate) {
-                Location relativeLocation = blockLocation.subtract(spawnLocation.x(), spawnLocation.y(), spawnLocation.z());
+                Location relativeLocation = blockLocation.subtract(spawnLocation.getX(), spawnLocation.getY(), spawnLocation.getZ());
                 relativeLocation.setWorld(this.pixelBlockLocation.getWorld());
 
                 Pixel newPixel = new Pixel(relativeLocation, event.getBlock().getBlockData(), ((double) 1 /pixelsPerBlock), 0);
@@ -276,18 +278,18 @@ public class PixelBlock {
                     .orElseThrow().relativeLocation.getZ();
 
             Location spawnLocation = pixelBlockLocation.clone().add(
-                    ((startingX+endingX+1)/2)/16,
-                    (startingY/16)-hitboxOffset,
-                    ((startingZ+endingZ+1)/2)/16
+                    ((startingX+endingX+1)/2)/pixelsPerBlock,
+                    (startingY/pixelsPerBlock)-hitboxOffset,
+                    ((startingZ+endingZ+1)/2)/pixelsPerBlock
                 );
 
-            float height = (float) (endingY-startingY+1)/16 + 2*hitboxOffset;
+            float height = (float) (endingY-startingY+1)/pixelsPerBlock + 2*hitboxOffset;
 
             float width;
             if((endingX-startingX) > (endingZ-startingZ)) {
-                width = (float) (endingX-startingX+1)/16 + 2*hitboxOffset;
+                width = (float) (endingX-startingX+1)/pixelsPerBlock + 2*hitboxOffset;
             } else {
-                width = (float) (endingZ-startingZ+1)/16 + 2*hitboxOffset;
+                width = (float) (endingZ-startingZ+1)/pixelsPerBlock + 2*hitboxOffset;
             }
 
             if(spawnLocation.getX()+width/2 > this.pixelBlockLocation.getX()+1) {
@@ -318,9 +320,9 @@ public class PixelBlock {
         Bukkit.getScheduler().runTask(plugin, () -> {
             this.clearEntities(true);
 
-            for (int x = 0; x < 16; x++) {
-                for (int y = 0; y < 16; y++) {
-                    for (int z = 0; z < 16; z++) {
+            for (int x = 0; x < pixelsPerBlock; x++) {
+                for (int y = 0; y < pixelsPerBlock; y++) {
+                    for (int z = 0; z < pixelsPerBlock; z++) {
                         Location relativeLocation = new Location(pixelBlockLocation.getWorld(), x, y, z);
                         Location blockLocation = Objects.requireNonNull(Bukkit
                                 .getWorld(getWorldPathFromPixelblock(this)))