From e5e1f39989be62df8832295cb4cca1d103bc4518 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20M=C3=BCller?= Date: Mon, 22 Jul 2024 19:14:47 +0200 Subject: [PATCH] wip: code cleanup --- .idea/modules.xml | 1 + .idea/modules/PixelBlocks.main.iml | 4 + .idea/modules/projects.PixelBlocks.main.iml | 14 + .../pixelblocks/PixelBlockConfiguration.java | 44 ++ ...{DataBase.java => PixelBlockDatabase.java} | 67 ++- .../minecraft/pixelblocks/PixelBlockItem.java | 50 ++ .../minecraft/pixelblocks/PixelBlocks.java | 94 ---- .../pixelblocks/PixelBlocksPlugin.java | 68 +++ .../chunkGenerators/EmptyChunkGenerator.java | 2 +- .../commands/CreatePixelBlockCommand.java | 4 +- .../commands/ExitWorldCommand.java | 5 +- .../listeners/BlockBreakListener.java | 36 +- .../listeners/BlockExplodeListener.java | 12 - .../listeners/BlockPlaceListener.java | 16 +- .../listeners/CommonEventHandlers.java | 4 +- .../listeners/CreatureSpawnListener.java | 12 - .../listeners/EntityDamageListener.java | 12 - .../listeners/EntityExplodeListener.java | 12 - .../listeners/InPixelWorldCancelListener.java | 42 ++ .../listeners/PlayerChangeWorldListener.java | 2 +- .../listeners/PlayerDropItemListener.java | 12 - .../listeners/PlayerInteractListener.java | 13 +- .../listeners/PlayerMoveListener.java | 6 +- .../listeners/PlayerPortalListener.java | 12 - .../pixelblocks/pixelblock/Direction.java | 8 - .../pixelblocks/pixelblock/Pixel.java | 44 +- .../pixelblocks/pixelblock/PixelBlock.java | 462 +++++------------- .../pixelblock/PixelBlockInteraction.java | 5 + .../pixelblock/PixelBlockWorld.java | 124 +++++ .../pixelblocks/utils/Direction.java | 26 + .../pixelblocks/utils/MinMaxUtil.java | 14 + src/main/resources/plugin.yml | 2 +- 32 files changed, 623 insertions(+), 606 deletions(-) create mode 100644 .idea/modules/projects.PixelBlocks.main.iml create mode 100644 src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlockConfiguration.java rename src/main/java/eu/mhsl/minecraft/pixelblocks/{DataBase.java => PixelBlockDatabase.java} (81%) create mode 100644 src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlockItem.java delete mode 100644 src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlocks.java create mode 100644 src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlocksPlugin.java delete mode 100644 src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockExplodeListener.java delete mode 100644 src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/CreatureSpawnListener.java delete mode 100644 src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/EntityDamageListener.java delete mode 100644 src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/EntityExplodeListener.java create mode 100644 src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/InPixelWorldCancelListener.java delete mode 100644 src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerDropItemListener.java delete mode 100644 src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerPortalListener.java delete mode 100644 src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/Direction.java create mode 100644 src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlockInteraction.java create mode 100644 src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlockWorld.java create mode 100644 src/main/java/eu/mhsl/minecraft/pixelblocks/utils/Direction.java create mode 100644 src/main/java/eu/mhsl/minecraft/pixelblocks/utils/MinMaxUtil.java diff --git a/.idea/modules.xml b/.idea/modules.xml index 87190ce..242f4dc 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -4,6 +4,7 @@ + \ No newline at end of file diff --git a/.idea/modules/PixelBlocks.main.iml b/.idea/modules/PixelBlocks.main.iml index bbeeb3e..746f1fd 100644 --- a/.idea/modules/PixelBlocks.main.iml +++ b/.idea/modules/PixelBlocks.main.iml @@ -11,4 +11,8 @@ + + + + \ No newline at end of file diff --git a/.idea/modules/projects.PixelBlocks.main.iml b/.idea/modules/projects.PixelBlocks.main.iml new file mode 100644 index 0000000..bbeeb3e --- /dev/null +++ b/.idea/modules/projects.PixelBlocks.main.iml @@ -0,0 +1,14 @@ + + + + + + + PAPER + ADVENTURE + + 1 + + + + \ No newline at end of file diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlockConfiguration.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlockConfiguration.java new file mode 100644 index 0000000..fc6466e --- /dev/null +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlockConfiguration.java @@ -0,0 +1,44 @@ +package eu.mhsl.minecraft.pixelblocks; + +import org.bukkit.configuration.file.FileConfiguration; +import org.jetbrains.annotations.NotNull; + +public record PixelBlockConfiguration( + int pixelsPerBlock, + boolean liveUpdate, + int worldGrassBorderWidth, + double hitboxOffset, + boolean deleteOnBreak, // TODO does no longer exists + boolean onlyBreakableByOwner, + boolean onlyEditableByOwner +) { + public static void setDefaults(FileConfiguration config) { + config.addDefault(Keys.PixelsPerBlock.key, 16); + config.addDefault(Keys.LiveUpdate.key, true); + config.addDefault(Keys.WorldGrassBorderWidth.key, 5); + config.addDefault(Keys.HitboxOffset.key, 0.005); + config.addDefault(Keys.DeleteOnBreak.key, false); + config.addDefault(Keys.OnlyBreakableByOwners.key, false); + config.addDefault(Keys.OnlyEditableByOwners.key, true); + config.options().copyDefaults(true); + } + + public enum Keys { + PixelsPerBlock("pixelsPerBlock"), + LiveUpdate("liveUpdate"), + WorldGrassBorderWidth("worldGrassBorderWidth"), + HitboxOffset("hitboxOffset"), + DeleteOnBreak("deleteOnBreak"), + OnlyBreakableByOwners("onlyBreakableByOwners"), + OnlyEditableByOwners("onlyEditableByOwners"); + + private final String key; + Keys(@NotNull String key) { + this.key = key; + } + + public @NotNull String getKey() { + return key; + } + } +} diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/DataBase.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlockDatabase.java similarity index 81% rename from src/main/java/eu/mhsl/minecraft/pixelblocks/DataBase.java rename to src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlockDatabase.java index e9a4c5b..f0f3907 100644 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/DataBase.java +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlockDatabase.java @@ -1,6 +1,6 @@ package eu.mhsl.minecraft.pixelblocks; -import eu.mhsl.minecraft.pixelblocks.pixelblock.Direction; +import eu.mhsl.minecraft.pixelblocks.utils.Direction; import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -12,29 +12,27 @@ import java.util.ArrayList; import java.util.List; import java.util.UUID; -import static eu.mhsl.minecraft.pixelblocks.PixelBlocks.plugin; +public class PixelBlockDatabase { + public Connection db; -public class DataBase { - public static String url; - - public DataBase(String url) { - DataBase.url = url; - } - - public Connection getConnection() throws SQLException { - return DriverManager.getConnection(url); + public PixelBlockDatabase(String url) { + try { + this.db = DriverManager.getConnection(url); + } catch (SQLException e) { + throw new RuntimeException("Failed to load Database", e); + } } public Statement getStatement() throws SQLException { - return getConnection().createStatement(); + return this.db.createStatement(); } public void removePixelBlock(PixelBlock pixelBlock) { - Bukkit.getScheduler().runTask(plugin, () -> { + Bukkit.getScheduler().runTask(PixelBlocksPlugin.plugin, () -> { - try (var conn = getConnection()) { - PreparedStatement prep = conn.prepareStatement("DELETE FROM pixelblocks WHERE uuid = ?"); - prep.setString(1, pixelBlock.uuid.toString()); + try { + PreparedStatement prep = this.db.prepareStatement("DELETE FROM pixelblocks WHERE uuid = ?"); + prep.setString(1, pixelBlock.blockUUID.toString()); prep.executeUpdate(); prep.close(); @@ -46,7 +44,7 @@ public class DataBase { } public void savePixelBlock(PixelBlock pixelBlock) { - Bukkit.getScheduler().runTask(plugin, () -> { + Bukkit.getScheduler().runTask(PixelBlocksPlugin.plugin, () -> { List uuids = new ArrayList<>(); try (var statement = getStatement()) { @@ -58,17 +56,17 @@ public class DataBase { System.err.println(e.getMessage()); } - if(!uuids.contains(pixelBlock.uuid)) { + if(!uuids.contains(pixelBlock.blockUUID)) { - try (var conn = getConnection()) { - PreparedStatement prep = conn.prepareStatement( + try { + PreparedStatement prep = this.db.prepareStatement( "INSERT INTO pixelblocks(uuid, owner, " + "locationWorldName, locationX, locationY, locationZ, " + "entryLocationWorldName, entryLocationX, entryLocationY, entryLocationZ, direction) " + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);" ); - prep.setString(1, pixelBlock.uuid.toString()); - prep.setString(2, pixelBlock.ownerUID.toString()); + prep.setString(1, pixelBlock.blockUUID.toString()); + prep.setString(2, pixelBlock.ownerUUID.toString()); prep.setString(3, pixelBlock.pixelBlockLocation.getWorld().getName()); prep.setDouble(4, pixelBlock.pixelBlockLocation.getX()); @@ -87,7 +85,7 @@ public class DataBase { prep.setDouble(10, Bukkit.getWorlds().getFirst().getSpawnLocation().getZ()); } - prep.setString(11, pixelBlock.direction.toString()); + prep.setString(11, pixelBlock.facingDirection.toString()); prep.executeUpdate(); prep.close(); @@ -96,14 +94,14 @@ public class DataBase { } } else { - try (var conn = getConnection()) { - PreparedStatement prep = conn.prepareStatement( + try { + PreparedStatement prep = this.db.prepareStatement( "UPDATE pixelblocks " + "SET owner=?, locationWorldName=?, locationX=?, locationY=?, locationZ=?," + "entryLocationWorldName=?, entryLocationX=?, entryLocationY=?, entryLocationZ=?, direction=? " + "WHERE uuid=?;" ); - prep.setString(1, pixelBlock.ownerUID.toString()); + prep.setString(1, pixelBlock.ownerUUID.toString()); prep.setString(2, pixelBlock.pixelBlockLocation.getWorld().getName()); prep.setDouble(3, pixelBlock.pixelBlockLocation.getX()); @@ -122,8 +120,8 @@ public class DataBase { prep.setDouble(9, Bukkit.getWorlds().getFirst().getSpawnLocation().getZ()); } - prep.setString(10, pixelBlock.uuid.toString()); - prep.setString(11, pixelBlock.direction.toString()); + prep.setString(10, pixelBlock.blockUUID.toString()); + prep.setString(11, pixelBlock.facingDirection.toString()); prep.executeUpdate(); prep.close(); @@ -184,13 +182,12 @@ public class DataBase { newPixelBlockLocation.getChunk().getEntities(); } - entities.stream().filter(entity -> entity. - getLocation(). - clone(). - add(0, PixelBlock.hitboxOffset, 0). - toBlockLocation(). - equals(newPixelBlockLocation)). - forEach(Entity::remove); + entities.stream().filter(entity -> entity + .getLocation() + .add(0, PixelBlocksPlugin.configuration.hitboxOffset(), 0) + .toBlockLocation() + .equals(newPixelBlockLocation)) + .forEach(Entity::remove); PixelBlock newPixelBlock = new PixelBlock( newPixelBlockLocation, diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlockItem.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlockItem.java new file mode 100644 index 0000000..98e877f --- /dev/null +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlockItem.java @@ -0,0 +1,50 @@ +package eu.mhsl.minecraft.pixelblocks; + +import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock; +import net.kyori.adventure.text.Component; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.Recipe; +import org.bukkit.inventory.ShapedRecipe; +import org.bukkit.inventory.meta.ItemMeta; +import org.jetbrains.annotations.NotNull; + +import java.util.Optional; + +public class PixelBlockItem { + public static @NotNull ItemStack getBlockAsItem(@NotNull PixelBlock block) { + ItemStack itemStack = ItemStack.of(Material.GRAY_STAINED_GLASS); + String ownerName = Optional.ofNullable(Bukkit.getOfflinePlayer(block.ownerUUID).getName()).orElseGet(() -> block.ownerUUID.toString()); + + ItemMeta meta = itemStack.getItemMeta(); + meta.itemName(Component.text(block.blockUUID.toString())); + meta.displayName(Component.text("Pixelblock von " + ownerName)); + meta.setEnchantmentGlintOverride(true); + itemStack.setItemMeta(meta); + + return itemStack; + } + + public static @NotNull ItemStack getEmptyPixelBlock() { + ItemStack item = ItemStack.of(Material.GRAY_STAINED_GLASS); + ItemMeta meta = item.getItemMeta(); + meta.displayName(Component.text("Leerer Pixelblock")); + meta.setEnchantmentGlintOverride(true); + meta.itemName(Component.text("Pixelblock")); + item.setItemMeta(meta); + return item; + } + + public static @NotNull Recipe getRecipe() { + NamespacedKey key = new NamespacedKey(PixelBlocksPlugin.plugin, "pixelblock"); + ShapedRecipe recipe = new ShapedRecipe(key, getEmptyPixelBlock()); + recipe.shape("ABA", "BCB", "ABA"); + recipe.setIngredient('A', Material.DIAMOND_BLOCK); + recipe.setIngredient('B', Material.EMERALD_BLOCK); + recipe.setIngredient('C', Material.GRAY_STAINED_GLASS); + return recipe; + } +} + diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlocks.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlocks.java deleted file mode 100644 index a8b5b8e..0000000 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlocks.java +++ /dev/null @@ -1,94 +0,0 @@ -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; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.plugin.java.JavaPlugin; - -import java.io.File; -import java.util.Objects; - -public final class PixelBlocks extends JavaPlugin { - public static PixelBlocks plugin; - public static DataBase dataBase; - 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.addDefault("deleteOnBreak", false); - config.addDefault("onlyBreakableByOwners", false); - config.addDefault("onlyEditableByOwners", true); - 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"); - PixelBlock.deleteOnBreak = config.getBoolean("deleteOnBreak"); - PixelBlock.onlyBreakableByOwners = config.getBoolean("onlyBreakableByOwners"); - PixelBlock.onlyEditableByOwners = config.getBoolean("onlyEditableByOwners"); - - dataBase = new DataBase("jdbc:sqlite:pixelblocks.db"); - dataBase.loadPixelBlocks(); - - Listener[] listeners = { - new PlayerInteractListener(), - new PlayerMoveListener(), - new EntityDamageListener(), - new BlockBreakListener(), - new BlockPlaceListener(), - new CreatureSpawnListener(), - new EntityExplodeListener(), - new BlockExplodeListener(), - new PlayerPortalListener(), - new InventoryListener(), - new PlayerChangeWorldListener(), - new CraftItemListener() - }; - - for (Listener listener : listeners) { - getServer().getPluginManager().registerEvents(listener, plugin); - } - - Objects.requireNonNull(getCommand("createpixelblock")).setExecutor(new CreatePixelBlockCommand()); - Objects.requireNonNull(getCommand("exitworld")).setExecutor(new ExitWorldCommand()); - - - ItemStack item = ItemStack.of(Material.GRAY_STAINED_GLASS); - ItemMeta meta = item.getItemMeta(); - meta.setDisplayName("Pixelblock"); - meta.setEnchantmentGlintOverride(true); - meta.setItemName("Pixelblock"); - item.setItemMeta(meta); - - NamespacedKey key = new NamespacedKey(this, "pixelblock"); - ShapedRecipe recipe = new ShapedRecipe(key, item); - recipe.shape("ABA", "BCB", "ABA"); - recipe.setIngredient('A', Material.DIAMOND_BLOCK); - recipe.setIngredient('B', Material.EMERALD_BLOCK); - recipe.setIngredient('C', Material.GRAY_STAINED_GLASS); - Bukkit.addRecipe(recipe); - } - -} diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlocksPlugin.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlocksPlugin.java new file mode 100644 index 0000000..6f2c459 --- /dev/null +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/PixelBlocksPlugin.java @@ -0,0 +1,68 @@ +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.configuration.file.FileConfiguration; +import org.bukkit.event.Listener; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public final class PixelBlocksPlugin extends JavaPlugin { + public static PixelBlocksPlugin plugin; + public static PixelBlockConfiguration configuration; + public static PixelBlockDatabase database; + + public static List placedPixelBlocks = new ArrayList<>(); + + @Override + public void onLoad() { + PixelBlocksPlugin.plugin = this; + + FileConfiguration config = this.getConfig(); + PixelBlockConfiguration.setDefaults(config); + this.saveConfig(); + + PixelBlocksPlugin.configuration = new PixelBlockConfiguration( + config.getInt(PixelBlockConfiguration.Keys.PixelsPerBlock.getKey()), + config.getBoolean(PixelBlockConfiguration.Keys.LiveUpdate.getKey()), + config.getInt(PixelBlockConfiguration.Keys.WorldGrassBorderWidth.getKey()), + config.getDouble(PixelBlockConfiguration.Keys.HitboxOffset.getKey()), + config.getBoolean(PixelBlockConfiguration.Keys.DeleteOnBreak.getKey()), + config.getBoolean(PixelBlockConfiguration.Keys.OnlyBreakableByOwners.getKey()), + config.getBoolean(PixelBlockConfiguration.Keys.OnlyEditableByOwners.getKey()) + ); + + PixelBlocksPlugin.database = new PixelBlockDatabase("jdbc:sqlite:pixelblocks.db"); + } + + @Override + public void onEnable() { + database.loadPixelBlocks(); + + Listener[] listeners = { + new PlayerInteractListener(), + new PlayerMoveListener(), +// new BlockBreakListener(), + new BlockPlaceListener(), + new InventoryListener(), + new PlayerChangeWorldListener(), + new CraftItemListener(), + new InPixelWorldCancelListener() + }; + + for (Listener listener : listeners) { + getServer().getPluginManager().registerEvents(listener, plugin); + } + + Objects.requireNonNull(getCommand("createpixelblock")).setExecutor(new CreatePixelBlockCommand()); + Objects.requireNonNull(getCommand("exitworld")).setExecutor(new ExitWorldCommand()); + + Bukkit.addRecipe(PixelBlockItem.getRecipe()); + } +} diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/chunkGenerators/EmptyChunkGenerator.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/chunkGenerators/EmptyChunkGenerator.java index c93fe5c..da783d6 100644 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/chunkGenerators/EmptyChunkGenerator.java +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/chunkGenerators/EmptyChunkGenerator.java @@ -6,7 +6,7 @@ import org.bukkit.generator.ChunkGenerator; import javax.annotation.Nonnull; import java.util.Random; -public class EmptyChunkGenerator extends ChunkGenerator { +public class EmptyChunkGenerator extends ChunkGenerator { @Override @Nonnull public ChunkData generateChunkData(@Nonnull World world, @Nonnull Random random, int x, int y, @Nonnull BiomeGrid biome) { diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/commands/CreatePixelBlockCommand.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/commands/CreatePixelBlockCommand.java index 2b27576..7b537ca 100644 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/commands/CreatePixelBlockCommand.java +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/commands/CreatePixelBlockCommand.java @@ -1,6 +1,6 @@ package eu.mhsl.minecraft.pixelblocks.commands; -import eu.mhsl.minecraft.pixelblocks.pixelblock.Direction; +import eu.mhsl.minecraft.pixelblocks.utils.Direction; import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -24,7 +24,7 @@ public class CreatePixelBlockCommand implements CommandExecutor { if(Arrays.stream(standardWorlds).toList().contains(playerWorld)) { Location playerLocation = p.getLocation(); PixelBlock pixelBlock = new PixelBlock(playerLocation, p.getUniqueId(), UUID.randomUUID()); - pixelBlock.place(playerLocation, Direction.POSITIVE_Z); + pixelBlock.place(playerLocation, Direction.south); } else { p.sendMessage("Du kannst nur in der Overworld oder im Nether Pixelblocks erstellen!"); } diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/commands/ExitWorldCommand.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/commands/ExitWorldCommand.java index 0448e39..3d5d40d 100644 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/commands/ExitWorldCommand.java +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/commands/ExitWorldCommand.java @@ -1,5 +1,6 @@ package eu.mhsl.minecraft.pixelblocks.commands; +import eu.mhsl.minecraft.pixelblocks.PixelBlocksPlugin; import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -21,8 +22,8 @@ public class ExitWorldCommand implements CommandExecutor { if(!Arrays.stream(standardWorlds).toList().contains(playerWorld)) { - List ownedBlocks = PixelBlock.placedPixelBlocks.stream() - .filter(pixelBlock -> pixelBlock.ownerUID.equals(p.getUniqueId())) + List ownedBlocks = PixelBlocksPlugin.placedPixelBlocks.stream() + .filter(pixelBlock -> pixelBlock.ownerUUID.equals(p.getUniqueId())) .sorted(Comparator.comparing(pixelBlock -> pixelBlock.lastEntryTime)) .toList(); 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 bfab9a0..6645e5b 100644 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockBreakListener.java +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockBreakListener.java @@ -1,18 +1,18 @@ -package eu.mhsl.minecraft.pixelblocks.listeners; - -import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.block.BlockBreakEvent; - -public class BlockBreakListener implements Listener { - @EventHandler - static void onBlockBreak(BlockBreakEvent event) { - if(CommonEventHandlers.isInPixelWorld(event.getBlock().getWorld())) { - PixelBlock pixelBlock = PixelBlock.getPixelBlockFromWorld(event.getBlock().getLocation().getWorld()); - - assert pixelBlock != null; - pixelBlock.handleBlockBreak(event); - } - } -} +//package eu.mhsl.minecraft.pixelblocks.listeners; +// +//import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock; +//import org.bukkit.event.EventHandler; +//import org.bukkit.event.Listener; +//import org.bukkit.event.block.BlockBreakEvent; +// +//public class BlockBreakListener implements Listener { +// @EventHandler +// static void onBlockBreak(BlockBreakEvent event) { +// if(CommonEventHandlers.isInPixelWorld(event.getBlock().getWorld())) { +// PixelBlock pixelBlock = PixelBlock.getPixelBlockFromWorld(event.getBlock().getLocation().getWorld()); +// +// assert pixelBlock != null; +// pixelBlock.handleBlockBreak(event); +// } +// } +//} diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockExplodeListener.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockExplodeListener.java deleted file mode 100644 index cbdf73f..0000000 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockExplodeListener.java +++ /dev/null @@ -1,12 +0,0 @@ -package eu.mhsl.minecraft.pixelblocks.listeners; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.block.BlockExplodeEvent; - -public class BlockExplodeListener implements Listener { - @EventHandler - static void onBlockExplode(BlockExplodeEvent event) { - CommonEventHandlers.cancelIfInPixelWorld(event, event.getBlock().getWorld()); - } -} 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 d0429b8..6b4c688 100644 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockPlaceListener.java +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/BlockPlaceListener.java @@ -1,6 +1,7 @@ package eu.mhsl.minecraft.pixelblocks.listeners; import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock; +import eu.mhsl.minecraft.pixelblocks.utils.Direction; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; @@ -15,6 +16,7 @@ import java.util.UUID; public class BlockPlaceListener implements Listener { @EventHandler static void onBlockPlace(BlockPlaceEvent event) { + if(event.getItemInHand().getItemMeta().getDisplayName().contains("Pixelblock") && !event.getItemInHand().getItemMeta().getItemName().isEmpty() && event.getItemInHand().getItemMeta().getEnchantmentGlintOverride()) { @@ -33,7 +35,7 @@ public class BlockPlaceListener implements Listener { } PixelBlock pixelBlock = new PixelBlock(newBlockLocation, event.getPlayer().getUniqueId(), UUID.fromString(event.getItemInHand().getItemMeta().getItemName())); - if(pixelBlock.place(newBlockLocation, PixelBlock.vectorToDirection(event.getPlayer().getLocation().getDirection()))) { + if(pixelBlock.place(newBlockLocation, Direction.vectorToDirection(event.getPlayer().getLocation().getDirection()))) { event.getPlayer().getInventory().remove(event.getItemInHand()); } else { event.getPlayer().sendMessage("Hier wurde bereits ein Pixelblock plaziert."); @@ -42,12 +44,12 @@ public class BlockPlaceListener implements Listener { event.getPlayer().sendMessage("Du kannst nur in der Overworld oder im Nether Pixelblocks platzieren!"); } } else { - if(CommonEventHandlers.isInPixelWorld(event.getBlock().getWorld())) { - PixelBlock pixelBlock = PixelBlock.getPixelBlockFromWorld(event.getBlock().getLocation().getWorld()); - - assert pixelBlock != null; - pixelBlock.handleBlockPlace(event); - } +// if(CommonEventHandlers.isInPixelWorld(event.getBlock().getWorld())) { +// PixelBlock pixelBlock = PixelBlock.getPixelBlockFromWorld(event.getBlock().getLocation().getWorld()); +// +// assert pixelBlock != null; +// pixelBlock.handleBlockPlace(event); +// } } } } diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/CommonEventHandlers.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/CommonEventHandlers.java index 07e1887..6f7f094 100644 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/CommonEventHandlers.java +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/CommonEventHandlers.java @@ -6,12 +6,12 @@ import org.bukkit.event.Cancellable; import java.util.List; -import static eu.mhsl.minecraft.pixelblocks.PixelBlocks.plugin; +import static eu.mhsl.minecraft.pixelblocks.PixelBlocksPlugin.plugin; public class CommonEventHandlers { public static boolean isInPixelWorld(World world) { List standardWorlds = Bukkit.getWorlds().stream() - .filter(world1 -> !world1.getName().startsWith(plugin.getDataFolder().getPath())) + .filter(w -> !w.getName().startsWith(plugin.getDataFolder().getPath())) .toList(); return !standardWorlds.contains(world); diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/CreatureSpawnListener.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/CreatureSpawnListener.java deleted file mode 100644 index b14c67e..0000000 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/CreatureSpawnListener.java +++ /dev/null @@ -1,12 +0,0 @@ -package eu.mhsl.minecraft.pixelblocks.listeners; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.entity.CreatureSpawnEvent; - -public class CreatureSpawnListener implements Listener { - @EventHandler - static void onCreatureSpawn(CreatureSpawnEvent event) { - CommonEventHandlers.cancelIfInPixelWorld(event, event.getLocation().getWorld()); - } -} diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/EntityDamageListener.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/EntityDamageListener.java deleted file mode 100644 index 7cafda8..0000000 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/EntityDamageListener.java +++ /dev/null @@ -1,12 +0,0 @@ -package eu.mhsl.minecraft.pixelblocks.listeners; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.entity.EntityDamageEvent; - -public class EntityDamageListener implements Listener { - @EventHandler - static void onEntityDamage(EntityDamageEvent event) { - CommonEventHandlers.cancelIfInPixelWorld(event, event.getEntity().getWorld()); - } -} diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/EntityExplodeListener.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/EntityExplodeListener.java deleted file mode 100644 index 08b0b66..0000000 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/EntityExplodeListener.java +++ /dev/null @@ -1,12 +0,0 @@ -package eu.mhsl.minecraft.pixelblocks.listeners; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.entity.EntityExplodeEvent; - -public class EntityExplodeListener implements Listener { - @EventHandler - static void onEntityExplode(EntityExplodeEvent event) { - CommonEventHandlers.cancelIfInPixelWorld(event, event.getLocation().getWorld()); - } -} diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/InPixelWorldCancelListener.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/InPixelWorldCancelListener.java new file mode 100644 index 0000000..3a072a2 --- /dev/null +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/InPixelWorldCancelListener.java @@ -0,0 +1,42 @@ +package eu.mhsl.minecraft.pixelblocks.listeners; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockExplodeEvent; +import org.bukkit.event.entity.CreatureSpawnEvent; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.entity.EntityExplodeEvent; +import org.bukkit.event.player.PlayerDropItemEvent; +import org.bukkit.event.player.PlayerPortalEvent; + +public class InPixelWorldCancelListener implements Listener { + @EventHandler + static void onBlockExplode(BlockExplodeEvent event) { + CommonEventHandlers.cancelIfInPixelWorld(event, event.getBlock().getWorld()); + } + + @EventHandler + static void onCreatureSpawn(CreatureSpawnEvent event) { + CommonEventHandlers.cancelIfInPixelWorld(event, event.getLocation().getWorld()); + } + + @EventHandler + static void onEntityDamage(EntityDamageEvent event) { + CommonEventHandlers.cancelIfInPixelWorld(event, event.getEntity().getWorld()); + } + + @EventHandler + static void onEntityExplode(EntityExplodeEvent event) { + CommonEventHandlers.cancelIfInPixelWorld(event, event.getLocation().getWorld()); + } + + @EventHandler + static void onPlayerDropItem(PlayerDropItemEvent event) { + CommonEventHandlers.cancelIfInPixelWorld(event, event.getItemDrop().getWorld()); + } + + @EventHandler + static void onPlayerPortal(PlayerPortalEvent event) { + CommonEventHandlers.cancelIfInPixelWorld(event, event.getFrom().getWorld()); + } +} diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerChangeWorldListener.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerChangeWorldListener.java index ba1e92f..9b73ad9 100644 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerChangeWorldListener.java +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerChangeWorldListener.java @@ -12,7 +12,7 @@ public class PlayerChangeWorldListener implements Listener { static void onPlayerChangeWorld(PlayerChangedWorldEvent event) { if(CommonEventHandlers.isInPixelWorld(event.getFrom())) { // Bukkit.getScheduler().runTaskLater(plugin, () -> Objects.requireNonNull(PixelBlock.getPixelBlockFromWorld(event.getFrom())).update(), 60); - Objects.requireNonNull(PixelBlock.getPixelBlockFromWorld(event.getFrom())).update(); + Objects.requireNonNull(PixelBlock.getPixelBlockFromBlockWorld(event.getFrom())).update(); } } } diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerDropItemListener.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerDropItemListener.java deleted file mode 100644 index 36d9a2e..0000000 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerDropItemListener.java +++ /dev/null @@ -1,12 +0,0 @@ -package eu.mhsl.minecraft.pixelblocks.listeners; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerDropItemEvent; - -public class PlayerDropItemListener implements Listener { - @EventHandler - static void onPlayerDropItem(PlayerDropItemEvent event) { - CommonEventHandlers.cancelIfInPixelWorld(event, event.getItemDrop().getWorld()); - } -} diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerInteractListener.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerInteractListener.java index 02dcfa7..433002f 100644 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerInteractListener.java +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerInteractListener.java @@ -1,5 +1,6 @@ package eu.mhsl.minecraft.pixelblocks.listeners; +import eu.mhsl.minecraft.pixelblocks.PixelBlocksPlugin; import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock; import io.papermc.paper.event.player.PrePlayerAttackEntityEvent; import org.bukkit.*; @@ -12,28 +13,28 @@ public class PlayerInteractListener implements Listener { @EventHandler static void onPlayerInteractEntity(PlayerInteractEntityEvent event) { if(event.getRightClicked() instanceof Interaction) { - Location interactionLocation = event.getRightClicked().getLocation().clone().add(0, PixelBlock.hitboxOffset, 0).toBlockLocation(); + Location interactionLocation = event.getRightClicked().getLocation().clone().add(0, PixelBlocksPlugin.configuration.hitboxOffset(), 0).toBlockLocation(); interactionLocation.setYaw(0); interactionLocation.setPitch(0); - PixelBlock pixelBlock = PixelBlock.getPixelBlockFromLocation(interactionLocation); + PixelBlock pixelBlock = PixelBlock.getPixelBlockFromPlacedLocation(interactionLocation); assert pixelBlock != null; - pixelBlock.handleInteraction(event.getPlayer()); + pixelBlock.enterBlock(event.getPlayer()); } } @EventHandler static void onPlayerAttackEntity(PrePlayerAttackEntityEvent event) { if(event.getAttacked() instanceof Interaction) { - Location blockLocation = event.getAttacked().getLocation().add(0, PixelBlock.hitboxOffset, 0).toBlockLocation(); + Location blockLocation = event.getAttacked().getLocation().add(0, PixelBlocksPlugin.configuration.hitboxOffset(), 0).toBlockLocation(); blockLocation.setYaw(0); blockLocation.setPitch(0); - PixelBlock pixelBlock = PixelBlock.getPixelBlockFromLocation(blockLocation); + PixelBlock pixelBlock = PixelBlock.getPixelBlockFromPlacedLocation(blockLocation); assert pixelBlock != null; - pixelBlock.handleAttack(event.getPlayer()); + pixelBlock.destroy(event.getPlayer()); } } } diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerMoveListener.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerMoveListener.java index 63123e3..61bba8a 100644 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerMoveListener.java +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerMoveListener.java @@ -11,12 +11,12 @@ public class PlayerMoveListener implements Listener { static void onPlayerMove(PlayerMoveEvent event) { if(CommonEventHandlers.isInPixelWorld(event.getPlayer().getWorld())) { Location playerLocation = event.getPlayer().getLocation(); - PixelBlock pixelBlock = PixelBlock.getPixelBlockFromWorld(event.getPlayer().getWorld()); + PixelBlock pixelBlock = PixelBlock.getPixelBlockFromBlockWorld(event.getPlayer().getWorld()); assert pixelBlock != null; - Location portalLocation = pixelBlock.getPortalLocation().clone(); + Location portalLocation = pixelBlock.getPixelWorld().getPortalLocation().clone(); if(playerLocation.y() < -65) { - event.getPlayer().teleport(pixelBlock.getPlayerSpawnLocation(event.getPlayer())); + event.getPlayer().teleport(pixelBlock.getPixelWorld().getSpawnLocation()); } else if(playerLocation.getZ() < portalLocation.getZ()+1 && playerLocation.getZ() > portalLocation.getZ()) { if(playerLocation.getX() < portalLocation.getX()+3 diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerPortalListener.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerPortalListener.java deleted file mode 100644 index 0cbb789..0000000 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/listeners/PlayerPortalListener.java +++ /dev/null @@ -1,12 +0,0 @@ -package eu.mhsl.minecraft.pixelblocks.listeners; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerPortalEvent; - -public class PlayerPortalListener implements Listener { - @EventHandler - static void onPlayerPortal(PlayerPortalEvent event) { - CommonEventHandlers.cancelIfInPixelWorld(event, event.getFrom().getWorld()); - } -} diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/Direction.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/Direction.java deleted file mode 100644 index 67b63b5..0000000 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/Direction.java +++ /dev/null @@ -1,8 +0,0 @@ -package eu.mhsl.minecraft.pixelblocks.pixelblock; - -public enum Direction { - POSITIVE_X, - POSITIVE_Z, - NEGATIVE_X, - NEGATIVE_Z -} diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/Pixel.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/Pixel.java index d924d0a..7c9e655 100644 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/Pixel.java +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/Pixel.java @@ -1,23 +1,26 @@ package eu.mhsl.minecraft.pixelblocks.pixelblock; import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.block.data.BlockData; import org.bukkit.entity.BlockDisplay; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.util.Transformation; +import org.jetbrains.annotations.NotNull; +import javax.annotation.Nullable; +import java.util.Objects; import java.util.UUID; public class Pixel { - public Location relativeLocation; - public BlockData blockData; - double scale; - double offset; + public final Location relativeLocation; + public final BlockData blockData; + private final double scale; - public UUID uuid; + private @Nullable UUID uuid; - public Pixel(Location relativeLocation, BlockData blockData, double scale, double offset) { + public Pixel(@NotNull Location relativeLocation, @NotNull BlockData blockData, double scale) { this.relativeLocation = new Location( relativeLocation.getWorld(), relativeLocation.x(), @@ -26,31 +29,28 @@ public class Pixel { ); this.blockData = blockData; this.scale = scale; - this.offset = offset; } - public void spawn(Location spawnBlockLocation) { - double positionX = spawnBlockLocation.x()+relativeLocation.x()*scale+offset; - double positionY = spawnBlockLocation.y()+relativeLocation.y()*scale+offset; - double positionZ = spawnBlockLocation.z()+relativeLocation.z()*scale+offset; + public void place(@NotNull Location spawnBlockLocation) { + World world = spawnBlockLocation.getWorld(); + double positionX = spawnBlockLocation.x() + (relativeLocation.x()*scale); + double positionY = spawnBlockLocation.y() + (relativeLocation.y()*scale); + double positionZ = spawnBlockLocation.z() + (relativeLocation.z()*scale); - Location spawnLocation = new Location(spawnBlockLocation.getWorld(), positionX, positionY, positionZ); + Location spawnLocation = new Location(world, positionX, positionY, positionZ); - BlockDisplay bd = (BlockDisplay) spawnLocation.getWorld().spawnEntity( - spawnLocation, - EntityType.BLOCK_DISPLAY - ); + BlockDisplay entity = (BlockDisplay) world.spawnEntity(spawnLocation, EntityType.BLOCK_DISPLAY); + this.uuid = entity.getUniqueId(); - bd.setBlock(blockData); + entity.setBlock(blockData); - Transformation transform = bd.getTransformation(); + Transformation transform = entity.getTransformation(); transform.getScale().set(scale); - bd.setTransformation(transform); - - this.uuid = bd.getUniqueId(); + entity.setTransformation(transform); } - public void remove() { + public void destroy() { + Objects.requireNonNull(this.uuid); Entity pixelEntity = this.relativeLocation.getWorld().getEntity(this.uuid); if(pixelEntity != null) pixelEntity.remove(); } 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 387ae4e..0a88e0e 100644 --- a/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlock.java +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlock.java @@ -1,308 +1,110 @@ package eu.mhsl.minecraft.pixelblocks.pixelblock; -import eu.mhsl.minecraft.pixelblocks.PixelBlocks; -import eu.mhsl.minecraft.pixelblocks.chunkGenerators.EmptyChunkGenerator; +import eu.mhsl.minecraft.pixelblocks.PixelBlockItem; +import eu.mhsl.minecraft.pixelblocks.PixelBlocksPlugin; +import eu.mhsl.minecraft.pixelblocks.utils.Direction; +import eu.mhsl.minecraft.pixelblocks.utils.MinMaxUtil; import org.bukkit.*; import org.bukkit.block.data.BlockData; import org.bukkit.entity.*; -import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.util.Vector; -import org.codehaus.plexus.util.FileUtils; +import org.jetbrains.annotations.NotNull; +import javax.annotation.Nullable; import java.io.File; -import java.io.IOException; import java.util.*; -import static eu.mhsl.minecraft.pixelblocks.PixelBlocks.*; -import static java.lang.Math.abs; - public class PixelBlock { - public static List placedPixelBlocks = new ArrayList<>(); - public static float hitboxOffset = 0.005F; - public static int worldGrassBorderWidth = 5; - public static int pixelsPerBlock = 16; - public static boolean liveUpdate = true; - public static boolean deleteOnBreak = false; - public static boolean onlyBreakableByOwners = false; - public static boolean onlyEditableByOwners = true; + private final PixelBlockWorld pixelWorld; + + private final float hitboxOffset = (float) PixelBlocksPlugin.configuration.hitboxOffset(); + private final int pixelsPerBlock = PixelBlocksPlugin.configuration.pixelsPerBlock(); public Location pixelBlockLocation; - public Direction direction; + public Direction facingDirection; public ArrayList pixels = new ArrayList<>(); + public Interaction hitbox; public ItemDisplay barrier; public long lastEntryTime; public Location lastEntryLocation; - public UUID ownerUID; - public UUID uuid; + public UUID ownerUUID; + public UUID blockUUID; - public PixelBlock(Location originLocation, UUID ownerUID, UUID blockUUID) { - this.uuid = blockUUID; + public static @NotNull String getWorldName(@NotNull PixelBlock pixelBlock) { + return PixelBlocksPlugin.plugin.getDataFolder().getPath() + File.separator + pixelBlock.blockUUID; + } + + public static @Nullable PixelBlock getPixelBlockFromBlockWorld(World world) { + return PixelBlocksPlugin.placedPixelBlocks.stream() + .filter(block -> block.blockUUID.equals(getUUIDFromWorld(world))) + .findFirst() + .orElse(null); + } + + public static @Nullable UUID getUUIDFromWorld(@NotNull World world) { + try { + return UUID.fromString(List.of(world.getName().split("/")).getLast()); + } catch (IllegalArgumentException e) { + return null; + } + } + + public static @Nullable PixelBlock getPixelBlockFromPlacedLocation(@NotNull Location placedLocation) { + Location searchLocation = placedLocation.clone().toBlockLocation(); + searchLocation.setPitch(0); + searchLocation.setYaw(0); + + return PixelBlocksPlugin.placedPixelBlocks.stream() + .filter(block -> block.pixelBlockLocation.equals(searchLocation)) + .findFirst() + .orElse(null); + } + + public PixelBlock(Location originLocation, UUID ownerUUID, UUID blockUUID) { + this.ownerUUID = ownerUUID; + this.blockUUID = blockUUID; this.pixelBlockLocation = originLocation.toBlockLocation(); - this.direction = Direction.POSITIVE_Z; this.pixelBlockLocation.setYaw(0); this.pixelBlockLocation.setPitch(0); - this.ownerUID = ownerUID; - createPixelWorld(); + this.facingDirection = Direction.south; // TODO ?? + + this.pixelWorld = new PixelBlockWorld(this); } - public static PixelBlock getPixelBlockFromWorld(World world) { - String blockUUID = Arrays.stream(world.getName().split(pathSeparator)).toList().getLast(); - List pixelBlocks = placedPixelBlocks.stream().filter(block -> block.uuid.toString().equals(blockUUID)).toList(); - - if(!pixelBlocks.isEmpty()) { - return pixelBlocks.getFirst(); - } else { - return null; - } - } - - public static PixelBlock getPixelBlockFromLocation(Location location) { - Location loc = location.clone().toBlockLocation(); - loc.setPitch(0); - loc.setYaw(0); - - List pixelBlocks = placedPixelBlocks.stream() - .filter(block -> block.pixelBlockLocation.equals(loc)) - .toList(); - - if(pixelBlocks.isEmpty()) { - return null; - } else { - return pixelBlocks.getFirst(); - } - } - - public static String getWorldPathFromPixelblock(PixelBlock pixelBlock) { - return plugin.getDataFolder().getPath() + pathSeparator + pixelBlock.uuid.toString(); - } - - public static Direction vectorToDirection(Vector vector) { - if(abs(vector.getX()) > abs(vector.getZ())) { - if(vector.getX() >= 0) { - return Direction.POSITIVE_X; - } else { - return Direction.NEGATIVE_X; - } - } else { - if(vector.getZ() >= 0) { - return Direction.POSITIVE_Z; - } else { - return Direction.NEGATIVE_Z; - } - } - } - - - void createPixelWorld() { - File file = new File(getWorldPathFromPixelblock(this)); - final WorldCreator worldCreator = new WorldCreator(getWorldPathFromPixelblock(this)); - - if(!file.exists() || !file.isDirectory()) { - worldCreator.type(WorldType.FLAT); - worldCreator.generator(new EmptyChunkGenerator()); - World newWorld = Bukkit.createWorld(worldCreator); - - assert newWorld != null; - newWorld.setGameRule(GameRule.RANDOM_TICK_SPEED, 0); - newWorld.setGameRule(GameRule.DO_FIRE_TICK, false); - newWorld.setGameRule(GameRule.DO_MOB_SPAWNING, false); - newWorld.setGameRule(GameRule.DO_WEATHER_CYCLE, false); - newWorld.setGameRule(GameRule.DO_VINES_SPREAD, false); - newWorld.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false); - - Location borderStartLocation = newWorld.getSpawnLocation().clone().subtract(1, 1, 1); - Location grassStartLocation = borderStartLocation.clone().subtract(worldGrassBorderWidth, 0, worldGrassBorderWidth); - - Bukkit.getScheduler().runTask(plugin, () -> { - 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 < (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 < (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() + (pixelsPerBlock+1) || currentLocation.z() == borderStartLocation.z() + (pixelsPerBlock+1)) { - currentLocation.getBlock().setType(Material.RED_CONCRETE); - } - } - } - - for (int x = (2*worldGrassBorderWidth+pixelsPerBlock-1)/2; x < (2*worldGrassBorderWidth+pixelsPerBlock-1)/2+4; x++) { - for (int y = 0; y < 5; y++) { - grassStartLocation.clone().add(x, 1+y, 0).getBlock().setType(Material.OBSIDIAN); - } - } - for (int x = (2*worldGrassBorderWidth+pixelsPerBlock-1)/2+1; x < (2*worldGrassBorderWidth+pixelsPerBlock-1)/2+3; x++) { - for (int y = 1; y < 4; y++) { - grassStartLocation.clone().add(x, 1+y, 0).getBlock().setType(Material.NETHER_PORTAL); - } - } - }); - } else { - worldCreator.createWorld(); - } - } - - public ItemStack getAsItem() { - ItemStack itemStack = ItemStack.of(Material.GRAY_STAINED_GLASS); - - ItemMeta meta = itemStack.getItemMeta(); - meta.setDisplayName("Pixelblock von " + Objects.requireNonNull(Bukkit.getPlayer(this.ownerUID)).getName()); - meta.setEnchantmentGlintOverride(true); - meta.setItemName(this.uuid.toString()); - itemStack.setItemMeta(meta); - - return itemStack; - } - - public void handleInteraction(Player player) { - if(onlyEditableByOwners && !player.getUniqueId().equals(ownerUID)) { + public void enterBlock(@NotNull Player player) { + if(PixelBlocksPlugin.configuration.onlyEditableByOwner() && !player.getUniqueId().equals(ownerUUID)) { player.sendMessage("Dieser Pixelblock gehört nicht dir!"); return; } this.lastEntryLocation = player.getLocation(); this.lastEntryTime = System.currentTimeMillis(); - dataBase.savePixelBlock(this); + PixelBlocksPlugin.database.savePixelBlock(this); - player.teleport(getPlayerSpawnLocation(player)); - } - - public void handleAttack(Player player) { - if(onlyBreakableByOwners && !player.getUniqueId().equals(ownerUID)) { - player.sendMessage("Dieser Pixelblock gehört nicht dir!"); - return; - } - - World world = Objects.requireNonNull(this.getPixelWorld()); - - world.getPlayers().forEach( - player1 -> { - player1.sendMessage("Der Pixelblock wurde abgebaut!"); - player1.teleport(this.lastEntryLocation); - } - ); - - world.getEntities().forEach(entity -> entity.teleport(this.lastEntryLocation)); - - if(deleteOnBreak) { - this.delete(); - } else { - this.remove(); - this.pixelBlockLocation.getWorld().playSound(this.pixelBlockLocation, Sound.BLOCK_COPPER_BULB_BREAK, 1.0F, 30); - this.pixelBlockLocation.getWorld().dropItem(this.pixelBlockLocation, this.getAsItem()); - } - } - - 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()+(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.getX(), spawnLocation.getY(), spawnLocation.getZ()); - relativeLocation.setWorld(this.pixelBlockLocation.getWorld()); - List pixels = this.pixels.stream().filter(pixel -> pixel.relativeLocation.equals(relativeLocation)).toList(); - if(!pixels.isEmpty()) { - Pixel pixel = pixels.getFirst(); - pixel.remove(); - this.pixels.remove(pixel); - } - } - }); - } - - 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()+(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.getX(), spawnLocation.getY(), spawnLocation.getZ()); - relativeLocation.setWorld(this.pixelBlockLocation.getWorld()); - - Pixel newPixel = new Pixel(relativeLocation, event.getBlock().getBlockData(), ((double) 1 /pixelsPerBlock), 0); - this.pixels.add(newPixel); - newPixel.spawn(this.pixelBlockLocation); - } - }); - } - - public Location getPlayerSpawnLocation(Player player) { - Location spawnLocation = getPixelWorld().getSpawnLocation().clone().add((double) pixelsPerBlock/2, 0, 1.5-worldGrassBorderWidth); -// spawnLocation.setYaw(player.getLocation().getYaw()); -// spawnLocation.setPitch(player.getLocation().getPitch()); - return spawnLocation; - } - - public Location getPortalLocation() { - return getPixelWorld().getSpawnLocation().clone().add((double) pixelsPerBlock/2 -2, 0, -worldGrassBorderWidth-1); - } - - public World getPixelWorld() { - return Bukkit.getWorld(getWorldPathFromPixelblock(this)); + player.teleport(this.pixelWorld.getSpawnLocation()); } public void spawnInteraction(boolean fullBlock) { - if(fullBlock || pixels.isEmpty()) { + if(fullBlock) { hitbox = (Interaction) pixelBlockLocation.getWorld().spawnEntity( pixelBlockLocation.clone().add(0.5, -hitboxOffset, 0.5), EntityType.INTERACTION ); - hitbox.setInteractionHeight(1F + 2*hitboxOffset); - hitbox.setInteractionWidth(1F + 2*hitboxOffset); + hitbox.setInteractionHeight(1 + 2*hitboxOffset); + hitbox.setInteractionWidth(1 + 2*hitboxOffset); } else { - double startingX = this.pixels.stream() - .min(Comparator.comparing(pixel -> pixel.relativeLocation.getX())) - .orElseThrow().relativeLocation.getX(); - double startingY = this.pixels.stream() - .min(Comparator.comparing(pixel -> pixel.relativeLocation.getY())) - .orElseThrow().relativeLocation.getY(); - double startingZ = this.pixels.stream() - .min(Comparator.comparing(pixel -> pixel.relativeLocation.getZ())) - .orElseThrow().relativeLocation.getZ(); + double startingX = MinMaxUtil.getMinProperty(this.pixels, pixel -> pixel.relativeLocation.getX()); + double startingY = MinMaxUtil.getMinProperty(this.pixels, pixel -> pixel.relativeLocation.getY()); + double startingZ = MinMaxUtil.getMinProperty(this.pixels, pixel -> pixel.relativeLocation.getZ()); - double endingX = this.pixels.stream() - .max(Comparator.comparing(pixel -> pixel.relativeLocation.getX())) - .orElseThrow().relativeLocation.getX(); - double endingY = this.pixels.stream() - .max(Comparator.comparing(pixel -> pixel.relativeLocation.getY())) - .orElseThrow().relativeLocation.getY(); - double endingZ = this.pixels.stream() - .max(Comparator.comparing(pixel -> pixel.relativeLocation.getZ())) - .orElseThrow().relativeLocation.getZ(); + double endingX = MinMaxUtil.getMaxProperty(this.pixels, pixel -> pixel.relativeLocation.getX()); + double endingY = MinMaxUtil.getMaxProperty(this.pixels, pixel -> pixel.relativeLocation.getY()); + double endingZ = MinMaxUtil.getMaxProperty(this.pixels, pixel -> pixel.relativeLocation.getZ()); Location spawnLocation = pixelBlockLocation.clone().add( ((startingX+endingX+1)/2)/pixelsPerBlock, @@ -344,27 +146,24 @@ public class PixelBlock { } public void update() { - Bukkit.getScheduler().runTask(plugin, () -> { - this.clearEntities(true); + Bukkit.getScheduler().runTask(PixelBlocksPlugin.plugin, () -> { + this.clearEntities(); 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))) - .getSpawnLocation() - .clone(); - switch (this.direction) { - case POSITIVE_Z -> blockLocation.add(relativeLocation.x(), relativeLocation.y(), relativeLocation.z()); - case NEGATIVE_Z -> blockLocation.add((pixelsPerBlock-1)-relativeLocation.x(), relativeLocation.y(), (pixelsPerBlock-1)-relativeLocation.z()); - case POSITIVE_X -> blockLocation.add((pixelsPerBlock-1)-relativeLocation.z(), relativeLocation.y(), relativeLocation.x()); - case NEGATIVE_X -> blockLocation.add(relativeLocation.z(), relativeLocation.y(), (pixelsPerBlock-1)-relativeLocation.x()); + Location blockLocation = this.pixelWorld.getBuildOrigin(); + switch (this.facingDirection) { + case south -> blockLocation.add(relativeLocation.x(), relativeLocation.y(), relativeLocation.z()); + case north -> blockLocation.add((pixelsPerBlock-1)-relativeLocation.x(), relativeLocation.y(), (pixelsPerBlock-1)-relativeLocation.z()); + case east -> blockLocation.add((pixelsPerBlock-1)-relativeLocation.z(), relativeLocation.y(), relativeLocation.x()); + case west -> blockLocation.add(relativeLocation.z(), relativeLocation.y(), (pixelsPerBlock-1)-relativeLocation.x()); } BlockData block = blockLocation.getBlock().getBlockData(); if(block.getMaterial() != Material.AIR) { - Pixel newPixel = new Pixel(relativeLocation, block, ((double) 1 /pixelsPerBlock), 0); + Pixel newPixel = new Pixel(relativeLocation, block, ((double) 1 /pixelsPerBlock)); pixels.add(newPixel); } } @@ -372,15 +171,15 @@ public class PixelBlock { } for(Pixel pixel : this.pixels) { - pixel.spawn(this.pixelBlockLocation); + pixel.place(this.pixelBlockLocation); } if(this.pixels.size() < 5) { Location relativeLocation = new Location(pixelBlockLocation.getWorld(), 0, 0, 0); BlockData block = Material.GRAY_STAINED_GLASS.createBlockData(); - Pixel newPixel = new Pixel(relativeLocation, block, 1, 0); + Pixel newPixel = new Pixel(relativeLocation, block, 1); pixels.add(newPixel); - newPixel.spawn(this.pixelBlockLocation); + newPixel.place(this.pixelBlockLocation); Location itemDisplayLocation = this.pixelBlockLocation.clone().add(0.5, 0.5, 0.5); this.barrier = (ItemDisplay) this.pixelBlockLocation.getWorld().spawnEntity( @@ -396,17 +195,52 @@ public class PixelBlock { }); } - public void clearEntities(boolean secure) { - if(!this.pixelBlockLocation.getChunk().isEntitiesLoaded()) { - this.pixelBlockLocation.getChunk().load(true); - this.pixelBlockLocation.getChunk().getEntities(); + public boolean place(Location placeLocation, Direction direction) { + Location newLocation = placeLocation.toBlockLocation(); + newLocation.setPitch(0); + newLocation.setYaw(0); + + if(PixelBlock.getPixelBlockFromPlacedLocation(newLocation) == null || PixelBlock.getPixelBlockFromPlacedLocation(newLocation) == this) { + this.pixelBlockLocation = newLocation; + this.facingDirection = direction; + update(); + PixelBlocksPlugin.database.savePixelBlock(this); + PixelBlocksPlugin.placedPixelBlocks.add(this); + return true; + } + return false; + } + + public void destroy(Player destroyedBy) { + if(PixelBlocksPlugin.configuration.onlyBreakableByOwner() && !destroyedBy.getUniqueId().equals(ownerUUID)) { + destroyedBy.sendMessage("Dieser Pixelblock gehört nicht dir!"); + return; } - if(!pixels.isEmpty()) { - this.pixels.forEach(Pixel::remove); - this.pixels.clear(); + this.pixelWorld.getPlayersInWorld().forEach(p -> { + p.sendMessage("Der Pixelblock wurde abgebaut!"); + p.teleport(this.lastEntryLocation); + }); + + this.pixelWorld.getEntitiesInWorld().forEach(entity -> entity.teleport(this.lastEntryLocation)); + + this.clearEntities(); + PixelBlocksPlugin.database.removePixelBlock(this); + PixelBlocksPlugin.placedPixelBlocks.remove(this); + this.pixelBlockLocation.getWorld().playSound(this.pixelBlockLocation, Sound.BLOCK_COPPER_BULB_BREAK, 1.0F, 30); + this.pixelBlockLocation.getWorld().dropItem(this.pixelBlockLocation.add(new Vector(0.5, 0.5, 0.5)), PixelBlockItem.getBlockAsItem(this)); + } + + private void clearEntities() { + Chunk chunk = this.pixelBlockLocation.getChunk(); + if(!chunk.isEntitiesLoaded()) { + chunk.load(true); + chunk.getEntities(); } + this.pixels.forEach(Pixel::destroy); + this.pixels.clear(); + if(hitbox != null) { this.hitbox.remove(); this.hitbox = null; @@ -417,59 +251,23 @@ public class PixelBlock { this.barrier = null; } - if(secure) { - List entities = new ArrayList<>(); - entities.addAll(Bukkit.getWorlds().get(0).getEntities().stream() - .filter(entity -> entity.getType().equals(EntityType.BLOCK_DISPLAY) || entity.getType().equals(EntityType.INTERACTION) || entity.getType().equals(EntityType.ITEM_DISPLAY)) - .toList()); - entities.addAll(Bukkit.getWorlds().get(1).getEntities().stream() - .filter(entity -> entity.getType().equals(EntityType.BLOCK_DISPLAY) || entity.getType().equals(EntityType.INTERACTION) || entity.getType().equals(EntityType.ITEM_DISPLAY)) - .toList()); - entities.addAll(Bukkit.getWorlds().get(2).getEntities().stream() - .filter(entity -> entity.getType().equals(EntityType.BLOCK_DISPLAY) || entity.getType().equals(EntityType.INTERACTION) || entity.getType().equals(EntityType.ITEM_DISPLAY)) - .toList()); - - entities.stream().filter(entity -> entity. - getLocation(). - clone(). - add(0, PixelBlock.hitboxOffset, 0). - toBlockLocation(). - equals(this.pixelBlockLocation)). - forEach(Entity::remove); - } + this.pixelBlockLocation.getWorld().getEntities().stream() + .filter(this::isRelevantEntity) + .filter(entity -> entity.getLocation() + .add(0, hitboxOffset, 0) + .toBlockLocation() + .equals(this.pixelBlockLocation)) + .forEach(Entity::remove); } - public boolean place(Location placeLocation, Direction direction) { - Location newLocation = placeLocation.toBlockLocation(); - newLocation.setPitch(0); - newLocation.setYaw(0); - - if(PixelBlock.getPixelBlockFromLocation(newLocation) == null || PixelBlock.getPixelBlockFromLocation(newLocation) == this) { - this.pixelBlockLocation = newLocation; - this.direction = direction; - update(); - dataBase.savePixelBlock(this); - placedPixelBlocks.add(this); - return true; - } - return false; + private boolean isRelevantEntity(Entity entity) { + return entity.getType().equals(EntityType.BLOCK_DISPLAY) + || entity.getType().equals(EntityType.INTERACTION) + || entity.getType().equals(EntityType.ITEM_DISPLAY); } - public void remove() { - dataBase.removePixelBlock(this); - clearEntities(true); - placedPixelBlocks.remove(this); - } - - public void delete() { - this.remove(); - - Bukkit.unloadWorld(getPixelWorld(), true); - try { - FileUtils.deleteDirectory(getWorldPathFromPixelblock(this)); - } catch (IOException e) { - System.err.println("World could not be deleted: " + e.getMessage()); - } + public PixelBlockWorld getPixelWorld() { + return pixelWorld; } } diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlockInteraction.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlockInteraction.java new file mode 100644 index 0000000..19a1863 --- /dev/null +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlockInteraction.java @@ -0,0 +1,5 @@ +package eu.mhsl.minecraft.pixelblocks.pixelblock; + +public class PixelBlockInteraction { + +} diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlockWorld.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlockWorld.java new file mode 100644 index 0000000..ee9b412 --- /dev/null +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/pixelblock/PixelBlockWorld.java @@ -0,0 +1,124 @@ +package eu.mhsl.minecraft.pixelblocks.pixelblock; + +import eu.mhsl.minecraft.pixelblocks.PixelBlocksPlugin; +import eu.mhsl.minecraft.pixelblocks.chunkGenerators.EmptyChunkGenerator; +import org.bukkit.*; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.io.File; +import java.util.List; +import java.util.Objects; + +import static eu.mhsl.minecraft.pixelblocks.PixelBlocksPlugin.plugin; + +public class PixelBlockWorld { + private final PixelBlock parentPixelBlock; + private final World world; + + int worldGrassBorderWidth = PixelBlocksPlugin.configuration.worldGrassBorderWidth(); + int pixelsPerBlock = PixelBlocksPlugin.configuration.pixelsPerBlock(); + + public static boolean isPixelWorld(@NotNull World world) { + return world.getName().startsWith(plugin.getDataFolder().getPath()); + } + + public static @NotNull List getOtherWorlds() { + return Bukkit.getWorlds().stream().filter(w -> !PixelBlockWorld.isPixelWorld(w)).toList(); + } + + public static @NotNull List getPixelBlockWorlds() { + return Bukkit.getWorlds().stream().filter(PixelBlockWorld::isPixelWorld).toList(); + } + + public PixelBlockWorld(PixelBlock parentPixelBlock) { + this.parentPixelBlock = parentPixelBlock; + this.world = this.loadOrCreatePixelWorld(); + this.setBuildingPlatform(); + } + + public @NotNull String getWorldPathName() { + return PixelBlocksPlugin.plugin.getDataFolder().getPath() + File.separator + this.parentPixelBlock.blockUUID; + } + + public @NotNull Location getSpawnLocation() { + return this.getPortalLocation().add(1.5, 1, 2.5); + } + + public @NotNull Location getPortalLocation() { + return this.getBuildOrigin().add((double) pixelsPerBlock/2 -2, 0, -worldGrassBorderWidth); + } + + public @NotNull List getPlayersInWorld() { + return this.world.getPlayers(); + } + + public @NotNull List getEntitiesInWorld() { + return this.world.getEntities(); + } + + public @NotNull Location getBuildOrigin() { + return new Location(this.world, 0, -60, 0); + } + + private World loadOrCreatePixelWorld() { + final WorldCreator worldCreator = new WorldCreator(getWorldPathName()); + + worldCreator.type(WorldType.FLAT); + worldCreator.generator(new EmptyChunkGenerator()); + + World world = Bukkit.createWorld(worldCreator); + Objects.requireNonNull(world); + world.setGameRule(GameRule.RANDOM_TICK_SPEED, 0); + world.setGameRule(GameRule.DO_FIRE_TICK, false); + world.setGameRule(GameRule.DO_MOB_SPAWNING, false); + world.setGameRule(GameRule.DO_WEATHER_CYCLE, false); + world.setGameRule(GameRule.DO_VINES_SPREAD, false); + world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false); + + WorldBorder worldBorder = world.getWorldBorder(); + worldBorder.setCenter(getBuildOrigin()); + return world; + } + + private void setBuildingPlatform() { + Location borderStartLocation = getBuildOrigin().subtract(1, 1, 1); + Location grassStartLocation = borderStartLocation.clone().subtract(worldGrassBorderWidth, 0, worldGrassBorderWidth); + + Bukkit.getScheduler().runTask(PixelBlocksPlugin.plugin, () -> { + 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 < (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 < (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() + (pixelsPerBlock+1) || currentLocation.z() == borderStartLocation.z() + (pixelsPerBlock+1)) { + currentLocation.getBlock().setType(Material.RED_CONCRETE); + } + } + } + + for (int x = (2*worldGrassBorderWidth+pixelsPerBlock-1)/2; x < (2*worldGrassBorderWidth+pixelsPerBlock-1)/2+4; x++) { + for (int y = 0; y < 5; y++) { + grassStartLocation.clone().add(x, 1+y, 0).getBlock().setType(Material.OBSIDIAN); + } + } + for (int x = (2*worldGrassBorderWidth+pixelsPerBlock-1)/2+1; x < (2*worldGrassBorderWidth+pixelsPerBlock-1)/2+3; x++) { + for (int y = 1; y < 4; y++) { + grassStartLocation.clone().add(x, 1+y, 0).getBlock().setType(Material.NETHER_PORTAL); + } + } + }); + } +} diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/utils/Direction.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/utils/Direction.java new file mode 100644 index 0000000..c9f5082 --- /dev/null +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/utils/Direction.java @@ -0,0 +1,26 @@ +package eu.mhsl.minecraft.pixelblocks.utils; + +import org.bukkit.util.Vector; + +public enum Direction { + north, + east, + south, + west; + + public static Direction vectorToDirection(Vector vector) { + if(Math.abs(vector.getX()) > Math.abs(vector.getZ())) { + if(vector.getX() >= 0) { + return east; + } else { + return west; + } + } else { + if(vector.getZ() >= 0) { + return south; + } else { + return north; + } + } + } +} diff --git a/src/main/java/eu/mhsl/minecraft/pixelblocks/utils/MinMaxUtil.java b/src/main/java/eu/mhsl/minecraft/pixelblocks/utils/MinMaxUtil.java new file mode 100644 index 0000000..861752e --- /dev/null +++ b/src/main/java/eu/mhsl/minecraft/pixelblocks/utils/MinMaxUtil.java @@ -0,0 +1,14 @@ +package eu.mhsl.minecraft.pixelblocks.utils; + +import java.util.Comparator; +import java.util.List; +import java.util.function.Function; + +public class MinMaxUtil { + public static > O getMinProperty(List list, Function supplier) { + return supplier.apply(list.stream().min(Comparator.comparing(supplier)).orElseThrow()); + } + public static > O getMaxProperty(List list, Function supplier) { + return supplier.apply(list.stream().max(Comparator.comparing(supplier)).orElseThrow()); + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 57b75d8..3ec46db 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: PixelBlocks version: '${version}' -main: eu.mhsl.minecraft.pixelblocks.PixelBlocks +main: eu.mhsl.minecraft.pixelblocks.PixelBlocksPlugin api-version: '1.21' commands: createpixelblock: