Skip to content

Commit

Permalink
use MgRegions own (de)Serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
FireInstall committed Jun 11, 2024
1 parent d1f778f commit a719c0c
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

public class MinigameUtils {
Expand Down Expand Up @@ -51,48 +52,17 @@ public static String getItemStackName(ItemStack item) {
* displayed.
*
* @param time - The time in seconds to be converted
* @param small - If the time should be shortened to: hh:mm:ss
* @param small - If the time should be shortened to: ww:dd:hh:mm:ss
* @return A message with a neat time
*/
public static String convertTime(int time, boolean small) {
int weeks = 0;
int days = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;
int rtime = time;
public static String convertTime(long time, boolean small) {
long weeks = TimeUnit.SECONDS.toDays(time) / 7;
long days = TimeUnit.SECONDS.toDays(time) % 7;
long hours = TimeUnit.SECONDS.toHours(time) % 24;
long minutes = TimeUnit.SECONDS.toMinutes(time) % 60;
long seconds = time % 60;
String msg = "";

if (time > 604800) {
weeks = rtime / 604800;
rtime = rtime - weeks * 604800;
days = rtime / 86400;
rtime = rtime - days * 86400;
hours = rtime / 3600;
rtime = rtime - hours * 3600;
minutes = rtime / 60;
rtime = rtime - minutes * 60;
seconds = rtime;
} else if (time > 86400) {
days = rtime / 86400;
rtime = rtime - days * 86400;
hours = rtime / 3600;
rtime = rtime - hours * 3600;
minutes = rtime / 60;
rtime = rtime - minutes * 60;
seconds = rtime;
} else if (time > 3600) {
hours = rtime / 3600;
rtime = rtime - hours * 3600;
minutes = rtime / 60;
seconds = rtime - minutes * 60;
} else if (time > 60) {
minutes = time / 60;
seconds = rtime - minutes * 60;
} else {
seconds = time;
}

if (weeks != 0) {
if (!small)
msg = String.format(getLang("time.weeks"), weeks);
Expand Down Expand Up @@ -316,7 +286,6 @@ public static boolean isMinigameTool(@Nullable ItemStack item) {

//was not in hands, search in inventory.
for (ItemStack item : player.getPlayer().getInventory().getContents()) {
;
if (isMinigameTool(item)) {
return new MinigameTool(item);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import au.com.mineauz.minigames.mechanics.TreasureHuntMechanic;
import au.com.mineauz.minigames.minigame.Minigame;
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 Down Expand Up @@ -75,6 +77,10 @@ public Minigames() {
super();
log = this.getLogger();
startUpHandler = new StartUpLogHandler();

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

protected Minigames(final JavaPluginLoader loader, final PluginDescriptionFile description, final File dataFolder, final File file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,41 @@ public RegionFlag(MgRegion value, String name) {

@Override
public void saveValue(String path, FileConfiguration config) {
config.set(path + "." + getName() + ".name", getFlag().getName());
config.set(path + "." + getName() + ".world", getFlag().getWorld().getName());
config.set(path + "." + getName() + ".pos1", getFlag().getPos1().x() + ":" + getFlag().getPos1().y() + ":" + getFlag().getPos1().z());
config.set(path + "." + getName() + ".pos2", getFlag().getPos2().x() + ":" + getFlag().getPos2().y() + ":" + getFlag().getPos2().z());
config.set(path + "." + getName(), getFlag() == null ? null : getFlag());
}

@Override
public void loadValue(String path, FileConfiguration config) {
String name = config.getString(path + "." + getName() + ".name");
if (name != null) {
String world = config.getString(path + "." + getName() + ".world");

String[] sliptPos1 = config.getString(path + "." + getName() + ".pos1").split(":");
String[] sliptPos2 = config.getString(path + "." + getName() + ".pos2").split(":");

double x1 = Double.parseDouble(sliptPos1[0]);
double y1 = Double.parseDouble(sliptPos1[1]);
double z1 = Double.parseDouble(sliptPos1[2]);

double x2 = Double.parseDouble(sliptPos2[0]);
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 {
//import legacy regions from before regions existed
if (legacyFistPointLabel != null && legacySecondPointLabel != null) {
SimpleLocationFlag locFlag1 = new SimpleLocationFlag(null, legacyFistPointLabel);
SimpleLocationFlag locFlag2 = new SimpleLocationFlag(null, legacySecondPointLabel);

if (locFlag1.getFlag() != null && locFlag2.getFlag() != null) {
setFlag(new MgRegion("legacy", locFlag1.getFlag(), locFlag2.getFlag()));
MgRegion region = config.getObject(path + "." + getName(), MgRegion.class);
setFlag(region);

if (region == null) {
//todo remove next release - this was just a temporary snapshot thing.
String name = config.getString(path + "." + getName() + ".name");
if (name != null) {
String world = config.getString(path + "." + getName() + ".world");

String[] sliptPos1 = config.getString(path + "." + getName() + ".pos1").split(":");
String[] sliptPos2 = config.getString(path + "." + getName() + ".pos2").split(":");

double x1 = Double.parseDouble(sliptPos1[0]);
double y1 = Double.parseDouble(sliptPos1[1]);
double z1 = Double.parseDouble(sliptPos1[2]);

double x2 = Double.parseDouble(sliptPos2[0]);
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
//import legacy regions from before regions existed
if (legacyFistPointLabel != null && legacySecondPointLabel != null) {
SimpleLocationFlag locFlag1 = new SimpleLocationFlag(null, legacyFistPointLabel);
SimpleLocationFlag locFlag2 = new SimpleLocationFlag(null, legacySecondPointLabel);

if (locFlag1.getFlag() != null && locFlag2.getFlag() != null) {
setFlag(new MgRegion("legacy", locFlag1.getFlag(), locFlag2.getFlag()));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package au.com.mineauz.minigames.objects;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* 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
*/
public class MgRegion {
public class MgRegion implements ConfigurationSerializable {
private final @NotNull String name;
private @NotNull World world;
private @NotNull Position pos1;
Expand Down Expand Up @@ -159,4 +163,88 @@ public String toString() {
"pos1=" + pos1 + ", " +
"pos2=" + pos2 + ']';
}

/**
* Creates a Map representation of this class.
*
* @return Map containing the current state of this class
*/
@Override
public @NotNull Map<String, Object> serialize() {
HashMap<String, Object> result = new HashMap<>();

result.put("name", name);
result.put("world", world.getName());
result.put("pos1", pos1.serialize());
result.put("pos2", pos2.serialize());

return result;
}

/**
* tries to recreate a MgRegion from a map representation
*
* @return the fitting MgRegion or null if it fails.
* @see ConfigurationSerializable
*/
@NotNull
public static MgRegion deserialize(@NotNull Map<String, Object> map) {
String name;
World world;
Position pos1, pos2;

if (map.get("name") instanceof String temp) {
name = temp;
} else {
throw new IllegalArgumentException("no name");
}

if (map.get("world") instanceof String worldName) {
world = Bukkit.getWorld(worldName);

if (world == null) {
throw new IllegalArgumentException("unknown world");
}
} else {
throw new IllegalArgumentException("no world");
}

if (map.get("pos1") instanceof Map<?, ?> posObjMap) {
HashMap<String, Object> posStrMap = new HashMap<>();

for (Map.Entry<?, ?> objEntry : posObjMap.entrySet()) {
if (objEntry.getKey() instanceof String key) {
posStrMap.put(key, objEntry.getValue());
}
}

pos1 = Position.deserialize(posStrMap);

if (pos1 == null) {
throw new IllegalArgumentException("broken position 1");
}
} else {
throw new IllegalArgumentException("no position 1");
}

if (map.get("pos2") instanceof Map<?, ?> posObjMap2) {
HashMap<String, Object> posStrMap = new HashMap<>();

for (Map.Entry<?, ?> objEntry : posObjMap2.entrySet()) {
if (objEntry.getKey() instanceof String key) {
posStrMap.put(key, objEntry.getValue());
}
}

pos2 = Position.deserialize(posStrMap);

if (pos2 == null) {
throw new IllegalArgumentException("broken position 2");
}
} else {
throw new IllegalArgumentException("no position 2");
}

return new MgRegion(world, name, pos1, pos2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

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) {
public record Position(double x, double y, double z) implements ConfigurationSerializable {

/**
* Creates a position at the coordinates
Expand Down Expand Up @@ -165,4 +170,47 @@ public Vector toVector() {
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<String, Object> serialize() {
HashMap<String, Object> 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<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 new Position(x, y, z);
}
}

return null;
}
}
Loading

0 comments on commit a719c0c

Please sign in to comment.