Move stuff over from OTEMod

This commit is contained in:
Aria 2023-02-27 15:16:46 -07:00
parent fb71db187d
commit f27c76f334
40 changed files with 2036 additions and 5 deletions

2
.gitignore vendored
View file

@ -23,3 +23,5 @@ run
# Files from Forge MDK
forge*changelog.txt
.vscode

View file

@ -1,4 +0,0 @@
{
"java.compile.nullAnalysis.mode": "automatic",
"java.configuration.updateBuildConfiguration": "automatic"
}

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.4.13
libz_version=1.0.5.3

7
settings.gradle Normal file
View file

@ -0,0 +1,7 @@
pluginManagement {
repositories {
gradlePluginPortal()
maven { url = 'https://maven.minecraftforge.net/' }
maven { url = 'https://maven.parchmentmc.org' } // Add this line
}
}

View file

@ -1,10 +1,43 @@
package dev.zontreck.essentials;
import org.slf4j.Logger;
import com.mojang.logging.LogUtils;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.server.ServerStartedEvent;
import net.minecraftforge.event.server.ServerStoppingEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
@Mod(AriasEssentials.MODID)
public class AriasEssentials {
public static final String MODID = "ariasessentials";
public static final Logger LOGGER = LogUtils.getLogger();
public static boolean ALIVE;
public AriasEssentials()
{
IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
MinecraftForge.EVENT_BUS.register(this);
}
@SubscribeEvent
public void onServerStart(final ServerStartedEvent ev)
{
ALIVE=true;
}
@SubscribeEvent
public void onServerStop(final ServerStoppingEvent ev)
{
ALIVE=false;
}
}

View file

@ -0,0 +1,11 @@
package dev.zontreck.essentials;
import dev.zontreck.libzontreck.chat.ChatColor;
public class Messages {
public static final String ESSENTIALS_PREFIX;
static{
ESSENTIALS_PREFIX = ChatColor.doColors("!Gray![!Dark_Green!AE!Gray!] ");
}
}

View file

@ -0,0 +1,13 @@
package dev.zontreck.essentials.commands;
import dev.zontreck.otemod.commands.homes.DelHomeCommand;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
public class CommandRegister {
@SubscribeEvent
public void onCommandRegister(final RegisterCommandsEvent ev)
{
DelHomeCommand.register(ev);
}
}

View file

@ -0,0 +1,68 @@
package dev.zontreck.otemod.commands.homes;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
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 net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.network.chat.TextComponent;
public class HomeCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{
dispatcher.register(Commands.literal("home").executes(c-> home(c.getSource(), "default")).then(Commands.argument("nickname", StringArgumentType.string()).executes(c -> home(c.getSource(), StringArgumentType.getString(c, "nickname")))));
//dispatcher.register(Commands.literal("sethome").then(Commands.argument("nickname", StringArgumentType.string())).executes(command -> {
//String arg = StringArgumentType.getString(command, "nickname");
//return setHome(command.getSource(), arg);
//}));
}
private static int home(CommandSourceStack ctx, String homeName)
{
// Request homes
// String homeName = "";
// CommandSourceStack ctx = ctx2.getSource();
// homeName = StringArgumentType.getString(ctx2, "nickname");
// 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);
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());
}catch(CommandSyntaxException e)
{
e.printStackTrace();
return 1;
}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());
return 0;
} catch (UserProfileNotYetExistsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
}

View file

@ -0,0 +1,65 @@
package dev.zontreck.otemod.commands.homes;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
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 net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.network.chat.Style;
import net.minecraft.network.chat.TextComponent;
public class HomesCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{
dispatcher.register(Commands.literal("homes").executes(HomesCommand::getHomes));
}
private static int getHomes(CommandContext<CommandSourceStack> ctx)
{
// Request homes
try{
ServerPlayer player = ctx.getSource().getPlayerOrException();
Profile p = Profile.get_profile_of(player.getStringUUID());
Homes homes = p.player_homes;
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);
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());
}
}catch(CommandSyntaxException ex)
{
ex.printStackTrace();
} catch (UserProfileNotYetExistsException e) {
e.printStackTrace();
}
return 0;
}
}

View file

@ -0,0 +1,77 @@
package dev.zontreck.otemod.commands.homes;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.zontreck.libzontreck.chat.ChatColor;
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;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
import net.minecraft.network.chat.TextComponent;
public class SetHomeCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{
dispatcher.register(Commands.literal("sethome").executes(c->setHome(c.getSource(), "default")).then(Commands.argument("nickname", StringArgumentType.string()).executes(c -> setHome(c.getSource(), StringArgumentType.getString(c, "nickname")))));
//dispatcher.register(Commands.literal("sethome").then(Commands.argument("nickname", StringArgumentType.string())).executes(command -> {
//String arg = StringArgumentType.getString(command, "nickname");
//return setHome(command.getSource(), arg);
//}));
}
private static int setHome(CommandSourceStack ctx, String homeName)
{
// Request homes
// String homeName = "";
// CommandSourceStack ctx = ctx2.getSource();
// homeName = StringArgumentType.getString(ctx2, "nickname");
// if(homeName==null)return 0;
ServerPlayer p;
try {
p = ctx.getPlayerOrException();
Vec3 position = p.position();
Vec2 rot = p.getRotationVector();
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);
ChatServerOverride.broadcastTo(p.getUUID(), new TextComponent(OTEMod.OTEPrefix + 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());
e.printStackTrace();
}
return 0;
}
}

View file

@ -0,0 +1,90 @@
package dev.zontreck.essentials.commands.teleport;
import com.mojang.brigadier.CommandDispatcher;
import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.libzontreck.vectors.Vector3;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.Vec3;
public class RTPCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{
dispatcher.register(Commands.literal("rtp").executes(c->rtp(c.getSource())));
//executes(c -> doCancel(c.getSource())));
//dispatcher.register(Commands.literal("sethome").then(Commands.argument("nickname", StringArgumentType.string())).executes(command -> {
//String arg = StringArgumentType.getString(command, "nickname");
//return setHome(command.getSource(), arg);
//}));
}
private static int rtp(CommandSourceStack source) {
/*if(!CommandRegistry.canUse("rtp")) {
ChatServerOverride.broadcastTo(source.getPlayer().getUUID(), Component.translatable("dev.zontreck.otemod.msgs.command_cooling_down").append(Component.literal(""+CommandRegistry.getRemaining("rtp"))).append(Component.translatable("dev.zontreck.otemod.msgs.command_cooling_down_seconds")), source.getServer());
// exit
//return 0; // Removed until the player data registry is implemented
}
CommandRegistry.markUsed("rtp");*/
if(!(source.getEntity() instanceof Player)){
return 1;
}
final ServerPlayer pla = (ServerPlayer)source.getEntity();
final TeleportContainer cont = new TeleportContainer(pla, Vec3.ZERO, pla.getRotationVector(), source.getLevel());
Thread tx = new Thread(new Runnable() {
@Override
public void run(){
// We can now execute the loop to search for a safe spot!
Vector3 v = new Vector3();
// RTP is not designed to be safe really, but we at least want to check if where we are putting the player is air
Vec3 pos = pla.position();
boolean found_place= false;
RTPContainer container = RandomPositionFactory.beginRTPSearch(pla, pos, pla.getRotationVector(), pla.getLevel());
while(!container.complete)
{
if(!OTEMod.ALIVE)
{
container.aborted=true;
container.containingThread.interrupt();
return;
}
if(container.tries>30)
{
// abort!
return;
}
}
v = container.container.world_pos.Position;
ChatServerOverride.broadcastTo(pla.getUUID(), new TextComponent(ChatColor.DARK_GRAY + "["+ChatColor.DARK_GREEN + "OTEMOD" + ChatColor.DARK_GRAY + "] "+ChatColor.DARK_PURPLE+" A suitable location has been found. Wormhole opening now!"), OTEMod.THE_SERVER);
// Apply the effect
TeleportActioner.ApplyTeleportEffect(pla);
cont.Position=v.asMinecraftVector();
TeleportActioner.PerformTeleport(cont);
}
});
tx.start();
return 0;
}
}

