further variable encapsulation

This commit is contained in:
Elias Müller 2024-10-15 20:37:36 +02:00
parent b8bcb5e580
commit 796bee9696
11 changed files with 50 additions and 35 deletions

@ -17,15 +17,16 @@ import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.logging.Logger;
public final class Main extends JavaPlugin { public final class Main extends JavaPlugin {
public static Main plugin; private static Main plugin;
public static PixelBlockConfiguration configuration; private static PixelBlockConfiguration configuration;
public static PixelBlockDatabase database; private static PixelBlockDatabase database;
public static TaskChainFactory taskFactory; private static TaskChainFactory taskFactory;
public static List<PixelBlock> pixelBlocks = new ArrayList<>(); public final static List<PixelBlock> pixelBlocks = new ArrayList<>();
public static <T> TaskChain<T> chain() { public static <T> TaskChain<T> chain() {
return taskFactory.newChain(); return taskFactory.newChain();
@ -98,4 +99,20 @@ public final class Main extends JavaPlugin {
throw new RuntimeException("Failed disabling", e); throw new RuntimeException("Failed disabling", e);
} }
} }
public static Main plugin() {
return plugin;
}
public static Logger logger() {
return plugin.getLogger();
}
public static PixelBlockDatabase database() {
return database;
}
public static PixelBlockConfiguration configuration() {
return configuration;
}
} }

