Skip to content

Commit

Permalink
Doing more java stuffz, very wip, it will brick your save!
Browse files Browse the repository at this point in the history
Improved how I use classes and added alot of get & set methods to make
it more like normal java lol. Finished storing the deathLocation in memory. Storage seems to currently be fully bricked tho, gonna need to look into that.
  • Loading branch information
MrSn0wy committed Jan 7, 2025
1 parent b601828 commit c2d278c
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 113 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### [W.I.P]
- Storage is loaded in memory instead of reading it again and again, Improves speed and IO usage
- Made it so the DeathLocation is only kept in memory (WIP)
- Storage is loaded in memory instead of reading it again and again, Improves speed and IO usage. (W.I.P)
- Adding comments to alot of code (W.I.P)
- Made it so the DeathLocation is only kept in memory
- Improved the Storage classes and functions (I'm doing proper java, yipie)

### [v1.2.2]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mojang.datafixers.util.Pair;
import dev.mrsnowy.teleport_commands.storage.StorageManager;
import dev.mrsnowy.teleport_commands.commands.*;
import dev.mrsnowy.teleport_commands.storage.backListStorage;
import net.minecraft.commands.Commands;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
Expand All @@ -17,6 +18,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

import net.minecraft.core.BlockPos;

import static dev.mrsnowy.teleport_commands.storage.StorageManager.*;
Expand Down Expand Up @@ -57,23 +59,12 @@ public static void initializeMod(MinecraftServer server) {

// Runs when the playerDeath mixin calls it, updates the /back command position
public static void onPlayerDeath(ServerPlayer player) {
try {
BlockPos pos = new BlockPos(player.getBlockX(), player.getBlockY(), player.getBlockZ());

Pair<StorageManager.StorageClass, StorageManager.StorageClass.Player> storages = GetPlayerStorage(player.getStringUUID());

StorageManager.StorageClass.Player playerStorage = storages.getSecond();

playerStorage.deathLocation.x = pos.getX();
playerStorage.deathLocation.y = pos.getY();
playerStorage.deathLocation.z = pos.getZ();
playerStorage.deathLocation.world = player.serverLevel().dimension().location().toString();
BlockPos pos = new BlockPos(player.getBlockX(), player.getBlockY(), player.getBlockZ());
String world = player.serverLevel().dimension().location().toString();
String uuid = player.getStringUUID();

StorageSaver();

} catch (Exception e) {
LOGGER.error("Error while saving the player death location! => ", e);
}
backListStorage.backList backList = backListStorage.backList;
backList.setDeathLocation(uuid, pos, world);
}

// private static StorageManager.StorageClass loadStorage() throws Exception {
Expand All @@ -83,7 +74,7 @@ public static void onPlayerDeath(ServerPlayer player) {
// String jsonContent = Files.readString(STORAGE_FILE);
// Gson gson = new GsonBuilder().create();
//
//}
// }

// cleans and updates Storage to the newest "version". This is painful
private static StorageClass storageValidator() {
Expand Down Expand Up @@ -155,15 +146,6 @@ private static StorageClass storageValidator() {
String DefaultHome = player.has("DefaultHome")
? player.get("DefaultHome").getAsString() : "";


// Clean death location after server restart
JsonObject deathLocation = new JsonObject();

deathLocation.addProperty("x", 0);
deathLocation.addProperty("y", 0);
deathLocation.addProperty("z", 0);
deathLocation.addProperty("world", "");

JsonArray homes = new JsonArray();

if (player.has("Homes") && player.get("Homes").isJsonArray() ) {
Expand Down Expand Up @@ -226,7 +208,6 @@ private static StorageClass storageValidator() {

newPlayer.addProperty("UUID", UUID);
newPlayer.addProperty("DefaultHome", DefaultHome);
newPlayer.add("deathLocationClass", deathLocation);
newPlayer.add("Homes", homes);

newPlayersArray.add(newPlayer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.*;

import dev.mrsnowy.teleport_commands.storage.backListStorage;
import dev.mrsnowy.teleport_commands.utils.tools;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.Commands;
Expand All @@ -19,20 +20,6 @@
import static net.minecraft.commands.Commands.argument;

public class back {
public static final ArrayList<deathLocationClass> backList = new ArrayList<>();

public static class deathLocationClass {
final public String UUID;
final public BlockPos pos;
final public String world;

public deathLocationClass(String uuid, BlockPos pos, String world) {
this.UUID = uuid;
this.pos = pos;
this.world = world;
backList.add(this);
}
}

public static void register(Commands commandManager) {

Expand Down Expand Up @@ -73,26 +60,29 @@ public static void register(Commands commandManager) {

private static void ToDeathLocation(ServerPlayer player, boolean safetyDisabled) {

Optional<deathLocationClass> OptionalDeathLocation = backList.stream()
.filter( deathLocation -> Objects.equals( deathLocation.UUID, player.getStringUUID() ))
.findFirst();
backListStorage.backList backList = backListStorage.backList;

if (OptionalDeathLocation.isEmpty()) {
player.displayClientMessage(getTranslatedText("commands.teleport_commands.common.noLocation", player).withStyle(ChatFormatting.RED), true);
// get the deathLocation
Optional<backListStorage.deathLocationClass> optionalDeathLocation = backList.getDeathLocation( player.getStringUUID() );
if (optionalDeathLocation.isEmpty()) {
player.displayClientMessage(getTranslatedText("commands.teleport_commands.common.noLocation", player)
.withStyle(ChatFormatting.RED), true);
return;
}

deathLocationClass deathLocation = OptionalDeathLocation.get();
backListStorage.deathLocationClass deathLocation = optionalDeathLocation.get();

// get the world
Optional<ServerLevel> OptionalWorld = tools.getWorld( deathLocation.world );
if (OptionalWorld.isEmpty()) {
player.displayClientMessage(getTranslatedText("commands.teleport_commands.common.noLocation", player).withStyle(ChatFormatting.RED), true);
player.displayClientMessage(getTranslatedText("commands.teleport_commands.common.noLocation", player)
.withStyle(ChatFormatting.RED), true);
return;
}

ServerLevel world = OptionalWorld.get();

// check if the death location isn't safe and that safety isn't enabled
// if safety is enabled, check if the death location is safe.
if (!safetyDisabled) {

Pair<Integer, Optional<Vec3>> teleportData = teleportSafetyChecker(deathLocation.pos, world, player);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mojang.datafixers.util.Pair;
import dev.mrsnowy.teleport_commands.TeleportCommands;
import net.minecraft.core.BlockPos;

Expand Down Expand Up @@ -48,59 +47,61 @@ public static void StorageInit() {
}
}

public static StorageClass.Player PlayerAdd(String UUID) {

// try to find an exising storage for this player
Optional<StorageClass.Player> playerStorage = STORAGE.Players.stream()
.filter(player -> Objects.equals(UUID, player.UUID))
.findFirst();

if (playerStorage.isEmpty()) {
StorageClass.Player newPlayer = new StorageClass.Player(UUID); // TODO! verify that it creates the player proper

List<StorageClass.Player> playerList = STORAGE.Players;
playerList.add(newPlayer);

// StorageSaver(); // no need to save since no data is actually set yet!
TeleportCommands.LOGGER.info("Player '{}' added successfully in storage!", UUID);
return newPlayer;
} else {
TeleportCommands.LOGGER.info("Player '{}' already exists!", UUID);
return playerStorage.get();
}
}
// public static StorageClass.Player PlayerAdd(String UUID) {
//
// // try to find an exising storage for this player
// Optional<StorageClass.Player> playerStorage = STORAGE.Players.stream()
// .filter(player -> Objects.equals(UUID, player.UUID))
// .findFirst();
//
// if (playerStorage.isEmpty()) {
// StorageClass.Player newPlayer = new StorageClass.Player(UUID); // TODO! verify that it creates the player proper
//
// List<StorageClass.Player> playerList = STORAGE.Players;
// playerList.add(newPlayer);
//
//// StorageSaver(); // no need to save since no data is actually set yet!
// TeleportCommands.LOGGER.info("Player '{}' added successfully in storage!", UUID);
// return newPlayer;
// } else {
// TeleportCommands.LOGGER.info("Player '{}' already exists!", UUID);
// return playerStorage.get();
// }
// }

public static void StorageSaver() throws Exception {
Gson gson = new GsonBuilder().create();
byte[] json = gson.toJson( STORAGE ).getBytes();

Files.write(STORAGE_FILE, json, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
}
TeleportCommands.LOGGER.info(STORAGE.toString());

public static Pair<StorageClass, List<StorageClass.NamedLocation>> getWarpStorage() {
return new Pair<>(STORAGE, STORAGE.Warps);
Files.write(STORAGE_FILE, json, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
}

public static Pair<StorageClass, StorageClass.Player> GetPlayerStorage(String UUID) {

// try to find an exising storage for this player
Optional<StorageClass.Player> playerStorage = STORAGE.Players.stream()
.filter(player -> Objects.equals(UUID, player.UUID))
.findFirst();

if (playerStorage.isEmpty()) {
StorageClass.Player player = PlayerAdd(UUID); // create a new player

return new Pair<>(STORAGE, player);
}

return new Pair<>(STORAGE, playerStorage.get());
}
// public static Pair<StorageClass, List<StorageClass.NamedLocation>> getWarpStorage() {
// return new Pair<>(STORAGE, STORAGE.Warps);
// }

// public static Pair<StorageClass, StorageClass.Player> GetPlayerStorage(String UUID) {
//
// // try to find an exising storage for this player
// Optional<StorageClass.Player> playerStorage = STORAGE.Players.stream()
// .filter(player -> Objects.equals(UUID, player.UUID))
// .findFirst();
//
// if (playerStorage.isEmpty()) {
// StorageClass.Player player = PlayerAdd(UUID); // create a new player
//
// return new Pair<>(STORAGE, player);
// }
//
// return new Pair<>(STORAGE, playerStorage.get());
// }


public static class StorageClass {
public static List<NamedLocation> Warps = new ArrayList<>();
public static List<Player> Players = new ArrayList<>();
public static warpList Warps = new warpList();
public static playerList Players = new playerList();

public static class NamedLocation {
public String name;
Expand All @@ -118,22 +119,109 @@ public NamedLocation(String name, BlockPos pos, String world) {
}
}

public static class Location {
public int x = 0;
public int y = 0;
public int z = 0;
public String world = "";
public static class warpList {
private final ArrayList<NamedLocation> warpList = new ArrayList<>();

// filters the warpList and finds the one with the name (if there is one)
public Optional<NamedLocation> getWarp(String name) {
return warpList.stream()
.filter( warp -> Objects.equals( warp.name, name ))
.findFirst();
}

// returns all warps
public ArrayList<NamedLocation> getWarps() {
return warpList;
}

// creates a new warp, if there already is a warp it will update the existing one
public void setWarp(String name, BlockPos pos, String world) throws Exception {
Optional<NamedLocation> OptionalWarp = getWarp(name);

if (OptionalWarp.isEmpty()) {
// create a new warp
NamedLocation warp = new NamedLocation(name, pos, world);
warpList.add(warp);
StorageSaver();
} else {
// modify existing warp
NamedLocation warp = OptionalWarp.get();
warp.name = name;
}
}
}

public static class playerList {
private final ArrayList<Player> playerList = new ArrayList<>();

// filters the playerList and finds the one with the uuid (if there is one)
public Optional<Player> getPlayer(String uuid) {
return playerList.stream()
.filter( player -> Objects.equals( player.UUID, uuid ))
.findFirst();
}

// creates a new player, if there already is a player it will return the existing one
public Player addPlayer(String uuid, BlockPos pos, String world) {
Optional<Player> OptionalPlayer = getPlayer(uuid);

if (OptionalPlayer.isEmpty()) {
// create new player
Player player = new Player(uuid);
playerList.add(player);
TeleportCommands.LOGGER.info("Player '{}' added successfully in storage!", uuid);

return player;
} else {
// return existing player
TeleportCommands.LOGGER.info("Player '{}' already exists!", uuid);
return OptionalPlayer.get();
}
}
}


public static class Player {
public final String UUID;
public String DefaultHome = "";
public Location deathLocation; // todo! deprecate
public List<NamedLocation> Homes = new ArrayList<>();
public homeList Homes = new homeList();

public Player(String uuid) {
this.UUID = uuid;
}

public static class homeList {
private final List<NamedLocation> Homes = new ArrayList<>();

// filters the Homes and finds the one with the name (if there is one)
public Optional<NamedLocation> getHome(String name) {
return Homes.stream()
.filter( home -> Objects.equals( home.name, name ))
.findFirst();
}

// returns all homes
public List<NamedLocation> getHomes() {
return Homes;
}

// creates a new home, if there already is a home it will update the existing one
public void setHome(String name, BlockPos pos, String world) throws Exception {
Optional<NamedLocation> OptionalHome = getHome(name);

if (OptionalHome.isEmpty()) {
NamedLocation home = new NamedLocation(name, pos, world);

Homes.add(home);
StorageSaver();
} else {
NamedLocation home = OptionalHome.get();

home.name = name;
StorageSaver();
}
}
}
}
}
}
Loading

0 comments on commit c2d278c

Please sign in to comment.