View file

@ -0,0 +1,94 @@
package dev.zontreck.essentials.commands.teleport;
import com.mojang.brigadier.CommandDispatcher;
import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.libzontreck.chat.Clickable;
import dev.zontreck.libzontreck.chat.HoverTip;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.Style;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.level.ServerPlayer;
public class TPACommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{
dispatcher.register(Commands.literal("tpa").executes(c->usage(c.getSource())).then(Commands.argument("player", EntityArgument.player()).executes(c -> tpa(c.getSource(), EntityArgument.getPlayer(c, "player")))));
//dispatcher.register(Commands.literal("sethome").then(Commands.argument("nickname", StringArgumentType.string())).executes(command -> {
//String arg = StringArgumentType.getString(command, "nickname");
//return setHome(command.getSource(), arg);
//}));
}
private static int tpa(CommandSourceStack source, ServerPlayer serverPlayer) {
// Send the request to player
if(serverPlayer == null){
source.sendFailure(new TextComponent(ChatColor.DARK_RED+"Error: Player not found"));
return 1;
}
ServerPlayer play = (ServerPlayer)source.getEntity();
if(!OTEMod.DEVELOPER){
if(play.getUUID() == serverPlayer.getUUID()){
source.sendFailure(new TextComponent(ChatColor.DARK_RED+"You cannot teleport to yourself!"));
return 1;
}
}
TeleportContainer cont = new TeleportContainer(play.getUUID(), serverPlayer.getUUID());
for(TeleportContainer cont2 : OTEMod.TeleportRegistry){
if(cont2.compareTo(cont)==0){
ChatServerOverride.broadcastTo(cont.FromPlayer, new TextComponent(ChatColor.DARK_RED+ "You already have a TPA Request active, wait for it to expire, or use the cancel button/command"), source.getServer());
return 0;
}else {
if(cont2.FromPlayer == cont.FromPlayer){
ChatServerOverride.broadcastTo(cont.FromPlayer, new TextComponent(ChatColor.DARK_RED+ "You already have a TPA Request active, wait for it to expire, or use the cancel button/command"), source.getServer());
return 0;
}
}
}
ClickEvent ce = Clickable.command("/tpcancel "+cont.TeleportID.toString());
HoverEvent he = HoverTip.get(ChatColor.DARK_GREEN+"Cancel this teleport request (Not yet implemented)");
Style s = Style.EMPTY.withFont(Style.DEFAULT_FONT).withHoverEvent(he).withClickEvent(ce);
// Send the alerts
ChatServerOverride.broadcastTo(cont.FromPlayer, new TextComponent(ChatColor.BOLD + ChatColor.DARK_GREEN +"TPA Request Sent! ").append(new TextComponent(ChatColor.BOLD+ChatColor.DARK_GRAY+"["+ChatColor.DARK_RED+"X"+ChatColor.DARK_GRAY+"]").setStyle(s)), serverPlayer.server);
ce = Clickable.command("/tpaccept "+cont.TeleportID.toString());
he = HoverTip.get(ChatColor.DARK_GREEN + "Accept tp request");
ClickEvent ce2 = Clickable.command("/tpdeny "+cont.TeleportID.toString());
HoverEvent he2 = HoverTip.get(ChatColor.DARK_RED+"Deny this request");
s = Style.EMPTY.withFont(Style.DEFAULT_FONT).withClickEvent(ce).withHoverEvent(he);
Style s2 = Style.EMPTY.withFont(Style.DEFAULT_FONT).withClickEvent(ce2).withHoverEvent(he2);
Profile p;
try {
p = Profile.get_profile_of(cont.FromPlayer.toString());
} catch (UserProfileNotYetExistsException e) {
return 1;
}
ChatServerOverride.broadcastTo(cont.ToPlayer, new TextComponent(p.name_color+p.nickname + ChatColor.BOLD + ChatColor.DARK_PURPLE+" is requesting to teleport to you\n \n").
append(new TextComponent(ChatColor.DARK_GRAY+"["+ChatColor.DARK_GREEN+"ACCEPT" + ChatColor.DARK_GRAY+"] ").setStyle(s)).
append(new TextComponent(ChatColor.DARK_GRAY + "["+ChatColor.DARK_RED+"DENY"+ChatColor.DARK_GRAY+"]").setStyle(s2)), serverPlayer.server);
OTEMod.TeleportRegistry.add(cont);
return 0;
}
private static int usage(CommandSourceStack source) {
source.sendSuccess(new TextComponent("/tpa USAGE\n\n "+ChatColor.BOLD + ChatColor.DARK_GRAY+"/tpa "+ChatColor.DARK_RED+"target_player\n"), false);
return 0;
}
}

View file

