Skip to content

Commit

Permalink
Massive improvements on loading player models
Browse files Browse the repository at this point in the history
  • Loading branch information
duranaaron committed Jan 27, 2024
1 parent 0f360bd commit 200e8e3
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/main/java/cc/ddev/feather/api/API.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cc.ddev.feather.api;

import cc.ddev.feather.api.economy.EconomyManager;
import cc.ddev.feather.api.playerdata.PlayerManager;
import cc.ddev.feather.configuration.ConfigManager;
import cc.ddev.feather.api.sidebar.SidebarManager;
import cc.ddev.instanceguard.InstanceGuard;
Expand All @@ -15,6 +16,9 @@ public class API {
@Getter
public static EconomyManager economy = EconomyManager.getInstance();

@Getter
public static PlayerManager playerManager = PlayerManager.getInstance();

@Getter
public static InstanceGuard instanceGuard = new InstanceGuard();

Expand Down
91 changes: 85 additions & 6 deletions src/main/java/cc/ddev/feather/api/playerdata/PlayerManager.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,110 @@
package cc.ddev.feather.api.playerdata;

import cc.ddev.feather.api.API;
import cc.ddev.feather.api.config.Config;
import cc.ddev.feather.api.economy.EconomyManager;
import cc.ddev.feather.database.StormDatabase;
import cc.ddev.feather.database.models.PlayerModel;
import cc.ddev.feather.logger.Log;
import cc.ddev.feather.player.PlayerProfile;
import cc.ddev.feather.player.PlayerWrapper;
import net.minestom.server.MinecraftServer;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.Player;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public class PlayerManager {

public static Map<UUID, Point> plotWandPos1Map = new HashMap<>();
public static Map<UUID, Point> plotWandPos2Map = new HashMap<>();
private static PlayerManager instance;

public static void setPlotWandPos1(UUID uuid, Point point) {
public static PlayerManager getInstance() {
if (instance == null) {
instance = new PlayerManager();
}
return instance;
}

public Map<UUID, Point> plotWandPos1Map = new HashMap<>();
public Map<UUID, Point> plotWandPos2Map = new HashMap<>();

public void setPlotWandPos1(UUID uuid, Point point) {
plotWandPos1Map.remove(uuid);
plotWandPos1Map.put(uuid, point);
}

public static void setPlotWandPos2(UUID uuid, Point point) {
public void setPlotWandPos2(UUID uuid, Point point) {
plotWandPos2Map.remove(uuid);
plotWandPos2Map.put(uuid, point);
}

public static Point getPlotWandPos1(UUID uuid) {
public Point getPlotWandPos1(UUID uuid) {
return plotWandPos1Map.get(uuid);
}

public static Point getPlotWandPos2(UUID uuid) {
public Point getPlotWandPos2(UUID uuid) {
return plotWandPos2Map.get(uuid);
}

public PlayerProfile getPlayerProfile(Player player) {
PlayerProfile playerProfile = PlayerWrapper.getPlayerProfile(player);
if (playerProfile == null) {
player.kick("Failed to load player profile!");
return null;
}
return playerProfile;
}

public PlayerModel getPlayerModel(Player player) {
CompletableFuture<PlayerModel> future = StormDatabase.getInstance().loadPlayerModel(player.getUuid());
PlayerModel playerModel;

try {
// Prevent not loading the player model in time
future.get();

// Load player profile
PlayerProfile playerProfile = getPlayerProfile(player);

// Load player model
playerModel = playerProfile.getPlayerModel();
if (playerModel == null) {
player.kick("Failed to load player model!");
return null;
}
} catch (Exception e) {
Log.getLogger().info("Failed to load player profile and model for " + player.getUsername() + "!");
e.printStackTrace();
return null;
}
return playerModel;
}

public Pos getLastLocation(Player player) {
if (player == null) {
return null;
}
PlayerModel playerModel = getPlayerModel(player);
if (playerModel == null) {
return null;
}
// Set the spawn position
if (playerModel.getLastLocation() != null) {
// Extract X, Y and Z from the position string
String[] rawPosition = playerModel.getLastLocation().split(",");
double x = Double.parseDouble(rawPosition[0].replace("Pos[x=", ""));
double y = Double.parseDouble(rawPosition[1].replace("y=", ""));
double z = Double.parseDouble(rawPosition[2].replace("z=", ""));
float yaw = Float.parseFloat(rawPosition[3].replace("yaw=", ""));
float pitch = Float.parseFloat(rawPosition[4].replace("pitch=", "").replace("]", ""));
Pos pos = new Pos(x, y, z, yaw, pitch);

return pos;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public PlotCreateCommand() {
addSyntax((sender, context) -> {
Player player = (Player) sender;
UUID uuid = player.getUuid();
Point pos1 = PlayerManager.getPlotWandPos1(uuid);
Point pos2 = PlayerManager.getPlotWandPos2(uuid);
Point pos1 = API.getPlayerManager().getPlotWandPos1(uuid);
Point pos2 = API.getPlayerManager().getPlotWandPos2(uuid);
PlayerProfile playerProfile = PlayerWrapper.getPlayerProfile(player);
String plotName = context.get(plotNameArgument);
boolean topToBottom = context.get(topToBottomArgument);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/cc/ddev/feather/database/StormDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import lombok.Getter;
import lombok.Setter;
import net.minestom.server.MinecraftServer;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.timer.Scheduler;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -98,6 +99,7 @@ public CompletableFuture<PlayerModel> loadPlayerModel(UUID uuid) {
createdModel.setBalance(Config.Economy.STARTING_BALANCE);
createdModel.setLevel(0);
createdModel.setIsOperator(false);
createdModel.setLastLocation(new Pos(Config.Spawn.COORDS).toString());
PlayerWrapper.playerModels.put(uuid, createdModel);
completableFuture.complete(createdModel);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cc.ddev.feather.listener.player;

import cc.ddev.feather.api.API;
import cc.ddev.feather.api.config.Config;
import cc.ddev.feather.api.playerdata.PlayerManager;
import cc.ddev.feather.listener.handler.Listen;
Expand All @@ -21,11 +22,11 @@ public void onPlayerBlockBreak(PlayerBlockBreakEvent event) {
UUID uuid = player.getUuid();
if (player.getItemInMainHand().material() == Config.Plot.PLOTWAND.material()
&& player.getItemInMainHand().getDisplayName() == Config.Plot.PLOTWAND.getDisplayName()) {
PlayerManager.setPlotWandPos1(player.getUuid(), event.getBlockPosition());
API.getPlayerManager().setPlotWandPos1(player.getUuid(), event.getBlockPosition());
player.sendMessage(ChatUtils.translateMiniMessage("<green>Position 1 set to <dark_green>"
+ PlayerManager.getPlotWandPos1(uuid).blockX()
+ ", " + PlayerManager.getPlotWandPos1(uuid).blockY()
+ ", " + PlayerManager.getPlotWandPos1(uuid).blockZ()));
+ API.getPlayerManager().getPlotWandPos1(uuid).blockX()
+ ", " + API.getPlayerManager().getPlotWandPos1(uuid).blockY()
+ ", " + API.getPlayerManager().getPlotWandPos1(uuid).blockZ()));
event.setCancelled(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public void onPlayerBlockInteract(PlayerBlockInteractEvent event) {
if (player.getInstance() == null) return;

if (player.getItemInMainHand().equals(Config.Plot.PLOTWAND)) {
PlayerManager.setPlotWandPos2(uuid, event.getBlockPosition());
API.getPlayerManager().setPlotWandPos2(uuid, event.getBlockPosition());
player.sendMessage(ChatUtils.translateMiniMessage("<green>Position 2 set to <dark_green>"
+ PlayerManager.getPlotWandPos2(uuid).blockX()
+ ", " + PlayerManager.getPlotWandPos2(uuid).blockY()
+ ", " + PlayerManager.getPlotWandPos2(uuid).blockZ()));
+ API.getPlayerManager().getPlotWandPos2(uuid).blockX()
+ ", " + API.getPlayerManager().getPlotWandPos2(uuid).blockY()
+ ", " + API.getPlayerManager().getPlotWandPos2(uuid).blockZ()));
event.setCancelled(true);
}
}
Expand Down

0 comments on commit 200e8e3

Please sign in to comment.