Create a new event and add a few fields to existing events

This commit is contained in:
Aria 2023-03-05 15:53:56 -07:00
parent 957b65ebb3
commit ede19b0572
8 changed files with 61 additions and 11 deletions

View file

@ -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

View file

@ -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

View file

@ -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;
}
}

View file

@ -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

View file

@ -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;
}
}

View file

@ -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);

View file

@ -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)

View file

@ -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));
}
}