@ -0,0 +1,98 @@
package dev.zontreck.essentials.commands.teleport;
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 net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.Style;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.level.ServerPlayer;
public class TPAHereCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{
dispatcher.register(Commands.literal("tpahere").executes(c->usage(c.getSource())).then(Commands.argument("player", EntityArgument.player()).executes(c -> tpa(c.getSource(), EntityArgument.getPlayer(c, "player")))));
//dispatcher.register(Commands.literal("sethome").then(Commands.argument("nickname", StringArgumentType.string())).executes(command -> {
//String arg = StringArgumentType.getString(command, "nickname");
//return setHome(command.getSource(), arg);
//}));
}
private static int tpa(CommandSourceStack source, ServerPlayer serverPlayer) {
// Send the request to player
ServerPlayer play = (ServerPlayer)source.getEntity();
if(serverPlayer == null){
source.sendFailure(new TextComponent(ChatColor.DARK_RED+"Error: Player not found"));
return 1;
}
if(!OTEMod.DEVELOPER){
if(play.getUUID() == serverPlayer.getUUID()){
source.sendFailure(new TextComponent(ChatColor.DARK_RED+"You cannot teleport to yourself!"));
return 1;
}
}
TeleportContainer cont = new TeleportContainer(serverPlayer.getUUID(), play.getUUID());
for(TeleportContainer cont2 : OTEMod.TeleportRegistry){
if(cont2.compareTo(cont)==0){
ChatServerOverride.broadcastTo(cont.ToPlayer, new TextComponent(ChatColor.DARK_RED+ "You already have a TPA Request active, wait for it to expire, or use the cancel button/command"), source.getServer());
return 0;
}else {
if(cont2.ToPlayer.equals(cont.ToPlayer)){
ChatServerOverride.broadcastTo(cont.FromPlayer, new TextComponent(ChatColor.DARK_RED+ "You already have a TPA Request active, wait for it to expire, or use the cancel button/command"), source.getServer());
return 0;
}
}
}
ClickEvent ce = Clickable.command("/tpcancel "+cont.TeleportID.toString());
HoverEvent he = HoverTip.get(ChatColor.DARK_GREEN+"Cancel this teleport request (Not yet implemented)");
Style s = Style.EMPTY.withFont(Style.DEFAULT_FONT).withHoverEvent(he).withClickEvent(ce);
// Send the alerts
ChatServerOverride.broadcastTo(cont.ToPlayer, new TextComponent(ChatColor.BOLD + ChatColor.DARK_GREEN +"TPA Request Sent! ").append(new TextComponent(ChatColor.BOLD+ChatColor.DARK_GRAY+"["+ChatColor.DARK_RED+"X"+ChatColor.DARK_GRAY+"]").setStyle(s)), serverPlayer.server);
ce = Clickable.command("/tpaccept "+cont.TeleportID.toString());
he = HoverTip.get(ChatColor.DARK_GREEN + "Accept tp request");
ClickEvent ce2 = Clickable.command("/tpdeny "+cont.TeleportID.toString());
HoverEvent he2 = HoverTip.get(ChatColor.DARK_RED+"Deny this request");
s = Style.EMPTY.withFont(Style.DEFAULT_FONT).withClickEvent(ce).withHoverEvent(he);
Style s2 = Style.EMPTY.withFont(Style.DEFAULT_FONT).withClickEvent(ce2).withHoverEvent(he2);
Profile p;
try {
p = Profile.get_profile_of(cont.ToPlayer.toString());
} catch (UserProfileNotYetExistsException e) {
return 1;
}
ChatServerOverride.broadcastTo(cont.FromPlayer, new TextComponent(p.name_color+p.nickname + ChatColor.BOLD + ChatColor.DARK_PURPLE+" is requesting you to teleport to them\n \n").
append(new TextComponent(ChatColor.DARK_GRAY+"["+ChatColor.DARK_GREEN+"ACCEPT" + ChatColor.DARK_GRAY+"] ").setStyle(s)).
append(new TextComponent(ChatColor.DARK_GRAY + "["+ChatColor.DARK_RED+"DENY"+ChatColor.DARK_GRAY+"]").setStyle(s2)), serverPlayer.server);
OTEMod.TeleportRegistry.add(cont);
return 0;
}
private static int usage(CommandSourceStack source) {
source.sendSuccess(new TextComponent("/tpa USAGE\n\n "+ChatColor.BOLD + ChatColor.DARK_GRAY+"/tpa "+ChatColor.DARK_RED+"target_player\n"), false);
return 0;
}
}

View file

@ -0,0 +1,65 @@
package dev.zontreck.essentials.commands.teleport;
import java.util.UUID;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import dev.zontreck.libzontreck.chat.ChatColor;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.level.ServerPlayer;
public class TPAcceptCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{
dispatcher.register(Commands.literal("tpaccept").then(Commands.argument("TeleportUUID", StringArgumentType.string()).executes(c->doCancel(c.getSource(), StringArgumentType.getString(c, "TeleportUUID")))));
//executes(c -> doCancel(c.getSource())));
//dispatcher.register(Commands.literal("sethome").then(Commands.argument("nickname", StringArgumentType.string())).executes(command -> {
//String arg = StringArgumentType.getString(command, "nickname");
//return setHome(command.getSource(), arg);
//}));
}
private static int doCancel(CommandSourceStack source, String TPID) {
UUID teleporter = UUID.fromString(TPID);
ServerPlayer play = (ServerPlayer)source.getEntity();
for(TeleportContainer cont : TeleportRegistry.get()){
if(cont.TeleportID.equals(teleporter)){
// Accepting!
ServerPlayer from = source.getServer().getPlayerList().getPlayer(cont.FromPlayer);
ServerPlayer to = source.getServer().getPlayerList().getPlayer(cont.ToPlayer);
Component comp = new TextComponent(OTEMod.OTEPrefix + " " + ChatColor.DARK_PURPLE+"Teleport request was accepted. Opening wormhole!");
ChatServerOverride.broadcastTo(cont.FromPlayer, comp, source.getServer());
ChatServerOverride.broadcastTo(cont.ToPlayer, comp, source.getServer());
OTEMod.TeleportRegistry.remove(cont);
cont.PlayerInst = from;
cont.Position = to.position();
cont.Rotation = to.getRotationVector();
cont.Dimension = to.getLevel();
TeleportActioner.ApplyTeleportEffect(from);
TeleportActioner.PerformTeleport(cont);
return 0;
}
}
Component comp = new TextComponent(ChatColor.DARK_RED+"The teleport was not found, perhaps the request expired or was already cancelled/denied");
ChatServerOverride.broadcastTo(play.getUUID(), comp, source.getServer());
return 0;
}
}

View file

@ -0,0 +1,54 @@
package dev.zontreck.otemod.commands.teleport;
import java.util.UUID;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.chat.ChatServerOverride;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.level.ServerPlayer;
public class TPCancelCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{
dispatcher.register(Commands.literal("tpcancel").then(Commands.argument("TeleportUUID", StringArgumentType.string()).executes(c->doCancel(c.getSource(), StringArgumentType.getString(c, "TeleportUUID")))));
//executes(c -> doCancel(c.getSource())));
//dispatcher.register(Commands.literal("sethome").then(Commands.argument("nickname", StringArgumentType.string())).executes(command -> {
//String arg = StringArgumentType.getString(command, "nickname");
//return setHome(command.getSource(), arg);
//}));
}
private static int doCancel(CommandSourceStack source, String TPID) {
UUID teleporter = UUID.fromString(TPID);
ServerPlayer play = (ServerPlayer)source.getEntity();
for(TeleportContainer cont : OTEMod.TeleportRegistry){
if(cont.TeleportID.equals(teleporter)){
// Canceling!
Component comp = new TextComponent(OTEMod.OTEPrefix + " " + ChatColor.DARK_PURPLE+"Teleport request was cancelled");
ChatServerOverride.broadcastTo(cont.FromPlayer, comp, source.getServer());
ChatServerOverride.broadcastTo(cont.ToPlayer, comp, source.getServer());
OTEMod.TeleportRegistry.remove(cont);
return 0;
}
}
Component comp = new TextComponent(ChatColor.DARK_RED+"The teleport was not found, perhaps the request expired or was already cancelled");
ChatServerOverride.broadcastTo(play.getUUID(), comp, source.getServer());
return 0;
}
}

View file

