Add initial implementation of TPA

This commit is contained in:
Zontreck 2022-10-04 20:05:52 -07:00
parent 707b99f58d
commit 3c55b638af
4 changed files with 180 additions and 2 deletions

View file

@ -3,14 +3,18 @@ package dev.zontreck.otemod;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.mojang.logging.LogUtils;
import com.mojang.serialization.Codec;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.world.BiomeModifier;
@ -23,6 +27,7 @@ import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.server.ServerStartingEvent;
import net.minecraftforge.event.server.ServerStoppingEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
@ -30,6 +35,7 @@ import net.minecraftforge.registries.ForgeRegistries;
import org.slf4j.Logger;
import dev.zontreck.otemod.blocks.ModBlocks;
import dev.zontreck.otemod.chat.ChatColor;
import dev.zontreck.otemod.chat.ChatServerOverride;
import dev.zontreck.otemod.commands.DelHomeCommand;
import dev.zontreck.otemod.commands.FlyCommand;
@ -41,6 +47,7 @@ import dev.zontreck.otemod.commands.profilecmds.NameColorCommand;
import dev.zontreck.otemod.commands.profilecmds.NickCommand;
import dev.zontreck.otemod.commands.profilecmds.PrefixColorCommand;
import dev.zontreck.otemod.commands.profilecmds.PrefixCommand;
import dev.zontreck.otemod.commands.teleport.TeleportContainer;
import dev.zontreck.otemod.configs.OTEServerConfig;
import dev.zontreck.otemod.configs.Profile;
import dev.zontreck.otemod.database.Database;
@ -61,6 +68,10 @@ public class OTEMod
public static final ResourceLocation MODIFY_BIOMES_RL = new ResourceLocation(OTEMod.MOD_ID, MODIFY_BIOMES);
public static Database DB=null;
public static Map<String,Profile> PROFILES = new HashMap<String,Profile>();
public static List<TeleportContainer> TeleportRegistry = new ArrayList<>();
public static MinecraftServer THE_SERVER;
private static boolean ALIVE;
public OTEMod()
@ -162,7 +173,7 @@ public class OTEMod
PrefixCommand.register(ev.getDispatcher());
NickCommand.register(ev.getDispatcher());
}
// You can use SubscribeEvent and let the Event Bus discover methods to call
@ -174,7 +185,7 @@ public class OTEMod
try {
OTEMod.DB = new Database(this);
OTEMod.ALIVE=true;
// Validate that the database has been established and that tables exist
Connection con = OTEMod.DB.getConnection();
con.setAutoCommit(true);
@ -195,6 +206,37 @@ public class OTEMod
" `dimension` varchar(25) NOT NULL)");
con.endRequest();
// Set up the repeating task to expire a TeleportContainer
OTEMod.THE_SERVER = event.getServer();
Thread th = new Thread(new Runnable(){
public void run()
{
while(OTEMod.ALIVE){
// Check if the teleports have expired
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for(TeleportContainer cont : OTEMod.TeleportRegistry){
if(cont.has_expired())
{
try{
Component expire = Component.literal(ChatColor.DARK_PURPLE+"Teleport request has expired");
ChatServerOverride.broadcastTo(cont.FromPlayer, expire, OTEMod.THE_SERVER);
ChatServerOverride.broadcastTo(cont.ToPlayer, expire, OTEMod.THE_SERVER);
OTEMod.TeleportRegistry.remove(cont);
}catch(Exception e){
break;
}
}
}
}
}
});
th.start();
} catch (DatabaseConnectionException | SQLException e) {
e.printStackTrace();
@ -203,6 +245,11 @@ public class OTEMod
}
}
@SubscribeEvent
public static void onStop(final ServerStoppingEvent ev)
{
OTEMod.ALIVE=false; // Tear down all looping threads that will watch this
}
// You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent
@Mod.EventBusSubscriber(modid = OTEMod.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
public static class ClientModEvents

View file

@ -4,6 +4,7 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.configs.PlayerFlyCache;
@ -141,4 +142,16 @@ public class ChatServerOverride {
play.displayClientMessage(message, false); // Send the message!
}
}
public static void broadcastTo(UUID ID, Component message, MinecraftServer s)
{
ServerPlayer play = s.getPlayerList().getPlayer(ID);
play.displayClientMessage(message, false); // Send the message!
}
public static void broadcastToAbove(UUID ID, Component message, MinecraftServer s)
{
ServerPlayer play = s.getPlayerList().getPlayer(ID);
play.displayClientMessage(message, true); // Send the message!
}
}

View file

@ -1,5 +1,85 @@
package dev.zontreck.otemod.commands;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.chat.ChatColor;
import dev.zontreck.otemod.chat.ChatServerOverride;
import dev.zontreck.otemod.chat.Clickable;
import dev.zontreck.otemod.chat.HoverTip;
import dev.zontreck.otemod.commands.teleport.TeleportContainer;
import dev.zontreck.otemod.configs.Profile;
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.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(Component.literal(ChatColor.DARK_RED+"Error: Player not found"));
return 1;
}
TeleportContainer cont = new TeleportContainer(source.getPlayer().getUUID(), serverPlayer.getUUID());
for(TeleportContainer cont2 : OTEMod.TeleportRegistry){
if(cont2.compareTo(cont)==0){
ChatServerOverride.broadcastTo(cont.FromPlayer, Component.literal(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, Component.literal(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");
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, Component.literal(ChatColor.BOLD + ChatColor.DARK_GREEN +"TPA Request Sent! ").append(Component.literal(ChatColor.BOLD+ChatColor.DARK_GRAY+"["+ChatColor.DARK_RED+"X"+ChatColor.DARK_GRAY+"]").setStyle(s)), serverPlayer.server);
ce = Clickable.command("tpaccept");
he = HoverTip.get(ChatColor.DARK_GREEN + "Accept tp request");
ClickEvent ce2 = Clickable.command("tpdeny");
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 = Profile.get_profile_of(cont.FromPlayer.toString());
ChatServerOverride.broadcastTo(cont.ToPlayer, Component.literal(p.name_color+p.nickname + ChatColor.BOLD + ChatColor.DARK_PURPLE+" is requesting to teleport to you\n \n").
append(Component.literal(ChatColor.DARK_GRAY+"["+ChatColor.DARK_GREEN+"ACCEPT" + ChatColor.DARK_GRAY+"] ").setStyle(s)).
append(Component.literal(ChatColor.DARK_GRAY + "["+ChatColor.DARK_RED+"DENY"+ChatColor.DARK_GRAY+"]").setStyle(s2)), serverPlayer.server);
return 0;
}
private static int usage(CommandSourceStack source) {
source.sendSuccess(Component.literal("/tpa USAGE\n\n\t"+ChatColor.BOLD + ChatColor.DARK_GRAY+"/tpa "+ChatColor.DARK_RED+"target_player\n"), false);
return 0;
}
}

View file

@ -0,0 +1,38 @@
package dev.zontreck.otemod.commands.teleport;
import java.time.Instant;
import java.util.UUID;
public class TeleportContainer implements Comparable{
public UUID FromPlayer;
public UUID ToPlayer;
public long StartedAt;
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;
StartedAt = Instant.now().getEpochSecond();
}
@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;
}
}