From e826e4c32812964f33d911909e59b71ecda8efe0 Mon Sep 17 00:00:00 2001 From: FireInstall Date: Wed, 24 Jul 2024 17:41:59 +0200 Subject: [PATCH] Remove own Position implementation, we are only compatible with paper anyway. Remove Paperlib --- .../au/com/mineauz/minigames/Minigames.java | 5 +- .../mineauz/minigames/config/RegionFlag.java | 8 +- .../mineauz/minigames/objects/MgRegion.java | 72 ++++-- .../mineauz/minigames/objects/Position.java | 216 ------------------ .../minigames/recorder/RecorderData.java | 10 +- .../com/mineauz/minigamesregions/Region.java | 2 +- 6 files changed, 69 insertions(+), 244 deletions(-) delete mode 100644 Minigames/src/main/java/au/com/mineauz/minigames/objects/Position.java diff --git a/Minigames/src/main/java/au/com/mineauz/minigames/Minigames.java b/Minigames/src/main/java/au/com/mineauz/minigames/Minigames.java index f62872d11..bf7cdde00 100644 --- a/Minigames/src/main/java/au/com/mineauz/minigames/Minigames.java +++ b/Minigames/src/main/java/au/com/mineauz/minigames/Minigames.java @@ -13,7 +13,6 @@ import au.com.mineauz.minigames.minigame.reward.RewardsModule; import au.com.mineauz.minigames.objects.MgRegion; import au.com.mineauz.minigames.objects.MinigamePlayer; -import au.com.mineauz.minigames.objects.Position; import au.com.mineauz.minigames.objects.ResourcePack; import au.com.mineauz.minigames.recorder.BasicRecorder; import au.com.mineauz.minigames.signs.SignBase; @@ -23,7 +22,6 @@ import com.google.common.io.Closeables; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.ListenableFuture; -import io.papermc.lib.PaperLib; import net.milkbowl.vault.economy.Economy; import org.bstats.bukkit.Metrics; import org.bstats.charts.CustomChart; @@ -79,7 +77,6 @@ public Minigames() { startUpHandler = new StartUpLogHandler(); //register ConfigurationSerializable - ConfigurationSerialization.registerClass(Position.class); ConfigurationSerialization.registerClass(MgRegion.class); } @@ -257,7 +254,7 @@ public void onEnable() { e.printStackTrace(); } } - PaperLib.suggestPaper(this); + log().info(desc.getName() + " successfully enabled."); this.hookPlaceHolderApi(); } catch (final Throwable e) { diff --git a/Minigames/src/main/java/au/com/mineauz/minigames/config/RegionFlag.java b/Minigames/src/main/java/au/com/mineauz/minigames/config/RegionFlag.java index 54858b814..df65e8c26 100644 --- a/Minigames/src/main/java/au/com/mineauz/minigames/config/RegionFlag.java +++ b/Minigames/src/main/java/au/com/mineauz/minigames/config/RegionFlag.java @@ -2,7 +2,7 @@ import au.com.mineauz.minigames.menu.MenuItem; import au.com.mineauz.minigames.objects.MgRegion; -import au.com.mineauz.minigames.objects.Position; +import io.papermc.paper.math.Position; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.configuration.file.FileConfiguration; @@ -42,7 +42,7 @@ public void loadValue(String path, FileConfiguration config) { setFlag(region); if (region == null) { - //todo remove next release - this was just a temporary snapshot thing. + // data fixer upper String name = config.getString(path + "." + getName() + ".name"); if (name != null) { String world = config.getString(path + "." + getName() + ".world"); @@ -58,8 +58,8 @@ public void loadValue(String path, FileConfiguration config) { double y2 = Double.parseDouble(sliptPos2[1]); double z2 = Double.parseDouble(sliptPos2[2]); - setFlag(new MgRegion(Bukkit.getWorld(world), name, new Position(x1, y1, z1), new Position(x2, y2, z2))); - } else { //todo end of snapshot code + setFlag(new MgRegion(Bukkit.getWorld(world), name, Position.fine(x1, y1, z1), Position.fine(x2, y2, z2))); + } else { // end of data fixer upper //import legacy regions from before regions existed if (legacyFistPointLabel != null && legacySecondPointLabel != null) { SimpleLocationFlag locFlag1 = new SimpleLocationFlag(null, legacyFistPointLabel); diff --git a/Minigames/src/main/java/au/com/mineauz/minigames/objects/MgRegion.java b/Minigames/src/main/java/au/com/mineauz/minigames/objects/MgRegion.java index 182def44d..badc8459a 100644 --- a/Minigames/src/main/java/au/com/mineauz/minigames/objects/MgRegion.java +++ b/Minigames/src/main/java/au/com/mineauz/minigames/objects/MgRegion.java @@ -1,10 +1,13 @@ package au.com.mineauz.minigames.objects; +import io.papermc.paper.math.BlockPosition; +import io.papermc.paper.math.Position; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.HashMap; import java.util.Map; @@ -14,17 +17,18 @@ * This is the base class for all regions in the minigames plugin and its regions & nodes addon. * It is a cuboid region defined by 2 Positions, a World and a name */ +@SuppressWarnings("UnstableApiUsage") // Position public class MgRegion implements ConfigurationSerializable { private final @NotNull String name; private @NotNull World world; - private @NotNull Position pos1; - private @NotNull Position pos2; + private @NotNull BlockPosition pos1; + private @NotNull BlockPosition pos2; public MgRegion(@NotNull World world, @NotNull String name, @NotNull Position pos1, @NotNull Position pos2) { this.name = name; this.world = world; - this.pos1 = pos1; - this.pos2 = pos2; + this.pos1 = pos1.toBlock(); + this.pos2 = pos2.toBlock(); } public MgRegion(@NotNull String name, @NotNull Location loc1, @NotNull Location loc2) { @@ -47,7 +51,7 @@ public void setWorld(@NotNull World world) { } public void setFirstPos(@NotNull Position pos1) { - this.pos1 = pos1; + this.pos1 = pos1.toBlock(); } public void setFirstPos(@NotNull Location loc1) { @@ -56,7 +60,7 @@ public void setFirstPos(@NotNull Location loc1) { } public void setSecondPos(@NotNull Position pos2) { - this.pos2 = pos2; + this.pos2 = pos2.toBlock(); } public void setSecondPos(@NotNull Location loc2) { @@ -93,10 +97,10 @@ public void updateRegion(Location loc1, Location loc2) { */ public void sortPositions() { //temporary storage to not overwrite the max values - Position pos1 = new Position(getMinX(), getMinY(), getMinZ()); + Position pos1 = Position.fine(getMinX(), getMinY(), getMinZ()); - this.pos2 = new Position(getMaxX(), getMaxY(), getMaxZ()); - this.pos1 = pos1; + this.pos2 = Position.fine(getMaxX(), getMaxY(), getMaxZ()).toBlock(); + this.pos1 = pos1.toBlock(); } public double getMinX() { @@ -164,6 +168,21 @@ public String toString() { "pos2=" + pos2 + ']'; } + /** + * Creates a Map representation of Position. + * + * @return Map containing the current state of this class + */ + private static @NotNull Map serializePosition(@NotNull Position pos) { + HashMap result = new HashMap<>(); + + result.put("x", pos.x()); + result.put("y", pos.y()); + result.put("z", pos.z()); + + return result; + } + /** * Creates a Map representation of this class. * @@ -175,12 +194,39 @@ public String toString() { result.put("name", name); result.put("world", world.getName()); - result.put("pos1", pos1.serialize()); - result.put("pos2", pos2.serialize()); + result.put("pos1", serializePosition(pos1)); + result.put("pos2", serializePosition(pos2)); return result; } + /** + * tries to recreate a Position from a map representation + * + * @return the fitting position or null if it fails. + */ + private static @Nullable Position deserializePosition(@Nullable Map map) { + if (map != null) { + Double x = null, y = null, z = null; + + if (map.get("x") instanceof Number numX) { + x = numX.doubleValue(); + } + if (map.get("y") instanceof Number numY) { + y = numY.doubleValue(); + } + if (map.get("z") instanceof Number numZ) { + z = numZ.doubleValue(); + } + + if (x != null && y != null && z != null) { + return Position.fine(x, y, z); + } + } + + return null; + } + /** * tries to recreate a MgRegion from a map representation * @@ -218,7 +264,7 @@ public static MgRegion deserialize(@NotNull Map map) { } } - pos1 = Position.deserialize(posStrMap); + pos1 = deserializePosition(posStrMap); if (pos1 == null) { throw new IllegalArgumentException("broken position 1"); @@ -236,7 +282,7 @@ public static MgRegion deserialize(@NotNull Map map) { } } - pos2 = Position.deserialize(posStrMap); + pos2 = deserializePosition(posStrMap); if (pos2 == null) { throw new IllegalArgumentException("broken position 2"); diff --git a/Minigames/src/main/java/au/com/mineauz/minigames/objects/Position.java b/Minigames/src/main/java/au/com/mineauz/minigames/objects/Position.java deleted file mode 100644 index 9b864dad6..000000000 --- a/Minigames/src/main/java/au/com/mineauz/minigames/objects/Position.java +++ /dev/null @@ -1,216 +0,0 @@ -package au.com.mineauz.minigames.objects; - -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.configuration.serialization.ConfigurationSerializable; -import org.bukkit.util.NumberConversions; -import org.bukkit.util.Vector; -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.HashMap; -import java.util.Map; - -//io.papermc.paper.math.FinePosition -//please note: This is a copy of an experimental implementation. It is most likely not compatible with later versions or even the original. -public record Position(double x, double y, double z) implements ConfigurationSerializable { - - /** - * Creates a position at the coordinates - * - * @param x x coord - * @param y y coord - * @param z z coord - * @return a position with those coords - */ - @Contract(value = "_, _, _ -> new", pure = true) - public static @NotNull Position block(int x, int y, int z) { - return new Position(x, y, z); - } - - /** - * Creates a position from the location. - * - * @param location the location to copy the position of - * @return a new position at that location - */ - @Contract(value = "_ -> new", pure = true) - public static @NotNull Position block(@NotNull Location location) { - return new Position(location.getBlockX(), location.getBlockY(), location.getBlockZ()); - } - - /** - * Creates a position at the coordinates - * - * @param x x coord - * @param y y coord - * @param z z coord - * @return a position with those coords - */ - @Contract(value = "_, _, _ -> new", pure = true) - public static @NotNull Position fine(double x, double y, double z) { - return new Position(x, y, z); - } - - /** - * Creates a position from the location. - * - * @param location the location to copy the position of - * @return a new position at that location - */ - @Contract(value = "_ -> new", pure = true) - public static @NotNull Position fine(@NotNull Location location) { - return new Position(location.getX(), location.getY(), location.getZ()); - } - - /** - * Gets the block x value for this position - * Returns: - * the block x value - */ - public int blockX() { - return NumberConversions.floor(this.x()); - } - - /** - * Gets the block x value for this position - * Returns: - * the block y value - */ - public int blockY() { - return NumberConversions.floor(this.y()); - } - - /** - * Gets the block x value for this position - * Returns: - * the block z value - */ - public int blockZ() { - return NumberConversions.floor(this.z()); - } - - /** - * Checks of this position represents a BlockPosition - * Returns: - * true if block - */ - public boolean isBlock() { - return (x % 1 == 0) && (y % 1 == 0) && (z % 1 == 0); - } - - /** - * Checks if this position represents a FinePosition - * Returns: - * true if fine - */ - public boolean isFine() { - return (x % 1 != 0) || (y % 1 != 0) || (z % 1 != 0); - } - - /** - * Returns a position offset by the specified amounts. - * Params: - * x – x value to offset y – y value to offset z – z value to offset - * Returns: - * the offset position - */ - public Position offset(int x, int y, int z) { - return this.offset((double) x, y, z); - } - - /** - * Returns a position offset by the specified amounts. - * Params: - * x – x value to offset y – y value to offset z – z value to offset - * Returns: - * the offset position - */ - public Position offset(double x, double y, double z) { - return x == 0.0 && y == 0.0 && z == 0.0 ? this : new Position(this.x() + x, this.y() + y, this.z() + z); - } - - /** - * Returns a new position at the center of the block position this represents - * Returns: - * a new center position - */ - public Position toCenter() { - return new Position(this.blockX() + 0.5, this.blockY() + 0.5, this.blockZ() + 0.5); - } - - /** - * Returns the block position of this position - * - * @return the block position - */ - @Contract(pure = true) - public @NotNull Position toBlock() { - return new Position(blockX(), blockY(), blockZ()); - } - - /** - * Converts this position to a vector - * - * @return a new vector - */ - @Contract(value = "-> new", pure = true) - public Vector toVector() { - return new Vector(this.x(), this.y(), this.z()); - } - - /** - * Creates a new location object at this position with the specified world - * - * @param world the world for the location object - * @return a new location - */ - @Contract(value = "_ -> new", pure = true) - public @NotNull Location toLocation(@NotNull World world) { - return new Location(world, this.x(), this.y(), this.z()); - } - - /** - * Creates a Map representation of this class. - * - * @return Map containing the current state of this class - */ - @Override - public @NotNull Map serialize() { - HashMap result = new HashMap<>(); - - result.put("x", this.x); - result.put("y", this.y); - result.put("z", this.z); - - return result; - } - - /** - * tries to recreate a position from a map representation - * - * @return the fitting position or null if it fails. - */ - public static @Nullable Position deserialize(@Nullable Map map) { - if (map != null) { - Double x = null, y = null, z = null; - - if (map.get("x") instanceof Number numX) { - x = numX.doubleValue(); - } - if (map.get("y") instanceof Number numY) { - y = numY.doubleValue(); - } - if (map.get("z") instanceof Number numZ) { - z = numZ.doubleValue(); - } - - if (x != null && y != null && z != null) { - return new Position(x, y, z); - } - } - - return null; - } -} diff --git a/Minigames/src/main/java/au/com/mineauz/minigames/recorder/RecorderData.java b/Minigames/src/main/java/au/com/mineauz/minigames/recorder/RecorderData.java index a19259593..317c72bc4 100644 --- a/Minigames/src/main/java/au/com/mineauz/minigames/recorder/RecorderData.java +++ b/Minigames/src/main/java/au/com/mineauz/minigames/recorder/RecorderData.java @@ -5,12 +5,10 @@ import au.com.mineauz.minigames.minigame.Minigame; import au.com.mineauz.minigames.minigame.MinigameState; import au.com.mineauz.minigames.objects.MinigamePlayer; -import au.com.mineauz.minigames.objects.Position; import com.google.common.collect.Lists; import com.google.gson.*; import com.google.gson.reflect.TypeToken; -import io.papermc.lib.PaperLib; -import io.papermc.lib.features.blockstatesnapshot.BlockStateSnapshotResult; +import io.papermc.paper.math.Position; import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.block.BlockState; @@ -31,6 +29,7 @@ import java.lang.reflect.Type; import java.util.*; +@SuppressWarnings("UnstableApiUsage") // Position public class RecorderData implements Listener { // list of blocks that need another block to not break private static final ArrayList supportedMats = new ArrayList<>(); @@ -147,8 +146,7 @@ public Minigame getMinigame() { } public MgBlockData addBlock(@NotNull Block block, @Nullable MinigamePlayer modifier) { - BlockStateSnapshotResult blockState = PaperLib.getBlockState(block, true); - return addBlock(blockState.getState(), modifier); + return addBlock(block.getState(true), modifier); } public MgBlockData addBlock(@NotNull BlockState block, @Nullable MinigamePlayer modifier) { @@ -406,7 +404,7 @@ public boolean restoreBlockData() { //todo load entity data as well throw new JsonParseException("'" + posStr + "' is not a valid position."); } - return new Position(Double.parseDouble(args[0]), Double.parseDouble(args[1]), Double.parseDouble(args[2])); //throws NumberFormatException + return Position.fine(Double.parseDouble(args[0]), Double.parseDouble(args[1]), Double.parseDouble(args[2])); //throws NumberFormatException } catch (JsonParseException | NumberFormatException exception) { exception.printStackTrace(); return null; diff --git a/Regions/src/main/java/au/com/mineauz/minigamesregions/Region.java b/Regions/src/main/java/au/com/mineauz/minigamesregions/Region.java index 31738e66b..85b646579 100644 --- a/Regions/src/main/java/au/com/mineauz/minigamesregions/Region.java +++ b/Regions/src/main/java/au/com/mineauz/minigamesregions/Region.java @@ -3,7 +3,6 @@ import au.com.mineauz.minigames.Minigames; import au.com.mineauz.minigames.objects.MgRegion; import au.com.mineauz.minigames.objects.MinigamePlayer; -import au.com.mineauz.minigames.objects.Position; import au.com.mineauz.minigames.script.ScriptCollection; import au.com.mineauz.minigames.script.ScriptReference; import au.com.mineauz.minigames.script.ScriptValue; @@ -14,6 +13,7 @@ import au.com.mineauz.minigamesregions.triggers.Trigger; import au.com.mineauz.minigamesregions.triggers.Triggers; import com.google.common.collect.ImmutableSet; +import io.papermc.paper.math.Position; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.Bukkit;