@ -0,0 +1,54 @@
package dev.zontreck.otemod.commands.teleport;
import java.util.UUID;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.chat.ChatServerOverride;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.level.ServerPlayer;
public class TPDenyCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{
dispatcher.register(Commands.literal("tpdeny").then(Commands.argument("TeleportUUID", StringArgumentType.string()).executes(c->doCancel(c.getSource(), StringArgumentType.getString(c, "TeleportUUID")))));
//executes(c -> doCancel(c.getSource())));
//dispatcher.register(Commands.literal("sethome").then(Commands.argument("nickname", StringArgumentType.string())).executes(command -> {
//String arg = StringArgumentType.getString(command, "nickname");
//return setHome(command.getSource(), arg);
//}));
}
private static int doCancel(CommandSourceStack source, String TPID) {
UUID teleporter = UUID.fromString(TPID);
ServerPlayer play = (ServerPlayer)source.getEntity();
for(TeleportContainer cont : OTEMod.TeleportRegistry){
if(cont.TeleportID.equals(teleporter)){
// Canceling!
Component comp = new TextComponent(OTEMod.OTEPrefix + " " + ChatColor.DARK_PURPLE+"Teleport request was denied");
ChatServerOverride.broadcastTo(cont.FromPlayer, comp, source.getServer());
ChatServerOverride.broadcastTo(cont.ToPlayer, comp, source.getServer());
OTEMod.TeleportRegistry.remove(cont);
return 0;
}
}
Component comp = new TextComponent(ChatColor.DARK_RED+"The teleport was not found, perhaps the request expired or was already cancelled/denied");
ChatServerOverride.broadcastTo(play.getUUID(), comp, source.getServer());
return 0;
}
}

View file

@ -0,0 +1,39 @@
package dev.zontreck.essentials.commands.teleport;
import dev.zontreck.libzontreck.util.DelayedExecutorService;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
public class TeleportActioner
{
public static void PerformTeleport(TeleportContainer contain){
//sub_runnable run = new sub_runnable(contain);
DelayedExecutorService.getInstance().schedule(new TeleportRunnable(contain), 2);
}
public static void ApplyTeleportEffect(ServerPlayer player){
// 10/05/2022 - Thinking ahead here to future proof it so i can do things in threads safely
// By adding this task onto the main server thread, any thread can call the TeleportActioner and it will be actioned on the main thread without needing to repeat the process of sending this to the server thread.
player.server.execute(new Runnable(){
public void run(){
MobEffectInstance inst = new MobEffectInstance(MobEffects.BLINDNESS, 250, 1, true, true);
// 02/26/2023 - Adjusted to 400 due to 1.18.2, the teleport is slightly more delayed, and thus a regen is needed incase levitation runs out too soon
MobEffectInstance regen = new MobEffectInstance(MobEffects.REGENERATION, 400, 2, true, true);
// 10-05-2022 - Adjusted to 100 on duration due to a small issue where it would sometimes stop levitation prior to the teleport taking effect.
// 02/26/2023 - Adjusted to 200 on duration due to 1.18.2 causing levitation to run out too quickly before teleport
// Small tradeoff is the player now levitates slightly longer at the destination. This is acceptable. Compensated by increasing regen strength by 1
MobEffectInstance levitate = new MobEffectInstance(MobEffects.LEVITATION, 200, 1, true, true);
player.addEffect(inst);
player.addEffect(regen);
player.addEffect(levitate); // ensure the player can't fall into lava in the short time we are not in control (if the player was silly enough to make a home above lava!!!)
}
});
}
}

View file

@ -0,0 +1,70 @@
package dev.zontreck.essentials.commands.teleport;
import java.time.Instant;
import java.util.UUID;
import dev.zontreck.libzontreck.vectors.Vector3;
import dev.zontreck.libzontreck.vectors.WorldPosition;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
public class TeleportContainer implements Comparable{
public UUID FromPlayer;
public UUID ToPlayer;
public long StartedAt;
public UUID TeleportID;
public WorldPosition world_pos;
/*
* The following variables are only used when actioning the teleport itself, and should only be initialized once the teleport is about to engage
*/
public ServerPlayer PlayerInst;
public Vec3 Position;
public Vec2 Rotation;
public ServerLevel Dimension;
public boolean has_expired(){
if(Instant.now().getEpochSecond() > (StartedAt + (60)))
{
return true;
}else return false;
}
public TeleportContainer (UUID From, UUID To)
{
FromPlayer = From;
ToPlayer=To;
TeleportID = UUID.randomUUID();
StartedAt = Instant.now().getEpochSecond();
}
public TeleportContainer(ServerPlayer f_p, Vec3 f_pos, Vec2 f_rot, ServerLevel f_dim) {
SetTeleportDestination(f_p, f_pos, f_rot, f_dim);
world_pos = new WorldPosition(new Vector3(f_pos), f_dim);
}
private void SetTeleportDestination(ServerPlayer f_p, Vec3 f_pos, Vec2 f_rot, ServerLevel f_dim) {
PlayerInst = f_p;
Position = f_pos;
Rotation = f_rot;
Dimension = f_dim;
world_pos = new WorldPosition(new Vector3(f_pos), f_dim);
}
@Override
public int compareTo(Object o) {
if(o instanceof TeleportContainer){
TeleportContainer cont = (TeleportContainer)o;
if(cont.FromPlayer != FromPlayer){
return -1;
}else {
if(cont.ToPlayer != ToPlayer)return 1;
else return 0;
}
}else return -1;
}
}

View file

@ -0,0 +1,51 @@
package dev.zontreck.essentials.commands.teleport;
import dev.zontreck.libzontreck.vectors.Vector2;
import dev.zontreck.libzontreck.vectors.Vector3;
import dev.zontreck.libzontreck.vectors.WorldPosition;
import dev.zontreck.libzontreck.exceptions.InvalidDeserialization;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.server.level.ServerLevel;
/**
* This defines the data structure, and methods for deserializing and serializing teleport destinations, for easier storage in the database
**/
public class TeleportDestination extends WorldPosition
{
public Vector2 Rotation;
public TeleportDestination(CompoundTag tag) throws InvalidDeserialization
{
super(tag, true);
Rotation = new Vector2(tag.getString("Rotation"));
}
public TeleportDestination(Vector3 pos, Vector2 rot, String dim)
{
super(pos, dim);
Rotation = rot;
}
public TeleportDestination(Vector3 pos, Vector2 rot, ServerLevel dim)
{
super(pos,dim);
Rotation=rot;
}
@Override
public String toString()
{
return NbtUtils.structureToSnbt(serialize());
}
public CompoundTag serialize(){
CompoundTag tag = super.serializePretty();
tag.putString("Rotation", Rotation.toString());
return tag;
}
}

View file

@ -0,0 +1,18 @@
package dev.zontreck.essentials.commands.teleport;
import java.util.ArrayList;
import java.util.List;
public class TeleportRegistry {
private static final List<TeleportContainer> lst;
static{
lst=new ArrayList<>();
}
public static List<TeleportContainer> get()
{
return lst;
}
}

View file

@ -0,0 +1,28 @@
package dev.zontreck.essentials.commands.teleport;
import dev.zontreck.libzontreck.util.DelayedExecutorService;
public class TeleportRunnable implements Runnable
{
public final TeleportContainer Action;
public TeleportRunnable(TeleportContainer cont){
Action = cont;
}
@Override
public void run() {
Action.PlayerInst.teleportTo(Action.Dimension, Action.Position.x, Action.Position.y, Action.Position.z, Action.Rotation.y, Action.Rotation.x);
DelayedExecutorService.getInstance().schedule(new Runnable(){
public TeleportContainer container=Action;
@Override
public void run()
{
container.PlayerInst.onUpdateAbilities();
container.PlayerInst.setPos(container.Position);
container.PlayerInst.giveExperiencePoints(1);
}
}, 1);
}
}