@ -58,7 +58,7 @@ public class PixelBlockDatabase {
} }
public void deletePixelBlock(PixelBlock pixelBlock) { public void deletePixelBlock(PixelBlock pixelBlock) {
Bukkit.getScheduler().runTaskAsynchronously(Main.plugin, () -> { Bukkit.getScheduler().runTaskAsynchronously(Main.plugin(), () -> {
try { try {
this.deletePixelBlock.setString(1, pixelBlock.getBlockUUID().toString()); this.deletePixelBlock.setString(1, pixelBlock.getBlockUUID().toString());
this.deletePixelBlock.executeUpdate(); this.deletePixelBlock.executeUpdate();
@ -69,7 +69,7 @@ public class PixelBlockDatabase {
} }
public void savePixelBlock(PixelBlock pixelBlock) { public void savePixelBlock(PixelBlock pixelBlock) {
Bukkit.getScheduler().runTask(Main.plugin, () -> { Bukkit.getScheduler().runTask(Main.plugin(), () -> {
try { try {
this.insertOrReplacePixelBlock.setString(1, pixelBlock.getBlockUUID().toString()); this.insertOrReplacePixelBlock.setString(1, pixelBlock.getBlockUUID().toString());
this.insertOrReplacePixelBlock.setString(2, pixelBlock.getOwnerUUID().toString()); this.insertOrReplacePixelBlock.setString(2, pixelBlock.getOwnerUUID().toString());

@ -22,9 +22,9 @@ import java.util.Optional;
import java.util.UUID; import java.util.UUID;
public class PixelBlockItem { public class PixelBlockItem {
public static final NamespacedKey recipeKey = new NamespacedKey(Main.plugin, "pixelblock"); public static final NamespacedKey recipeKey = new NamespacedKey(Main.plugin(), "pixelblock");
public static NamespacedKey idProperty = new NamespacedKey(Main.plugin, "id"); public static NamespacedKey idProperty = new NamespacedKey(Main.plugin(), "id");
public static NamespacedKey ownerProperty = new NamespacedKey(Main.plugin, "owner"); public static NamespacedKey ownerProperty = new NamespacedKey(Main.plugin(), "owner");
public static final String itemTexture = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYzE5NGU5ZTc3NTdkMDZkNmY1ZTViZTg0NTQ4YTdjYjUyMTczZDY4Y2NmODAyZDIxMTI3NWQzMWNkYmEwYTA2ZSJ9fX0="; public static final String itemTexture = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYzE5NGU5ZTc3NTdkMDZkNmY1ZTViZTg0NTQ4YTdjYjUyMTczZDY4Y2NmODAyZDIxMTI3NWQzMWNkYmEwYTA2ZSJ9fX0=";

@ -13,7 +13,7 @@ public class BreakPixelBlockListener implements Listener {
public void destroyPixelBlock(PrePlayerAttackEntityEvent event) { public void destroyPixelBlock(PrePlayerAttackEntityEvent event) {
if(!(event.getAttacked() instanceof Interaction)) return; if(!(event.getAttacked() instanceof Interaction)) return;
Location blockLocation = event.getAttacked().getLocation().add(0, Main.configuration.hitboxOffset(), 0).toBlockLocation(); Location blockLocation = event.getAttacked().getLocation().add(0, Main.configuration().hitboxOffset(), 0).toBlockLocation();
PixelBlock pixelBlock = PixelBlock.getPixelBlockFromPlacedLocation(blockLocation); PixelBlock pixelBlock = PixelBlock.getPixelBlockFromPlacedLocation(blockLocation);
if(pixelBlock == null) return; if(pixelBlock == null) return;
pixelBlock.destroy(event.getPlayer()); pixelBlock.destroy(event.getPlayer());

@ -20,7 +20,7 @@ public class DiscoverRecipesListener implements Listener {
if(!(event.getWhoClicked() instanceof Player player)) return; if(!(event.getWhoClicked() instanceof Player player)) return;
if(!List.of(Material.HEART_OF_THE_SEA, Material.END_CRYSTAL).contains(clickedItem.getType())) return; if(!List.of(Material.HEART_OF_THE_SEA, Material.END_CRYSTAL).contains(clickedItem.getType())) return;
if(player.hasDiscoveredRecipe(PixelBlockItem.recipeKey)) return; if(player.hasDiscoveredRecipe(PixelBlockItem.recipeKey)) return;
Main.plugin.getLogger().log(Level.INFO, String.format("%s unlocked tne PixelBlock recipe!", player.getName())); Main.logger().log(Level.INFO, String.format("%s unlocked tne PixelBlock recipe!", player.getName()));
player.discoverRecipe(PixelBlockItem.recipeKey); player.discoverRecipe(PixelBlockItem.recipeKey);
} }
} }

@ -16,7 +16,7 @@ public class EnterPixelBlockListener implements Listener {
Location interactionLocation = event Location interactionLocation = event
.getRightClicked() .getRightClicked()
.getLocation() .getLocation()
.add(0, Main.configuration.hitboxOffset(), 0) .add(0, Main.configuration().hitboxOffset(), 0)
.toBlockLocation(); .toBlockLocation();
PixelBlock pixelBlock = PixelBlock.getPixelBlockFromPlacedLocation(interactionLocation); PixelBlock pixelBlock = PixelBlock.getPixelBlockFromPlacedLocation(interactionLocation);
if(pixelBlock == null) return; if(pixelBlock == null) return;

@ -75,9 +75,9 @@ public class PixelBlock {
this.placeholder = new PixelBlockPlaceholder(this); this.placeholder = new PixelBlockPlaceholder(this);
this.hitbox = new PixelBlockHitbox(this); this.hitbox = new PixelBlockHitbox(this);
Main.plugin.getLogger().info(String.format("Loaded existing pixelblock '%s'", this.blockUUID)); Main.logger().info(String.format("Loaded existing pixelblock '%s'", this.blockUUID));
} catch(Exception e) { } catch(Exception e) {
Main.plugin.getLogger().info(String.format("Failed initializing existing pixelblock '%s': %s", this.blockUUID, e.getMessage())); Main.logger().info(String.format("Failed initializing existing pixelblock '%s': %s", this.blockUUID, e.getMessage()));
} }
} }
@ -109,7 +109,7 @@ public class PixelBlock {
this.getBlockTaskChain() this.getBlockTaskChain()
.async(() -> { .async(() -> {
Main.database.savePixelBlock(this); Main.database().savePixelBlock(this);
Main.pixelBlocks.add(this); Main.pixelBlocks.add(this);
}) })
.execute(); .execute();
@ -120,7 +120,7 @@ public class PixelBlock {
} }
public void enterBlock(@NotNull Player player) { public void enterBlock(@NotNull Player player) {
if(Main.configuration.onlyEditableByOwner() && !player.getUniqueId().equals(ownerUUID)) { if(Main.configuration().onlyEditableByOwner() && !player.getUniqueId().equals(ownerUUID)) {
player.sendMessage(Component.text("Dieser Pixelblock gehört nicht dir!", NamedTextColor.RED)); player.sendMessage(Component.text("Dieser Pixelblock gehört nicht dir!", NamedTextColor.RED));
return; return;
} }
@ -128,13 +128,13 @@ public class PixelBlock {
getBlockTaskChain() getBlockTaskChain()
.async(() -> { .async(() -> {
this.lastEntryLocation = player.getLocation(); this.lastEntryLocation = player.getLocation();
Main.database.savePixelBlock(this); Main.database().savePixelBlock(this);
}) })
.sync(() -> { .sync(() -> {
if(!exists) return; if(!exists) return;
player.teleport(this.pixelWorld.getSpawnLocation()); player.teleport(this.pixelWorld.getSpawnLocation());
}) })
.current(() -> Main.plugin.getLogger() .current(() -> Main.logger()
.info(String.format("'%s' entered PixelBlock '%s' at %s", player.getName(), this.blockUUID, this.pixelBlockLocation.toString()))) .info(String.format("'%s' entered PixelBlock '%s' at %s", player.getName(), this.blockUUID, this.pixelBlockLocation.toString())))
.execute(); .execute();
} }
@ -142,7 +142,7 @@ public class PixelBlock {
public void exitBlock(@NotNull Player player) { public void exitBlock(@NotNull Player player) {
this.getBlockTaskChain() this.getBlockTaskChain()
.sync(() -> player.teleport(this.lastEntryLocation != null ? this.lastEntryLocation : this.pixelBlockLocation)) .sync(() -> player.teleport(this.lastEntryLocation != null ? this.lastEntryLocation : this.pixelBlockLocation))
.current(() -> Main.plugin.getLogger().info(String.format("%s exited PixelBlock", player.getName()))) .current(() -> Main.logger().info(String.format("%s exited PixelBlock", player.getName())))
.sync(() -> this.pixelData = this.pixelWorld.getPixels(this.facingDirection)) .sync(() -> this.pixelData = this.pixelWorld.getPixels(this.facingDirection))
.execute(); .execute();
@ -170,7 +170,7 @@ public class PixelBlock {
// @Nullable PixelBlock blockAtLocation = PixelBlock.getPixelBlockFromPlacedLocation(newLocation); // @Nullable PixelBlock blockAtLocation = PixelBlock.getPixelBlockFromPlacedLocation(newLocation);
// if(blockAtLocation != null && blockAtLocation != this) throw new IllegalArgumentException("Es können nicht mehrere Pixelblöcke ineinander platziert werden."); // if(blockAtLocation != null && blockAtLocation != this) throw new IllegalArgumentException("Es können nicht mehrere Pixelblöcke ineinander platziert werden.");
// //
// Main.plugin.getLogger().info(String.format("Placing PixelBlock '%s' at %s", this.blockUUID, placeLocation)); // Main.logger().info(String.format("Placing PixelBlock '%s' at %s", this.blockUUID, placeLocation));
// this.pixelBlockLocation = newLocation; // this.pixelBlockLocation = newLocation;
// this.facingDirection = direction; // this.facingDirection = direction;
// updateEntities(); // updateEntities();
@ -181,7 +181,7 @@ public class PixelBlock {
public void destroy(Player destroyedBy) { public void destroy(Player destroyedBy) {
if(!this.exists) return; if(!this.exists) return;
if(Main.configuration.onlyBreakableByOwner() && !destroyedBy.getUniqueId().equals(ownerUUID)) { if(Main.configuration().onlyBreakableByOwner() && !destroyedBy.getUniqueId().equals(ownerUUID)) {
destroyedBy.sendMessage("Dieser Pixelblock gehört nicht dir!"); destroyedBy.sendMessage("Dieser Pixelblock gehört nicht dir!");
return; return;
} }
@ -191,7 +191,7 @@ public class PixelBlock {
p.teleport(this.lastEntryLocation); p.teleport(this.lastEntryLocation);
}); });
Main.plugin.getLogger().info(String.format("Destroying PixelBlock '%s' at %s", this.blockUUID, pixelBlockLocation)); Main.logger().info(String.format("Destroying PixelBlock '%s' at %s", this.blockUUID, pixelBlockLocation));
this.exists = false; this.exists = false;
this.pixelWorld.getEntitiesInWorld().stream() this.pixelWorld.getEntitiesInWorld().stream()
@ -206,7 +206,7 @@ public class PixelBlock {
world.dropItem(this.pixelBlockLocation.add(new Vector(0.5, 0.5, 0.5)), PixelBlockItem.getBlockAsItem(this)); world.dropItem(this.pixelBlockLocation.add(new Vector(0.5, 0.5, 0.5)), PixelBlockItem.getBlockAsItem(this));
}) })
.async(() -> { .async(() -> {
Main.database.deletePixelBlock(this); Main.database().deletePixelBlock(this);
Main.pixelBlocks.remove(this); Main.pixelBlocks.remove(this);
}) })
.execute(); .execute();

@ -12,7 +12,7 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
public class PixelBlockHitbox { public class PixelBlockHitbox {
private static final NamespacedKey hitboxOfTag = new NamespacedKey(Main.plugin, "hitbox_of"); private static final NamespacedKey hitboxOfTag = new NamespacedKey(Main.plugin(), "hitbox_of");
private final PixelBlock parentBlock; private final PixelBlock parentBlock;
@ -25,8 +25,8 @@ public class PixelBlockHitbox {
List<PixelBlockWorld.PixelData> pixels = this.parentBlock.getPixelData(); List<PixelBlockWorld.PixelData> pixels = this.parentBlock.getPixelData();
double scale = pixels.getFirst().scale(); double scale = pixels.getFirst().scale();
float offset = (float) Main.configuration.hitboxOffset(); float offset = (float) Main.configuration().hitboxOffset();
int pixelsPerBlock = Main.configuration.pixelsPerBlock(); int pixelsPerBlock = Main.configuration().pixelsPerBlock();
Interaction interaction; Interaction interaction;
if(true) { if(true) {

@ -16,7 +16,7 @@ import org.bukkit.util.Transformation;
import java.util.*; import java.util.*;
public class PixelBlockPlaceholder { public class PixelBlockPlaceholder {
private static final NamespacedKey placeholderOfTag = new NamespacedKey(Main.plugin, "placeholder_of"); private static final NamespacedKey placeholderOfTag = new NamespacedKey(Main.plugin(), "placeholder_of");
private final PixelBlock parentBlock; private final PixelBlock parentBlock;

@ -17,17 +17,15 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Random; import java.util.Random;
import static eu.mhsl.minecraft.pixelblocks.Main.plugin;
public class PixelBlockWorld { public class PixelBlockWorld {
private final PixelBlock parentPixelBlock; private final PixelBlock parentPixelBlock;
private final World world; private final World world;
int worldGrassBorderWidth = 10; int worldGrassBorderWidth = 10;
int pixelsPerBlock = Main.configuration.pixelsPerBlock(); int pixelsPerBlock = Main.configuration().pixelsPerBlock();
public static boolean isPixelWorld(@NotNull World world) { public static boolean isPixelWorld(@NotNull World world) {
return world.getName().startsWith(plugin.getDataFolder().getPath()); return world.getName().startsWith(Main.plugin().getDataFolder().getPath());
} }
public static @NotNull List<World> getOtherWorlds() { public static @NotNull List<World> getOtherWorlds() {
@ -53,7 +51,7 @@ public class PixelBlockWorld {
} }
public @NotNull String getWorldPathName() { public @NotNull String getWorldPathName() {
return Main.plugin.getDataFolder().getPath() + File.separator + "worlds" + File.separator + this.parentPixelBlock.getBlockUUID(); return Main.plugin().getDataFolder().getPath() + File.separator + "worlds" + File.separator + this.parentPixelBlock.getBlockUUID();
} }
public @NotNull Location getSpawnLocation() { public @NotNull Location getSpawnLocation() {
@ -146,7 +144,7 @@ public class PixelBlockWorld {
} }
private void setBuildingPlatform() { private void setBuildingPlatform() {
Bukkit.getScheduler().runTask(Main.plugin, () -> { Bukkit.getScheduler().runTask(Main.plugin(), () -> {
for (int x = 0; x < (pixelsPerBlock+2) + 2 * worldGrassBorderWidth; x++) { for (int x = 0; x < (pixelsPerBlock+2) + 2 * worldGrassBorderWidth; x++) {
for (int z = 0; z < (pixelsPerBlock+2) + 2 * worldGrassBorderWidth; z++) { for (int z = 0; z < (pixelsPerBlock+2) + 2 * worldGrassBorderWidth; z++) {
getPlatformOrigin().add(x, 0, z).getBlock().setType(Material.GRASS_BLOCK); getPlatformOrigin().add(x, 0, z).getBlock().setType(Material.GRASS_BLOCK);

@ -15,7 +15,7 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
public class Pixels { public class Pixels {
private static final NamespacedKey pixelOfTag = new NamespacedKey(Main.plugin, "pixel_of"); private static final NamespacedKey pixelOfTag = new NamespacedKey(Main.plugin(), "pixel_of");
private final @NotNull PixelBlock parentBlock; private final @NotNull PixelBlock parentBlock;