From ede19b0572f4b90dd7dba6f83d76e752348ef5f0 Mon Sep 17 00:00:00 2001 From: Aria Date: Sun, 5 Mar 2023 15:53:56 -0700 Subject: [PATCH] Create a new event and add a few fields to existing events --- gradle.properties | 2 +- .../events/ForgeEventHandlers.java | 4 +++- .../events/ProfileLoadedEvent.java | 8 ++++++- .../events/ProfileUnloadedEvent.java | 1 + .../events/ProfileUnloadingEvent.java | 19 +++++++++++++++++ .../libzontreck/profiles/Profile.java | 21 ++++++++++++++----- .../libzontreck/util/ChatHelpers.java | 3 --- .../libzontreck/util/ServerUtilities.java | 14 +++++++++++++ 8 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 src/main/java/dev/zontreck/libzontreck/events/ProfileUnloadingEvent.java create mode 100644 src/main/java/dev/zontreck/libzontreck/util/ServerUtilities.java diff --git a/gradle.properties b/gradle.properties index 43de2b0..c4bc40e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,5 +5,5 @@ org.gradle.daemon=false mc_version=1.18.2 forge_version=40.2.1 -myversion=1.0.5.0305231526 +myversion=1.0.5.0305231539 parchment_version=2022.11.06 diff --git a/src/main/java/dev/zontreck/libzontreck/events/ForgeEventHandlers.java b/src/main/java/dev/zontreck/libzontreck/events/ForgeEventHandlers.java index 38ae4f3..bf89fef 100644 --- a/src/main/java/dev/zontreck/libzontreck/events/ForgeEventHandlers.java +++ b/src/main/java/dev/zontreck/libzontreck/events/ForgeEventHandlers.java @@ -4,6 +4,7 @@ import dev.zontreck.libzontreck.LibZontreck; import dev.zontreck.libzontreck.memory.PlayerContainer; import dev.zontreck.libzontreck.profiles.Profile; import dev.zontreck.libzontreck.profiles.UserProfileNotYetExistsException; +import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent; @@ -41,8 +42,9 @@ public class ForgeEventHandlers { ServerPlayer player = (ServerPlayer)ev.getPlayer(); Profile prof = Profile.factory(player); + ServerLevel level = player.getLevel(); - MinecraftForge.EVENT_BUS.post(new ProfileLoadedEvent(prof)); + MinecraftForge.EVENT_BUS.post(new ProfileLoadedEvent(prof, player, level)); } @SubscribeEvent diff --git a/src/main/java/dev/zontreck/libzontreck/events/ProfileLoadedEvent.java b/src/main/java/dev/zontreck/libzontreck/events/ProfileLoadedEvent.java index 02c8760..fc6ba5c 100644 --- a/src/main/java/dev/zontreck/libzontreck/events/ProfileLoadedEvent.java +++ b/src/main/java/dev/zontreck/libzontreck/events/ProfileLoadedEvent.java @@ -1,13 +1,19 @@ package dev.zontreck.libzontreck.events; import dev.zontreck.libzontreck.profiles.Profile; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; import net.minecraftforge.eventbus.api.Event; public class ProfileLoadedEvent extends Event { public Profile profile; - public ProfileLoadedEvent(Profile prof) + public ServerPlayer player; + public ServerLevel level; + public ProfileLoadedEvent(Profile prof, ServerPlayer player, ServerLevel level) { profile=prof; + this.player=player; + this.level=level; } } diff --git a/src/main/java/dev/zontreck/libzontreck/events/ProfileUnloadedEvent.java b/src/main/java/dev/zontreck/libzontreck/events/ProfileUnloadedEvent.java index 95ebda3..5948721 100644 --- a/src/main/java/dev/zontreck/libzontreck/events/ProfileUnloadedEvent.java +++ b/src/main/java/dev/zontreck/libzontreck/events/ProfileUnloadedEvent.java @@ -1,5 +1,6 @@ package dev.zontreck.libzontreck.events; +import dev.zontreck.libzontreck.profiles.Profile; import net.minecraftforge.eventbus.api.Event; public class ProfileUnloadedEvent extends Event diff --git a/src/main/java/dev/zontreck/libzontreck/events/ProfileUnloadingEvent.java b/src/main/java/dev/zontreck/libzontreck/events/ProfileUnloadingEvent.java new file mode 100644 index 0000000..d4f78f3 --- /dev/null +++ b/src/main/java/dev/zontreck/libzontreck/events/ProfileUnloadingEvent.java @@ -0,0 +1,19 @@ +package dev.zontreck.libzontreck.events; + +import dev.zontreck.libzontreck.profiles.Profile; +import net.minecraft.server.level.ServerPlayer; +import net.minecraftforge.eventbus.api.Cancelable; +import net.minecraftforge.eventbus.api.Event; + +@Cancelable +public class ProfileUnloadingEvent extends Event +{ + public Profile profile; + public ServerPlayer player; + + public ProfileUnloadingEvent(Profile profile, ServerPlayer player) + { + this.profile=profile; + this.player=player; + } +} diff --git a/src/main/java/dev/zontreck/libzontreck/profiles/Profile.java b/src/main/java/dev/zontreck/libzontreck/profiles/Profile.java index c2c9327..8213aa8 100644 --- a/src/main/java/dev/zontreck/libzontreck/profiles/Profile.java +++ b/src/main/java/dev/zontreck/libzontreck/profiles/Profile.java @@ -10,7 +10,9 @@ import dev.zontreck.libzontreck.LibZontreck; import dev.zontreck.libzontreck.chat.ChatColor; import dev.zontreck.libzontreck.events.ProfileCreatedEvent; import dev.zontreck.libzontreck.events.ProfileUnloadedEvent; +import dev.zontreck.libzontreck.events.ProfileUnloadingEvent; import dev.zontreck.libzontreck.util.FileTreeDatastore; +import dev.zontreck.libzontreck.util.ServerUtilities; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.NbtIo; import net.minecraft.server.level.ServerPlayer; @@ -26,6 +28,8 @@ public class Profile { public String chat_color; public Boolean flying; public int available_vaults; + public int deaths; + public ServerPlayer player; private File accessor; @@ -44,7 +48,7 @@ public class Profile { } } - public Profile(String username, String prefix, String nickname, String name_color, String ID, String prefix_color, String chat_color, Boolean isFlying, int vaults, File vaultFile) { + public Profile(String username, String prefix, String nickname, String name_color, String ID, String prefix_color, String chat_color, Boolean isFlying, int vaults, File vaultFile, int deathCount, ServerPlayer player) { this.username = username; this.prefix = prefix; this.nickname = nickname; @@ -54,6 +58,8 @@ public class Profile { this.chat_color = chat_color; this.flying=isFlying; this.available_vaults=vaults; + this.deaths=deathCount; + this.player=player; this.accessor = vaultFile; @@ -72,7 +78,7 @@ public class Profile { // Load profile data File ace = userProfile.resolve("profile.nbt").toFile(); try { - Profile actual = load(NbtIo.read(ace), ace); + Profile actual = load(NbtIo.read(ace), ace, ServerUtilities.getPlayerByID(UUID)); LibZontreck.PROFILES.put(UUID, actual); return actual; } catch (IOException e) { @@ -94,9 +100,9 @@ public class Profile { } } - private static Profile load(CompoundTag tag, File accessor) + private static Profile load(CompoundTag tag, File accessor, ServerPlayer player) { - return new Profile(tag.getString("user"), tag.getString("prefix"), tag.getString("nick"), tag.getString("nickc"), tag.getString("id"), tag.getString("prefixc"), tag.getString("chatc"), tag.getBoolean("flying"), tag.getInt("vaults"), accessor); + return new Profile(tag.getString("user"), tag.getString("prefix"), tag.getString("nick"), tag.getString("nickc"), tag.getString("id"), tag.getString("prefixc"), tag.getString("chatc"), tag.getBoolean("flying"), tag.getInt("vaults"), accessor, tag.getInt("deaths"), player); } private static void generateNewProfile(ServerPlayer player) @@ -107,7 +113,7 @@ public class Profile { { // Load profile data File ace = userProfile.resolve("profile.nbt").toFile(); - Profile template = new Profile(player.getName().getString(), "Member", player.getDisplayName().getString(), ChatColor.GREEN, player.getStringUUID(), ChatColor.AQUA, ChatColor.WHITE, false, 0, ace); + Profile template = new Profile(player.getName().getString(), "Member", player.getDisplayName().getString(), ChatColor.GREEN, player.getStringUUID(), ChatColor.AQUA, ChatColor.WHITE, false, 0, ace, 0, player); template.commit(); @@ -129,6 +135,10 @@ public class Profile { @Override public void finalize() { + if(MinecraftForge.EVENT_BUS.post(new ProfileUnloadingEvent(this, player))) + { + commit(); + } LibZontreck.LOGGER.info("Profile is unloaded for "+username); MinecraftForge.EVENT_BUS.post(new ProfileUnloadedEvent(user_id)); } @@ -162,6 +172,7 @@ public class Profile { serial.putString("chatc", chat_color); serial.putBoolean("flying", flying); serial.putInt("vaults", available_vaults); + serial.putInt("deaths", deaths); diff --git a/src/main/java/dev/zontreck/libzontreck/util/ChatHelpers.java b/src/main/java/dev/zontreck/libzontreck/util/ChatHelpers.java index 5703678..86d7623 100644 --- a/src/main/java/dev/zontreck/libzontreck/util/ChatHelpers.java +++ b/src/main/java/dev/zontreck/libzontreck/util/ChatHelpers.java @@ -9,9 +9,6 @@ import net.minecraft.network.chat.TextComponent; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.player.Player; -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; -import net.minecraftforge.network.NetworkHooks; public class ChatHelpers { public static void broadcastActionBar(Component message, MinecraftServer server) diff --git a/src/main/java/dev/zontreck/libzontreck/util/ServerUtilities.java b/src/main/java/dev/zontreck/libzontreck/util/ServerUtilities.java new file mode 100644 index 0000000..3efcf78 --- /dev/null +++ b/src/main/java/dev/zontreck/libzontreck/util/ServerUtilities.java @@ -0,0 +1,14 @@ +package dev.zontreck.libzontreck.util; + +import java.util.UUID; + +import dev.zontreck.libzontreck.LibZontreck; +import net.minecraft.server.level.ServerPlayer; + +public class ServerUtilities +{ + public static ServerPlayer getPlayerByID(String id) + { + return LibZontreck.THE_SERVER.getPlayerList().getPlayer(UUID.fromString(id)); + } +} \ No newline at end of file