View file

@ -0,0 +1,69 @@
package dev.zontreck.otemod.commands.warps;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import dev.zontreck.libzontreck.chat.ChatColor;
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.warps.NoSuchWarpException;
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.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
public class DelWarpCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{
dispatcher.register(Commands.literal("delwarp").then(Commands.argument("nickname", StringArgumentType.string()).executes(c -> setWarp(c.getSource(), StringArgumentType.getString(c, "nickname")))));
//dispatcher.register(Commands.literal("sethome").then(Commands.argument("nickname", StringArgumentType.string())).executes(command -> {
//String arg = StringArgumentType.getString(command, "nickname");
//return setHome(command.getSource(), arg);
//}));
}
private static int setWarp(CommandSourceStack source, String string) {
ServerPlayer p = (ServerPlayer)source.getEntity();
Warp warp;
try {
warp = WarpsProvider.WARPS_INSTANCE.getNamedWarp(string);
} catch (NoSuchWarpException e) {
ChatServerOverride.broadcastTo(p.getUUID(), new TextComponent(OTEMod.OTEPrefix+ChatColor.doColors(" !Dark_Red!That warp does not exist")), p.server);
return 0;
}
if(p.getUUID().equals(warp.owner) || p.hasPermissions(5))
{
try {
WarpsProvider.WARPS_INSTANCE.delete(WarpsProvider.WARPS_INSTANCE.getNamedWarp(string));
} catch (NoSuchWarpException e) {
ChatServerOverride.broadcastTo(p.getUUID(), new TextComponent(OTEMod.OTEPrefix+ChatColor.doColors(" !Dark_Red!That warp does not exist")), p.server);
return 0;
}
ChatServerOverride.broadcastTo(p.getUUID(), new TextComponent(OTEMod.OTEPrefix+ChatColor.doColors(" !Dark_Green!Warp deleted successfully")), p.server);
}else {
ChatServerOverride.broadcastTo(p.getUUID(), new TextComponent(OTEMod.OTEPrefix+ChatColor.doColors(" !Dark_Red!Warp could not be deleted, because you do not own the warp.")), p.server);
}
return 0;
}
}

View file

@ -0,0 +1,55 @@
package dev.zontreck.otemod.commands.warps;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import dev.zontreck.libzontreck.chat.ChatColor;
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.warps.Warp;
import dev.zontreck.otemod.implementation.warps.WarpsProvider;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
public class RTPWarpCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{
dispatcher.register(Commands.literal("rtpwarp").then(Commands.argument("nickname", StringArgumentType.string()).executes(c -> setWarp(c.getSource(), StringArgumentType.getString(c, "nickname")))));
//dispatcher.register(Commands.literal("sethome").then(Commands.argument("nickname", StringArgumentType.string())).executes(command -> {
//String arg = StringArgumentType.getString(command, "nickname");
//return setHome(command.getSource(), arg);
//}));
}
private static int setWarp(CommandSourceStack source, String string) {
ServerPlayer p = (ServerPlayer)source.getEntity();
Vec3 position = p.position();
Vec2 rot = p.getRotationVector();
TeleportDestination dest = new TeleportDestination(new Vector3(position), new Vector2(rot), p.getLevel());
Warp warp = new Warp(p.getUUID(), string, true, true, dest);
WarpsProvider.WARPS_INSTANCE.add(warp);
ChatServerOverride.broadcastTo(p.getUUID(), new TextComponent(OTEMod.OTEPrefix+ChatColor.doColors(" !Dark_Green!Random Position Teleport (RTP) Warp created successfully")), p.server);
return 0;
}
}

View file

@ -0,0 +1,55 @@
package dev.zontreck.otemod.commands.warps;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import dev.zontreck.libzontreck.chat.ChatColor;
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.warps.Warp;
import dev.zontreck.otemod.implementation.warps.WarpsProvider;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
public class SetWarpCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{
dispatcher.register(Commands.literal("setwarp").then(Commands.argument("nickname", StringArgumentType.string()).executes(c -> setWarp(c.getSource(), StringArgumentType.getString(c, "nickname")))));
//dispatcher.register(Commands.literal("sethome").then(Commands.argument("nickname", StringArgumentType.string())).executes(command -> {
//String arg = StringArgumentType.getString(command, "nickname");
//return setHome(command.getSource(), arg);
//}));
}
private static int setWarp(CommandSourceStack source, String string) {
ServerPlayer p = (ServerPlayer)source.getEntity();
Vec3 position = p.position();
Vec2 rot = p.getRotationVector();
TeleportDestination dest = new TeleportDestination(new Vector3(position), new Vector2(rot), p.getLevel());
Warp w = new Warp(p.getUUID(), string, false, true, dest);
WarpsProvider.WARPS_INSTANCE.add(w);
ChatServerOverride.broadcastTo(p.getUUID(), new TextComponent(OTEMod.OTEPrefix+ChatColor.doColors(" !Dark_Green!Warp created successfully")), p.server);
return 0;
}
}

View file

@ -0,0 +1,124 @@
package dev.zontreck.otemod.commands.warps;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.List;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.libzontreck.chat.Clickable;
import dev.zontreck.libzontreck.exceptions.InvalidSideException;
import dev.zontreck.libzontreck.vectors.Vector3;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.chat.ChatServerOverride;
import dev.zontreck.otemod.commands.teleport.RTPCommand;
import dev.zontreck.otemod.commands.teleport.RTPContainer;
import dev.zontreck.otemod.commands.teleport.RandomPositionFactory;
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.warps.NoSuchWarpException;
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.nbt.NbtUtils;
import net.minecraft.network.chat.Style;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
public class WarpCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{
dispatcher.register(Commands.literal("warp").executes(c-> nowarp(c.getSource())).then(Commands.argument("name", StringArgumentType.string()).executes(c->warp(c.getSource(), StringArgumentType.getString(c, "name")))));
//dispatcher.register(Commands.literal("sethome").then(Commands.argument("nickname", StringArgumentType.string())).executes(command -> {
//String arg = StringArgumentType.getString(command, "nickname");
//return setHome(command.getSource(), arg);
//}));
}
private static int warp(CommandSourceStack source, String string) {
final ServerPlayer p;
try{
p=source.getPlayerOrException();
Warp warp = WarpsProvider.WARPS_INSTANCE.getNamedWarp(string);
TeleportDestination dest = warp.destination;
ServerLevel dimL=(ServerLevel) dest.getActualDimension();
final int type = warp.RTP ? 1 : 0;
final ServerLevel f_dim = dimL;
if(type == 1)
{
ChatServerOverride.broadcastTo(p.getUUID(), new TextComponent(OTEMod.OTEPrefix+ ChatColor.doColors(" !Dark_Green!Attempting to locate a safe location. This may take a minute or two")), p.server);
}else{
ChatServerOverride.broadcastTo(p.getUUID(), new TextComponent(OTEMod.OTEPrefix+ ChatColor.doColors(" !Dark_Purple!Warping!")), p.server);
}
Thread tx = new Thread(new Runnable(){
public void run(){
if(type==1){
try {
dest.Position = Vector3.ZERO;
RTPContainer cont = RandomPositionFactory.beginRTPSearch(p, Vec3.ZERO, Vec2.ZERO, f_dim);
while(!cont.complete)
{
if(!OTEMod.ALIVE)
{
cont.aborted=true;
cont.containingThread.interrupt();
return;
}
if(cont.tries>30)
{
return;
}
}
dest.Position = cont.container.world_pos.Position;
//RTPCommand.findPosition(source.getLevel(), false, p.getUUID());
ChatServerOverride.broadcastTo(p.getUUID(), new TextComponent(OTEMod.OTEPrefix+ChatColor.doColors(" !Dark_Green!Location found, warping!")), p.server);
} catch (Exception e) {
e.printStackTrace();
return;
}
}
TeleportActioner.ApplyTeleportEffect(p);
TeleportContainer tc = new TeleportContainer(p, dest.Position.asMinecraftVector(), dest.Rotation.asMinecraftVector(), f_dim);
TeleportActioner.PerformTeleport(tc);
}
});
tx.start();
}catch(NoSuchWarpException e)
{
ChatServerOverride.broadcastTo(source.getEntity().getUUID(), new TextComponent(ChatColor.DARK_RED+"No such warp"), source.getServer());
} catch (CommandSyntaxException e) {
e.printStackTrace();
}
return 0;
}
private static int nowarp(CommandSourceStack source) {
ServerPlayer p = (ServerPlayer)source.getEntity();
ChatServerOverride.broadcastTo(p.getUUID(), new TextComponent(ChatColor.DARK_RED + "You must supply the warp name. If you need to know what warps are available, please use the warps command, or click this message.").withStyle(Style.EMPTY.withFont(Style.DEFAULT_FONT).withClickEvent(Clickable.command("/warps"))), source.getServer());
return 0;
}
}

