Skip to content

Commit

Permalink
Remove own Position implementation, we are only compatible with paper…
Browse files Browse the repository at this point in the history
… anyway.

Remove Paperlib
  • Loading branch information
FireInstall committed Jul 24, 2024
1 parent 6f031bd commit e826e4c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 244 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -79,7 +77,6 @@ public Minigames() {
startUpHandler = new StartUpLogHandler();

//register ConfigurationSerializable
ConfigurationSerialization.registerClass(Position.class);
ConfigurationSerialization.registerClass(MgRegion.class);
}

Expand Down Expand Up @@ -257,7 +254,7 @@ public void onEnable() {
e.printStackTrace();
}
}
PaperLib.suggestPaper(this);

log().info(desc.getName() + " successfully enabled.");
this.hookPlaceHolderApi();
} catch (final Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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<String, Object> serializePosition(@NotNull Position pos) {
HashMap<String, Object> 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.
*
Expand All @@ -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<String, Object> 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
*
Expand Down Expand Up @@ -218,7 +264,7 @@ public static MgRegion deserialize(@NotNull Map<String, Object> map) {
}
}

pos1 = Position.deserialize(posStrMap);
pos1 = deserializePosition(posStrMap);

if (pos1 == null) {
throw new IllegalArgumentException("broken position 1");
Expand All @@ -236,7 +282,7 @@ public static MgRegion deserialize(@NotNull Map<String, Object> map) {
}
}

pos2 = Position.deserialize(posStrMap);
pos2 = deserializePosition(posStrMap);

if (pos2 == null) {
throw new IllegalArgumentException("broken position 2");
Expand Down
Loading

0 comments on commit e826e4c

Please sign in to comment.