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

View File

@ -17,15 +17,16 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.logging.Logger;
public final class Main extends JavaPlugin {
public static Main plugin;
public static PixelBlockConfiguration configuration;
public static PixelBlockDatabase database;
private static Main plugin;
private static PixelBlockConfiguration configuration;
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() {
return taskFactory.newChain();
@ -98,4 +99,20 @@ public final class Main extends JavaPlugin {
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;
}
}

View File

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

View File

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

View File

@ -13,7 +13,7 @@ public class BreakPixelBlockListener implements Listener {
public void destroyPixelBlock(PrePlayerAttackEntityEvent event) {
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);
if(pixelBlock == null) return;
pixelBlock.destroy(event.getPlayer());

View File

@ -20,7 +20,7 @@ public class DiscoverRecipesListener implements Listener {
if(!(event.getWhoClicked() instanceof Player player)) return;
if(!List.of(Material.HEART_OF_THE_SEA, Material.END_CRYSTAL).contains(clickedItem.getType())) 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);
}
}

View File

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

View File

@ -75,9 +75,9 @@ public class PixelBlock {
this.placeholder = new PixelBlockPlaceholder(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) {
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()
.async(() -> {
Main.database.savePixelBlock(this);
Main.database().savePixelBlock(this);
Main.pixelBlocks.add(this);
})
.execute();
@ -120,7 +120,7 @@ public class PixelBlock {
}
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));
return;
}
@ -128,13 +128,13 @@ public class PixelBlock {
getBlockTaskChain()
.async(() -> {
this.lastEntryLocation = player.getLocation();
Main.database.savePixelBlock(this);
Main.database().savePixelBlock(this);
})
.sync(() -> {
if(!exists) return;
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())))
.execute();
}
@ -142,7 +142,7 @@ public class PixelBlock {
public void exitBlock(@NotNull Player player) {
this.getBlockTaskChain()
.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))
.execute();
@ -170,7 +170,7 @@ public class PixelBlock {
// @Nullable PixelBlock blockAtLocation = PixelBlock.getPixelBlockFromPlacedLocation(newLocation);
// 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.facingDirection = direction;
// updateEntities();
@ -181,7 +181,7 @@ public class PixelBlock {
public void destroy(Player destroyedBy) {
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!");
return;
}
@ -191,7 +191,7 @@ public class PixelBlock {
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.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));
})
.async(() -> {
Main.database.deletePixelBlock(this);
Main.database().deletePixelBlock(this);
Main.pixelBlocks.remove(this);
})
.execute();

View File

@ -12,7 +12,7 @@ import java.util.List;
import java.util.Objects;
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;
@ -25,8 +25,8 @@ public class PixelBlockHitbox {
List<PixelBlockWorld.PixelData> pixels = this.parentBlock.getPixelData();
double scale = pixels.getFirst().scale();
float offset = (float) Main.configuration.hitboxOffset();
int pixelsPerBlock = Main.configuration.pixelsPerBlock();
float offset = (float) Main.configuration().hitboxOffset();
int pixelsPerBlock = Main.configuration().pixelsPerBlock();
Interaction interaction;
if(true) {

View File

@ -16,7 +16,7 @@ import org.bukkit.util.Transformation;
import java.util.*;
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;

View File

@ -17,17 +17,15 @@ import java.util.List;
import java.util.Objects;
import java.util.Random;
import static eu.mhsl.minecraft.pixelblocks.Main.plugin;
public class PixelBlockWorld {
private final PixelBlock parentPixelBlock;
private final World world;
int worldGrassBorderWidth = 10;
int pixelsPerBlock = Main.configuration.pixelsPerBlock();
int pixelsPerBlock = Main.configuration().pixelsPerBlock();
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() {
@ -53,7 +51,7 @@ public class PixelBlockWorld {
}
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() {
@ -146,7 +144,7 @@ public class PixelBlockWorld {
}
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 z = 0; z < (pixelsPerBlock+2) + 2 * worldGrassBorderWidth; z++) {
getPlatformOrigin().add(x, 0, z).getBlock().setType(Material.GRASS_BLOCK);

View File

@ -15,7 +15,7 @@ import java.util.List;
import java.util.Objects;
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;