View file

@ -0,0 +1,90 @@
package dev.zontreck.otemod.commands.warps;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
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;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.Style;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.level.ServerPlayer;
public class WarpsCommand {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{
dispatcher.register(Commands.literal("warps").executes(c-> warps(c.getSource())));
//dispatcher.register(Commands.literal("sethome").then(Commands.argument("nickname", StringArgumentType.string())).executes(command -> {
//String arg = StringArgumentType.getString(command, "nickname");
//return setHome(command.getSource(), arg);
//}));
}
private static int warps(CommandSourceStack source) {
ServerPlayer p = (ServerPlayer)source.getEntity();
Map<String, Warp> warps = WarpsProvider.WARPS_INSTANCE.get();
ChatServerOverride.broadcastTo(p.getUUID(), new TextComponent(OTEMod.OTEPrefix + " "+ChatColor.resetChat() + "There are "+warps.size()+" warps available"), source.getServer());
Iterator<Entry<String, Warp>> it = warps.entrySet().iterator();
while(it.hasNext())
{
// TODO: Implement public and private. Private requires an ACL be implemented. New GUI
Warp warp = it.next().getValue();
// Pull the owner profile
Profile prof=null;
try {
prof = Profile.get_profile_of(warp.owner.toString());
} catch (UserProfileNotYetExistsException e) {
e.printStackTrace();
return 1;
}
String warpName = warp.WarpName;
int warpType = 0;
if(warp.RTP) warpType=1;
String appendType = (warpType == 0) ? "standard warp." : "RTP Warp. This has a position randomizer.";
HoverEvent hover = HoverTip.get(ChatColor.BOLD + ChatColor.DARK_PURPLE + "This warp is a "+appendType);
ClickEvent click = Clickable.command("/warp "+warpName);
Style S = Style.EMPTY.withFont(Style.DEFAULT_FONT).withHoverEvent(hover).withClickEvent(click);
Component warpMsg = new TextComponent(ChatColor.GREEN + warpName + ChatColor.resetChat()).withStyle(S);
// Now, display the warp name, along with the warp's owner information
HoverEvent h2 = HoverTip.get(prof.name_color+prof.nickname+ChatColor.resetChat()+ChatColor.AQUA+" is the owner of this warp");
S = Style.EMPTY.withFont(Style.DEFAULT_FONT).withHoverEvent(h2);
Component ownerInfo = new TextComponent(ChatColor.GOLD+ " [Hover to see the warp's info]").withStyle(S);
// Combine the two
warpMsg = new TextComponent("").append(warpMsg).append(ownerInfo);
ChatServerOverride.broadcastTo(p.getUUID(), warpMsg, source.getServer());
}
return 0;
}
}

View file

@ -0,0 +1,18 @@
package dev.zontreck.essentials.events;
import dev.zontreck.essentials.homes.Home;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;
/**
* This event may be cancelled if the home is in a invalid location, like in the middle of a claim they have no rights to
*/
@Cancelable
public class HomeCreatedEvent extends Event
{
public Home home;
public HomeCreatedEvent(Home home)
{
this.home=home;
}
}

View file

@ -0,0 +1,19 @@
package dev.zontreck.essentials.events;
import dev.zontreck.essentials.homes.Home;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;
/**
* This event cannot be cancelled.
*/
public class HomeDeletedEvent extends Event
{
public Home home;
public HomeDeletedEvent(Home home)
{
this.home=home;
}
}

View file

@ -0,0 +1,22 @@
package dev.zontreck.essentials.events;
import dev.zontreck.libzontreck.vectors.Vector3;
import dev.zontreck.libzontreck.vectors.WorldPosition;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;
@Cancelable
public class RTPEvent extends Event
{
public ServerPlayer player;
public WorldPosition position;
public RTPEvent(ServerPlayer player, WorldPosition position)
{
this.player=player;
this.position=position;
}
}

View file

@ -0,0 +1,42 @@
package dev.zontreck.essentials.homes;
import java.util.UUID;
import dev.zontreck.libzontreck.exceptions.InvalidDeserialization;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerPlayer;
public class Home {
public UUID owner;
public String homeName;
public TeleportDestination destination;
public Home(ServerPlayer player, String name, TeleportDestination dest)
{
owner=player.getUUID();
homeName=name;
destination=dest;
}
public Home(CompoundTag tag)
{
owner = tag.getUUID("owner");
homeName = tag.getString("name");
try {
destination = new TeleportDestination(tag.getCompound("dest"));
} catch (InvalidDeserialization e) {
e.printStackTrace();
}
}
public CompoundTag serialize()
{
CompoundTag tag = new CompoundTag();
tag.putUUID("owner", owner);
tag.putString("name", homeName);
tag.put("dest", destination.serialize());
return tag;
}
}

View file

