Fully migrate homes commands

This commit is contained in:
Aria 2023-02-27 16:12:39 -07:00
parent f27c76f334
commit 603952e185
15 changed files with 99 additions and 80 deletions

View file

@ -12,4 +12,4 @@ my_modid="ariasessentials"
mc_version=1.18.2
forge_version=40.2.1
parchment_version=2022.11.06
libz_version=1.0.5.3
libz_version=1.0.5.4

View file

@ -1,9 +1,18 @@
package dev.zontreck.essentials;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.slf4j.Logger;
import com.mojang.logging.LogUtils;
import dev.zontreck.essentials.homes.Homes;
import dev.zontreck.essentials.homes.HomesProvider;
import dev.zontreck.libzontreck.events.ProfileLoadedEvent;
import dev.zontreck.libzontreck.events.ProfileUnloadedEvent;
import dev.zontreck.libzontreck.profiles.Profile;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.server.ServerStartedEvent;
import net.minecraftforge.event.server.ServerStoppingEvent;
@ -17,6 +26,7 @@ public class AriasEssentials {
public static final String MODID = "ariasessentials";
public static final Logger LOGGER = LogUtils.getLogger();
public static boolean ALIVE;
public static Map<UUID, Homes> player_homes = new HashMap<>();
public AriasEssentials()
{
@ -40,4 +50,15 @@ public class AriasEssentials {
}
@SubscribeEvent
public void onProfileLoaded(ProfileLoadedEvent ev)
{
player_homes.put(UUID.fromString(ev.profile.user_id), HomesProvider.getHomesForPlayer(ev.profile.user_id));
}
@SubscribeEvent
public void onProfileUnloaded(ProfileUnloadedEvent ev)
{
player_homes.remove(UUID.fromString(ev.user_id));
}
}

View file

@ -3,8 +3,11 @@ package dev.zontreck.essentials.commands.homes;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import dev.zontreck.essentials.AriasEssentials;
import dev.zontreck.essentials.Messages;
import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.libzontreck.profiles.Profile;
import dev.zontreck.libzontreck.util.ChatHelpers;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.server.level.ServerPlayer;
@ -30,15 +33,16 @@ public class DelHomeCommand {
// if(homeName==null)return 0;
try{
ServerPlayer p = ctx.getPlayerOrException();
Profile prof = Profile.get_profile_of(p.getStringUUID());
prof.player_homes.delete(homeName);
ChatServerOverride.broadcastTo(p.getUUID(), new TextComponent(OTEMod.OTEPrefix + ChatColor.doColors("!dark_green! Home was deleted successfully")), ctx.getServer());
AriasEssentials.player_homes.get(p.getUUID()).delete(homeName);
ChatHelpers.broadcastTo(p, new TextComponent(Messages.ESSENTIALS_PREFIX + ChatColor.doColors("!dark_green!Home was deleted successfully")), ctx.getServer());
}catch(Exception e)
{
e.printStackTrace();
ChatServerOverride.broadcastTo(ctx.getEntity().getUUID(), new TextComponent(OTEMod.OTEPrefix + ChatColor.doColors("!dark_red! Home could not be deleted due to an unknown error")), ctx.getServer());
ChatHelpers.broadcastTo(ctx.getEntity().getUUID(), new TextComponent(Messages.ESSENTIALS_PREFIX + ChatColor.doColors("!dark_red!Home could not be deleted due to an unknown error")), ctx.getServer());
}
return 0;
}

View file

@ -1,19 +1,19 @@
package dev.zontreck.otemod.commands.homes;
package dev.zontreck.essentials.commands.homes;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.zontreck.essentials.AriasEssentials;
import dev.zontreck.essentials.Messages;
import dev.zontreck.essentials.commands.teleport.TeleportActioner;
import dev.zontreck.essentials.commands.teleport.TeleportContainer;
import dev.zontreck.essentials.commands.teleport.TeleportDestination;
import dev.zontreck.essentials.homes.Home;
import dev.zontreck.essentials.homes.NoSuchHomeException;
import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.chat.ChatServerOverride;
import dev.zontreck.otemod.commands.teleport.TeleportActioner;
import dev.zontreck.otemod.commands.teleport.TeleportContainer;
import dev.zontreck.otemod.database.TeleportDestination;
import dev.zontreck.otemod.implementation.homes.Home;
import dev.zontreck.otemod.implementation.homes.NoSuchHomeException;
import dev.zontreck.otemod.implementation.profiles.Profile;
import dev.zontreck.otemod.implementation.profiles.UserProfileNotYetExistsException;
import dev.zontreck.libzontreck.profiles.UserProfileNotYetExistsException;
import dev.zontreck.libzontreck.util.ChatHelpers;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.server.level.ServerPlayer;
@ -39,15 +39,14 @@ public class HomeCommand {
// if(homeName==null)return 0;
try{
ServerPlayer p = ctx.getPlayerOrException();
Profile prof = Profile.get_profile_of(p.getStringUUID());
Home home = prof.player_homes.get(homeName);
Home home = AriasEssentials.player_homes.get(p.getUUID()).get(homeName);
TeleportDestination dest = home.destination;
TeleportActioner.ApplyTeleportEffect(p);
TeleportContainer cont = new TeleportContainer(p, dest.Position.asMinecraftVector(), dest.Rotation.asMinecraftVector(), dest.getActualDimension());
TeleportActioner.PerformTeleport(cont);
ChatServerOverride.broadcastTo(p.getUUID(), new TextComponent(OTEMod.OTEPrefix + ChatColor.doColors(" !dark_green!Home found! Wormhole opening now...")), ctx.getServer());
ChatHelpers.broadcastTo(p.getUUID(), new TextComponent(Messages.ESSENTIALS_PREFIX + ChatColor.doColors("!dark_green!Home found! Wormhole opening now...")), ctx.getServer());
}catch(CommandSyntaxException e)
{
e.printStackTrace();
@ -55,11 +54,8 @@ public class HomeCommand {
}catch(NoSuchHomeException e)
{
ChatServerOverride.broadcastTo(ctx.getEntity().getUUID(), new TextComponent(OTEMod.OTEPrefix + ChatColor.doColors(" !dark_red!Home not found. Maybe it does not exist?")), ctx.getServer());
ChatHelpers.broadcastTo(ctx.getEntity().getUUID(), new TextComponent(Messages.ESSENTIALS_PREFIX + ChatColor.doColors(" !dark_red!Home not found. Maybe it does not exist?")), ctx.getServer());
return 0;
} catch (UserProfileNotYetExistsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

View file

@ -1,4 +1,4 @@
package dev.zontreck.otemod.commands.homes;
package dev.zontreck.essentials.commands.homes;
import java.sql.Connection;
import java.sql.ResultSet;
@ -11,15 +11,14 @@ import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.zontreck.essentials.AriasEssentials;
import dev.zontreck.essentials.Messages;
import dev.zontreck.essentials.homes.Home;
import dev.zontreck.essentials.homes.Homes;
import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.libzontreck.chat.Clickable;
import dev.zontreck.libzontreck.chat.HoverTip;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.chat.ChatServerOverride;
import dev.zontreck.otemod.implementation.homes.Home;
import dev.zontreck.otemod.implementation.homes.Homes;
import dev.zontreck.otemod.implementation.profiles.Profile;
import dev.zontreck.otemod.implementation.profiles.UserProfileNotYetExistsException;
import dev.zontreck.libzontreck.util.ChatHelpers;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.server.level.ServerPlayer;
@ -39,24 +38,21 @@ public class HomesCommand {
try{
ServerPlayer player = ctx.getSource().getPlayerOrException();
Profile p = Profile.get_profile_of(player.getStringUUID());
Homes homes = p.player_homes;
Homes homes = AriasEssentials.player_homes.get(player.getUUID());
ChatServerOverride.broadcastTo(player.getUUID(), new TextComponent(OTEMod.OTEPrefix + ChatColor.doColors(" !Dark_Purple!There are !gold!"+String.valueOf(homes.count())+" !dark_purple!total homes.")), player.server);
ChatHelpers.broadcastTo(player.getUUID(), new TextComponent(Messages.ESSENTIALS_PREFIX + ChatColor.doColors(" !Dark_Purple!There are !gold!"+String.valueOf(homes.count())+" !dark_purple!total homes.")), player.server);
for (Home string : homes.getList()) {
Style st = Style.EMPTY.withFont(Style.DEFAULT_FONT).withHoverEvent(HoverTip.get(ChatColor.BOLD+ChatColor.DARK_GREEN+"Click here to go to this home")).withClickEvent(Clickable.command("/home "+string.homeName));
ChatServerOverride.broadcastTo(player.getUUID(), new TextComponent(ChatColor.BOLD + ChatColor.MINECOIN_GOLD+"["+ChatColor.resetChat()+ChatColor.UNDERLINE+ChatColor.BOLD+ChatColor.DARK_GREEN+"HOME"+ChatColor.resetChat()+ChatColor.BOLD+ChatColor.MINECOIN_GOLD+"] "+ChatColor.resetChat()+ChatColor.YELLOW+string).setStyle(st), ctx.getSource().getServer());
ChatHelpers.broadcastTo(player.getUUID(), new TextComponent(ChatColor.BOLD + ChatColor.MINECOIN_GOLD+"["+ChatColor.resetChat()+ChatColor.UNDERLINE+ChatColor.BOLD+ChatColor.DARK_GREEN+"HOME"+ChatColor.resetChat()+ChatColor.BOLD+ChatColor.MINECOIN_GOLD+"] "+ChatColor.resetChat()+ChatColor.YELLOW+string).setStyle(st), ctx.getSource().getServer());
}
}catch(CommandSyntaxException ex)
{
ex.printStackTrace();
} catch (UserProfileNotYetExistsException e) {
e.printStackTrace();
}
return 0;

View file

@ -1,4 +1,4 @@
package dev.zontreck.otemod.commands.homes;
package dev.zontreck.essentials.commands.homes;
import java.sql.Connection;
import java.sql.PreparedStatement;
@ -8,17 +8,14 @@ import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.zontreck.essentials.AriasEssentials;
import dev.zontreck.essentials.Messages;
import dev.zontreck.essentials.commands.teleport.TeleportDestination;
import dev.zontreck.essentials.homes.Home;
import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.libzontreck.util.ChatHelpers;
import dev.zontreck.libzontreck.vectors.Vector2;
import dev.zontreck.libzontreck.vectors.Vector3;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.chat.ChatServerOverride;
import dev.zontreck.otemod.database.TeleportDestination;
import dev.zontreck.otemod.implementation.homes.Home;
import dev.zontreck.otemod.implementation.homes.Homes;
import dev.zontreck.otemod.implementation.homes.HomesProvider;
import dev.zontreck.otemod.implementation.profiles.Profile;
import dev.zontreck.otemod.implementation.profiles.UserProfileNotYetExistsException;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.server.level.ServerPlayer;
@ -55,18 +52,13 @@ public class SetHomeCommand {
TeleportDestination dest = new TeleportDestination(new Vector3(position), new Vector2(rot), p.getLevel());
Home newhome = new Home(p, homeName, dest);
Profile profile = Profile.get_profile_of(p.getStringUUID());
Homes homes = profile.player_homes;
homes.add(newhome);
AriasEssentials.player_homes.get(p.getUUID()).add(newhome);
ChatServerOverride.broadcastTo(p.getUUID(), new TextComponent(OTEMod.OTEPrefix + ChatColor.doColors(" !dark_green!Home was created or updated successfully!")), ctx.getServer());
ChatHelpers.broadcastTo(p.getUUID(), new TextComponent(Messages.ESSENTIALS_PREFIX + ChatColor.doColors(" !dark_green!Home was created or updated successfully!")), ctx.getServer());
} catch (CommandSyntaxException e) {
ChatServerOverride.broadcastTo(ctx.getEntity().getUUID(), new TextComponent(OTEMod.OTEPrefix + ChatColor.doColors(" !dark_red!Home could not be created or updated!")), ctx.getServer());
e.printStackTrace();
} catch (UserProfileNotYetExistsException e) {
ChatServerOverride.broadcastTo(ctx.getEntity().getUUID(), new TextComponent(OTEMod.OTEPrefix + ChatColor.doColors(" !dark_red!Home could not be created or updated!")), ctx.getServer());
ChatHelpers.broadcastTo(ctx.getEntity().getUUID(), new TextComponent(Messages.ESSENTIALS_PREFIX + ChatColor.doColors(" !dark_red!Home could not be created or updated!")), ctx.getServer());
e.printStackTrace();
}

View file

@ -1,4 +1,4 @@
package dev.zontreck.otemod.commands.warps;
package dev.zontreck.essentials.commands.warps;
import java.sql.Connection;
import java.sql.PreparedStatement;
@ -13,12 +13,6 @@ import com.mojang.brigadier.CommandDispatcher;
import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.libzontreck.chat.Clickable;
import dev.zontreck.libzontreck.chat.HoverTip;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.chat.ChatServerOverride;
import dev.zontreck.otemod.implementation.profiles.Profile;
import dev.zontreck.otemod.implementation.profiles.UserProfileNotYetExistsException;
import dev.zontreck.otemod.implementation.warps.Warp;
import dev.zontreck.otemod.implementation.warps.WarpsProvider;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.ClickEvent;

View file

@ -2,6 +2,7 @@ package dev.zontreck.essentials.homes;
import java.util.UUID;
import dev.zontreck.essentials.commands.teleport.TeleportDestination;
import dev.zontreck.libzontreck.exceptions.InvalidDeserialization;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerPlayer;

View file

@ -1,16 +1,16 @@
package dev.zontreck.otemod.implementation.homes;
package dev.zontreck.essentials.homes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.implementation.events.HomeCreatedEvent;
import dev.zontreck.otemod.implementation.events.HomeDeletedEvent;
import dev.zontreck.essentials.events.HomeCreatedEvent;
import dev.zontreck.essentials.events.HomeDeletedEvent;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraftforge.common.MinecraftForge;
public class Homes {
private Map<String, Home> homes = new HashMap<>();
@ -44,16 +44,16 @@ public class Homes {
HomeDeletedEvent e = new HomeDeletedEvent(home);
homes.remove(name);
OTEMod.bus.post(e);
MinecraftForge.EVENT_BUS.post(e);
HomesProvider.commitHomes(this);
}
public void add(Home toAdd)
{
HomeCreatedEvent hce = new HomeCreatedEvent(toAdd);
OTEMod.bus.post(hce);
homes.put(toAdd.homeName, toAdd);
if(!MinecraftForge.EVENT_BUS.post(hce))
homes.put(toAdd.homeName, toAdd);
HomesProvider.commitHomes(this);
}

View file

@ -1,10 +1,10 @@
package dev.zontreck.otemod.implementation.homes;
package dev.zontreck.essentials.homes;
import java.io.IOException;
import java.nio.file.Path;
import java.util.UUID;
import dev.zontreck.otemod.implementation.profiles.Profile;
import dev.zontreck.essentials.util.EssentialsDatastore;
import net.minecraft.nbt.NbtIo;
public class HomesProvider {
@ -16,7 +16,7 @@ public class HomesProvider {
*/
public static Homes getHomesForPlayer(String player)
{
Path homesFile = Profile.BASE.resolve(player).resolve("homes.nbt");
Path homesFile = EssentialsDatastore.of(player).resolve("homes.nbt");
Homes homes = new Homes(player);
if(homesFile.toFile().exists())
@ -37,7 +37,7 @@ public class HomesProvider {
public static void commitHomes(Homes playerHomes)
{
Path homesFile = Profile.BASE.resolve(playerHomes.playerID).resolve("homes.nbt");
Path homesFile = EssentialsDatastore.of(playerHomes.playerID).resolve("homes.nbt");
try {
NbtIo.write(playerHomes.serialize(), homesFile.toFile());

View file

@ -1,4 +1,4 @@
package dev.zontreck.otemod.implementation.homes;
package dev.zontreck.essentials.homes;
public class NoSuchHomeException extends Exception{

View file

@ -0,0 +1,18 @@
package dev.zontreck.essentials.util;
import java.nio.file.Path;
import dev.zontreck.libzontreck.util.FileTreeDatastore;
public class EssentialsDatastore extends FileTreeDatastore
{
public static final Path AEBASE;
static{
AEBASE = FileTreeDatastore.of("essentials");
}
public static Path of(String path)
{
return AEBASE.resolve(path);
}
}

View file

@ -1,4 +1,4 @@
package dev.zontreck.otemod.implementation.warps;
package dev.zontreck.essentials.warps;
public class NoSuchWarpException extends Exception
{

View file

@ -1,4 +1,4 @@
package dev.zontreck.otemod.implementation.warps;
package dev.zontreck.essentials.warps;
import java.util.ArrayList;
import java.util.HashMap;
@ -7,9 +7,6 @@ import java.util.List;
import java.util.Map;
import dev.zontreck.libzontreck.exceptions.InvalidDeserialization;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.implementation.events.WarpCreatedEvent;
import dev.zontreck.otemod.implementation.events.WarpDeletedEvent;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;

View file

@ -1,13 +1,13 @@
package dev.zontreck.otemod.implementation.warps;
package dev.zontreck.essentials.warps;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import dev.zontreck.otemod.database.FileTreeDatastore;
import dev.zontreck.essentials.util.EssentialsDatastore;
import net.minecraft.nbt.NbtIo;
public class WarpsProvider extends FileTreeDatastore
public class WarpsProvider extends EssentialsDatastore
{
public static final Path BASE = of("warps");
public static final Path WARPS_DATA = BASE.resolve("warps.nbt");