@ -0,0 +1,84 @@
package dev.zontreck.otemod.implementation.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 net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
public class Homes {
private Map<String, Home> homes = new HashMap<>();
public String playerID;
public Homes(String playerID)
{
this.playerID=playerID;
}
public int count()
{
return homes.size();
}
public List<Home> getList()
{
return new ArrayList<>(homes.values());
}
public Home get(String name) throws NoSuchHomeException
{
if(homes.containsKey(name))
return homes.get(name);
else throw new NoSuchHomeException();
}
public void delete(String name) throws NoSuchHomeException
{
Home home = homes.get(name);
HomeDeletedEvent e = new HomeDeletedEvent(home);
homes.remove(name);
OTEMod.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);
HomesProvider.commitHomes(this);
}
public static Homes deserialize(CompoundTag tag)
{
Homes ret = new Homes(null);
ListTag theHomes = tag.getList("homes", Tag.TAG_COMPOUND);
for (Tag tag2 : theHomes) {
Home h = new Home((CompoundTag)tag2);
ret.homes.put(h.homeName, h);
}
return ret;
}
public CompoundTag serialize()
{
CompoundTag tag = new CompoundTag();
ListTag lst = new ListTag();
for (Map.Entry<String,Home> entry : homes.entrySet()) {
lst.add(entry.getValue().serialize());
}
tag.put("homes", lst);
return tag;
}
}

View file

@ -0,0 +1,49 @@
package dev.zontreck.otemod.implementation.homes;
import java.io.IOException;
import java.nio.file.Path;
import java.util.UUID;
import dev.zontreck.otemod.implementation.profiles.Profile;
import net.minecraft.nbt.NbtIo;
public class HomesProvider {
/**
* DO NOT USE. Internal use only.
* @see Profile#player_homes
* @param player
* @return
*/
public static Homes getHomesForPlayer(String player)
{
Path homesFile = Profile.BASE.resolve(player).resolve("homes.nbt");
Homes homes = new Homes(player);
if(homesFile.toFile().exists())
{
try {
homes=Homes.deserialize(NbtIo.read(homesFile.toFile()));
homes.playerID=player;
} catch (IOException e) {
e.printStackTrace();
}
}
return homes;
}
public static void commitHomes(Homes playerHomes)
{
Path homesFile = Profile.BASE.resolve(playerHomes.playerID).resolve("homes.nbt");
try {
NbtIo.write(playerHomes.serialize(), homesFile.toFile());
} catch (IOException e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,5 @@
package dev.zontreck.otemod.implementation.homes;
public class NoSuchHomeException extends Exception{
}

View file

@ -0,0 +1,154 @@
package dev.zontreck.essentials.util;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Stream;
import dev.zontreck.essentials.AriasEssentials;
import dev.zontreck.libzontreck.vectors.Vector3;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.BlockPos.MutableBlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.ChunkStatus;
import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.world.level.levelgen.Heightmap.Types;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
public class RTPContainer {
public TeleportContainer container;
public int tries = -1;
public boolean complete = false;
public boolean aborted=false;
public Thread containingThread;
private Types heightMapType;
private int SearchDirection;
public static final List<Block> BLACKLIST;
static {
List<Block> tmp = new ArrayList<>();
tmp.add(Blocks.LAVA);
tmp.add(Blocks.BEDROCK);
tmp.add(Blocks.WATER);
BLACKLIST = tmp;
}
public void moveDown() {
container.world_pos.Position = container.world_pos.Position.moveDown();
container.Position = container.world_pos.Position.asMinecraftVector();
}
public void moveUp() {
container.world_pos.Position = container.world_pos.Position.moveUp();
container.Position = container.world_pos.Position.asMinecraftVector();
}
public void move()
{
if(SearchDirection==1){
moveUp();
}else if(SearchDirection==0)
{
moveDown();
}
}
public void moveOpposite()
{
if(SearchDirection==1){
moveDown();
}else if(SearchDirection==0)
{
moveUp();
}
}
public void newPosition() {
if(!AriasEssentials.ALIVE)return;
containingThread=Thread.currentThread();
Random rng = new Random(Instant.now().getEpochSecond());
Vector3 pos = new Vector3(rng.nextDouble(0xFFFF), 0, rng.nextDouble(0xFFFF));
BlockPos bpos = pos.asBlockPos();
container.Dimension.getChunk(bpos.getX() >> 4, bpos.getZ() >> 4, ChunkStatus.HEIGHTMAPS);
pos = new Vector3(
container.Dimension.getHeightmapPos(heightMapType, pos.asBlockPos()));
while (!container.Dimension.getWorldBorder().isWithinBounds(pos.asBlockPos())) {
pos = new Vector3(rng.nextDouble(0xffff), 0, rng.nextDouble(0xffff));
bpos = pos.asBlockPos();
container.Dimension.getChunk(bpos.getX() >> 4, bpos.getZ() >> 4, ChunkStatus.HEIGHTMAPS);
pos = new Vector3(
container.Dimension.getHeightmapPos(heightMapType, pos.asBlockPos()));
}
container.world_pos.Position = pos;
container.Position = container.world_pos.Position.asMinecraftVector();
if (pos.y < -60) {
newPosition();
return;
}
if (pos.y >= container.Dimension.getLogicalHeight()) {
spiralPositions(pos);
}
tries++;
}
private void spiralPositions(Vector3 position)
{
for(BlockPos pos : BlockPos.spiralAround(new BlockPos(position.x, container.Dimension.getSeaLevel(), position.z), 16, Direction.WEST, Direction.NORTH)){
if(isSafe(pos)){
// Set the new position
container.world_pos.Position = new Vector3(pos);
container.Position = container.world_pos.Position.asMinecraftVector();
return;
}
}
}
protected RTPContainer(ServerPlayer player, Vec3 pos, Vec2 rot, ServerLevel level) {
container = new TeleportContainer(player, pos, rot, level);
if(container.Dimension.dimensionType().hasCeiling())
{
heightMapType = Heightmap.Types.MOTION_BLOCKING_NO_LEAVES;
SearchDirection=-1;
}else {
heightMapType = Types.MOTION_BLOCKING_NO_LEAVES;
SearchDirection=1;
}
newPosition();
}
public boolean isSafe(BlockPos blockPos) {
containingThread=Thread.currentThread();
BlockState b = container.Dimension.getBlockState(blockPos);
BlockState b2 = container.Dimension.getBlockState(blockPos.above());
BlockState b3 = container.Dimension.getBlockState(blockPos.below());
if (b.isAir() && b2.isAir()) {
if (!b3.isAir()) {
if (BLACKLIST.contains(b3.getBlock())) {
return false;
} else
return true;
} else
return false;
} else
return false;
}
}

View file

@ -0,0 +1,23 @@
package dev.zontreck.essentials.commands.teleport;
import dev.zontreck.essentials.implementation.DelayedExecutorService;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
/**
* The factory system used to start searching for a random teleport position
*/
public class RandomPositionFactory {
public static RTPContainer beginRTPSearch(ServerPlayer player, Vec3 pos, Vec2 rot, ServerLevel level)
{
RTPContainer contain= new RTPContainer(player, pos, rot, level);
Thread tx = new Thread(new RandomPositionLocator(contain));
tx.setName("RTPTask-"+String.valueOf(DelayedExecutorService.getNext()));
tx.start();
return contain;
}
}

View file

@ -0,0 +1,92 @@
package dev.zontreck.otemod.commands.teleport;
import java.util.Iterator;
import java.util.concurrent.ScheduledExecutorService;
import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.libzontreck.vectors.Vector3;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.chat.ChatServerOverride;
import dev.zontreck.otemod.events.RTPEvent;
import net.minecraft.core.BlockPos;
import net.minecraft.core.BlockPos.MutableBlockPos;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraftforge.common.MinecraftForge;
/**
* This class aims to serve as the Random Position Locate system
* It aims to be as non-thread blocking as possible to avoid server lag
*
* To utilize, initialize a RTPContainer from the RandomPositionFactory and execute from there.
*/
public class RandomPositionLocator implements Runnable
{
private final RTPContainer contain;
public RandomPositionLocator(RTPContainer rtp)
{
contain=rtp;
}
@Override
public void run() {
if(!OTEMod.ALIVE)return;
ChatServerOverride.broadcastTo(contain.container.PlayerInst.getUUID(), new TextComponent(OTEMod.OTEPrefix + ChatColor.doColors(" !Dark_Purple!Searching... Attempt "+String.valueOf(contain.tries)+"/30")), OTEMod.THE_SERVER);
ServerLevel levl = contain.container.Dimension;
ChunkAccess chunk = levl.getChunk(contain.container.world_pos.Position.asBlockPos());
ChunkPos cpos = chunk.getPos();
boolean needsLoading = false;
needsLoading = !(levl.getForcedChunks().contains(cpos.toLong()));
if(needsLoading)
levl.setChunkForced(cpos.x, cpos.z, true);
int curChecks=0;
while(curChecks<30)
{
if(contain.isSafe(contain.container.world_pos.Position.asBlockPos()))
{
contain.complete=true;
if(needsLoading)
levl.setChunkForced(cpos.x, cpos.z, false);
if(MinecraftForge.EVENT_BUS.post(new RTPEvent(contain.container.PlayerInst, contain.container.world_pos)))
{
contain.complete=false;
ChatServerOverride.broadcastTo(contain.container.PlayerInst.getUUID(), new TextComponent(OTEMod.OTEPrefix + ChatColor.doColors(" !Dark_Red!Last position checked was probably claimed. Another mod has asked us not to send you to that location, continuing the search")), OTEMod.THE_SERVER);
break;
}
return;
}else {
curChecks++;
contain.move();
OTEMod.LOGGER.info("[DEBUG] "+ChatColor.doColors("!Dark_Red!Checking position: "+contain.container.world_pos.Position.toString()+"; "+contain.container.Dimension.getBlockState(contain.container.world_pos.Position.asBlockPos()).getBlock().toString()+"; "+contain.container.Dimension.getBlockState(contain.container.world_pos.Position.asBlockPos().below()).getBlock().toString()));
}
}
if(needsLoading)
levl.setChunkForced(cpos.x, cpos.z, false);
contain.newPosition();
if(contain.tries > 30)
{
// Abort
ChatServerOverride.broadcastTo(contain.container.PlayerInst.getUUID(), new TextComponent(OTEMod.OTEPrefix + ChatColor.doColors(" !Dark_Red!Could not find a suitable location in 30 attempts")), OTEMod.THE_SERVER);
contain.aborted=true;
return;
}else {
// Schedule the task to execute
//run();
RandomPositionLocator next = new RandomPositionLocator(contain);
OTEMod.delayedExecutor.schedule(next, 2);
}
}
}

View file

@ -0,0 +1,6 @@
package dev.zontreck.otemod.implementation.warps;
public class NoSuchWarpException extends Exception
{
}

View file

@ -0,0 +1,41 @@
package dev.zontreck.otemod.implementation.warps;
import java.util.UUID;
import dev.zontreck.libzontreck.exceptions.InvalidDeserialization;
import dev.zontreck.otemod.database.TeleportDestination;
import net.minecraft.nbt.CompoundTag;
public class Warp {
public UUID owner;
public String WarpName;
public boolean RTP;
public boolean isPublic;
public TeleportDestination destination;
public Warp(UUID owner, String name, boolean rtp, boolean publicWarp, TeleportDestination destination)
{
this.owner=owner;
WarpName=name;
RTP=rtp;
isPublic=publicWarp;
this.destination=destination;
}
public static Warp deserialize(CompoundTag tag) throws InvalidDeserialization
{
return new Warp(tag.getUUID("owner"), tag.getString("name"), tag.getBoolean("rtp"), tag.getBoolean("public"), new TeleportDestination(tag.getCompound("destination")));
}
public CompoundTag serialize()
{
CompoundTag tag = new CompoundTag();
tag.putUUID("owner", owner);
tag.putString("name", WarpName);
tag.putBoolean("rtp", RTP);
tag.putBoolean("public", isPublic);
tag.put("destination", destination.serialize());
return tag;
}
}

View file

@ -0,0 +1,92 @@
package dev.zontreck.otemod.implementation.warps;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
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;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
public class Warps
{
private Map<String,Warp> warps = new HashMap<>();
private Warps(){
}
public Warp getNamedWarp(String name) throws NoSuchWarpException
{
if(warps.containsKey(name))
return warps.get(name);
else throw new NoSuchWarpException();
}
public void add(Warp w)
{
warps.put(w.WarpName,w);
WarpsProvider.updateFile();
WarpCreatedEvent e = new WarpCreatedEvent(w);
OTEMod.bus.post(e);
}
public Map<String, Warp> get()
{
return new HashMap<String, Warp>(warps);
}
public void delete(Warp w)
{
warps.remove(w.WarpName);
WarpsProvider.updateFile();
WarpDeletedEvent e = new WarpDeletedEvent(w);
OTEMod.bus.post(e);
}
public static Warps getNew()
{
return new Warps();
}
public CompoundTag serialize()
{
CompoundTag tag = new CompoundTag();
ListTag lst = new ListTag();
for(Map.Entry<String, Warp> entry : warps.entrySet())
{
lst.add(entry.getValue().serialize());
}
tag.put("warps", lst);
return tag;
}
public static Warps deserialize(CompoundTag tag)
{
Warps w = new Warps();
ListTag lst = tag.getList("warps", Tag.TAG_COMPOUND);
for (Tag tag2 : lst) {
Warp warp;
try {
warp = Warp.deserialize((CompoundTag)tag2);
w.warps.put(warp.WarpName, warp);
} catch (InvalidDeserialization e) {
e.printStackTrace();
}
}
return w;
}
}

View file

@ -0,0 +1,58 @@
package dev.zontreck.otemod.implementation.warps;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import dev.zontreck.otemod.database.FileTreeDatastore;
import net.minecraft.nbt.NbtIo;
public class WarpsProvider extends FileTreeDatastore
{
public static final Path BASE = of("warps");
public static final Path WARPS_DATA = BASE.resolve("warps.nbt");
public static final Warps WARPS_INSTANCE;
static{
if(!BASE.toFile().exists()){
try {
Files.createDirectory(BASE);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
WARPS_INSTANCE = getOrCreate();
}
/**
* Creates a new warps instance, or returns a fully deserialized instance
* @return
*/
private static Warps getOrCreate()
{
Warps instance = null;
if(WARPS_DATA.toFile().exists())
{
try{
instance= Warps.deserialize(NbtIo.read(WARPS_DATA.toFile()));
}catch(Exception e){
instance=Warps.getNew();
}
}else {
instance=Warps.getNew();
}
return instance;
}
public static void updateFile()
{
try {
NbtIo.write(WARPS_INSTANCE.serialize(), WARPS_DATA.toFile());
} catch (IOException e) {
e.printStackTrace();
}
}
}