Track player position for a custom event
This commit is contained in:
parent
4eee51709c
commit
b01d45f9ab
8 changed files with 214 additions and 3 deletions
|
@ -5,5 +5,5 @@ org.gradle.daemon=false
|
|||
|
||||
mc_version=1.19.2
|
||||
forge_version=43.2.3
|
||||
myversion=1.0.4.3
|
||||
myversion=1.0.4.4
|
||||
parchment_version=2022.11.27
|
|
@ -4,8 +4,13 @@ import org.slf4j.Logger;
|
|||
|
||||
import com.mojang.logging.LogUtils;
|
||||
|
||||
import dev.zontreck.libzontreck.events.PlayerChangedPositionEvent;
|
||||
import dev.zontreck.libzontreck.memory.PlayerContainer;
|
||||
import dev.zontreck.libzontreck.memory.VolatilePlayerStorage;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.entity.living.LivingEvent;
|
||||
import net.minecraftforge.event.server.ServerStartedEvent;
|
||||
import net.minecraftforge.eventbus.api.IEventBus;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
|
@ -18,9 +23,10 @@ public class LibZontreck {
|
|||
public static final Logger LOGGER = LogUtils.getLogger();
|
||||
public static final String MOD_ID = "libzontreck";
|
||||
public static MinecraftServer THE_SERVER;
|
||||
public static VolatilePlayerStorage playerStorage;
|
||||
|
||||
public LibZontreck(){
|
||||
|
||||
LibZontreck.playerStorage=new VolatilePlayerStorage();
|
||||
IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
|
||||
// Register the setup method for modloading
|
||||
bus.addListener(this::setup);
|
||||
|
@ -39,4 +45,29 @@ public class LibZontreck {
|
|||
{
|
||||
THE_SERVER = event.getServer();
|
||||
}
|
||||
|
||||
|
||||
@Mod.EventBusSubscriber(modid = LibZontreck.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
|
||||
public static class ForgeEventBus
|
||||
{
|
||||
@SubscribeEvent
|
||||
public void onPlayerTick(LivingEvent.LivingTickEvent ev)
|
||||
{
|
||||
if(ev.getEntity().level.isClientSide)return;
|
||||
|
||||
if(ev.getEntity() instanceof ServerPlayer)
|
||||
{
|
||||
ServerPlayer player = (ServerPlayer)ev.getEntity();
|
||||
PlayerContainer cont = LibZontreck.playerStorage.get(player.getUUID());
|
||||
|
||||
if(cont.player.positionChanged())
|
||||
{
|
||||
cont.player.update();
|
||||
|
||||
PlayerChangedPositionEvent pcpe = new PlayerChangedPositionEvent(player, cont.player.position, cont.player.lastPosition);
|
||||
MinecraftForge.EVENT_BUS.post(pcpe);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package dev.zontreck.libzontreck.events;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import dev.zontreck.libzontreck.vectors.Vector3;
|
||||
import dev.zontreck.libzontreck.vectors.WorldPosition;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraftforge.eventbus.api.Event;
|
||||
|
||||
public class PlayerChangedPositionEvent extends Event
|
||||
{
|
||||
public Player player;
|
||||
public WorldPosition position;
|
||||
public WorldPosition lastPosition;
|
||||
|
||||
|
||||
public PlayerChangedPositionEvent (Player current, WorldPosition pos, WorldPosition last)
|
||||
{
|
||||
player=current;
|
||||
position=pos;
|
||||
lastPosition=last;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package dev.zontreck.libzontreck.memory;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import dev.zontreck.libzontreck.LibZontreck;
|
||||
import dev.zontreck.libzontreck.exceptions.InvalidDeserialization;
|
||||
import dev.zontreck.libzontreck.vectors.WorldPosition;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
|
||||
public class PlayerComponent
|
||||
{
|
||||
public ServerPlayer player;
|
||||
public WorldPosition position;
|
||||
public WorldPosition lastPosition;
|
||||
|
||||
public PlayerComponent(ServerPlayer play)
|
||||
{
|
||||
player=play;
|
||||
position = new WorldPosition(play);
|
||||
}
|
||||
|
||||
public boolean positionChanged()
|
||||
{
|
||||
WorldPosition wp = new WorldPosition(player);
|
||||
return !(wp.same(position));
|
||||
}
|
||||
|
||||
public void update()
|
||||
{
|
||||
lastPosition=position;
|
||||
position = new WorldPosition(player);
|
||||
}
|
||||
|
||||
public static PlayerComponent fromID(UUID ID)
|
||||
{
|
||||
return new PlayerComponent(LibZontreck.THE_SERVER.getPlayerList().getPlayer(ID));
|
||||
}
|
||||
|
||||
public CompoundTag serialize()
|
||||
{
|
||||
CompoundTag tag = new CompoundTag();
|
||||
tag.putUUID("id", player.getUUID());
|
||||
tag.put("pos", position.serialize());
|
||||
return tag;
|
||||
}
|
||||
|
||||
public static PlayerComponent deserialize(CompoundTag tag)
|
||||
{
|
||||
PlayerComponent comp = PlayerComponent.fromID(tag.getUUID("id"));
|
||||
try {
|
||||
comp.position = new WorldPosition(tag.getCompound("pos"), false);
|
||||
} catch (InvalidDeserialization e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
return comp;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package dev.zontreck.libzontreck.memory;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import dev.zontreck.libzontreck.LibZontreck;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
|
||||
public class PlayerContainer {
|
||||
public UUID ID;
|
||||
public PlayerComponent player;
|
||||
public CompoundTag miscData;
|
||||
|
||||
public PlayerContainer(UUID ID)
|
||||
{
|
||||
this(LibZontreck.THE_SERVER.getPlayerList().getPlayer(ID));
|
||||
}
|
||||
public PlayerContainer(ServerPlayer player)
|
||||
{
|
||||
this.player = new PlayerComponent(player);
|
||||
miscData=new CompoundTag();
|
||||
ID = player.getUUID();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
package dev.zontreck.libzontreck.memory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
// This class is a universal data storage class for per-player data storage
|
||||
// If a player logs out, this memory gets erased. Do not store anything here that requires persistence. Store it on the player instead.
|
||||
public class VolatilePlayerStorage {
|
||||
private List<PlayerContainer> datas = new ArrayList<>();
|
||||
|
||||
|
||||
public void update(UUID player, PlayerContainer comp)
|
||||
{
|
||||
int indexOf = index(player);
|
||||
if(indexOf!=-1)
|
||||
datas.set(indexOf, comp);
|
||||
}
|
||||
|
||||
public PlayerContainer getNew(UUID player)
|
||||
{
|
||||
if(index(player)==-1)
|
||||
{
|
||||
PlayerContainer comp = new PlayerContainer(player);
|
||||
datas.add(comp);
|
||||
return comp;
|
||||
}else return get(player);
|
||||
}
|
||||
|
||||
public PlayerContainer get(UUID player)
|
||||
{
|
||||
int indexOf = index(player);
|
||||
if(indexOf!=-1)
|
||||
{
|
||||
return datas.get(indexOf);
|
||||
}else return getNew(player);
|
||||
}
|
||||
|
||||
public void delete(UUID id)
|
||||
{
|
||||
int index = index(id);
|
||||
if(index!=-1)
|
||||
{
|
||||
datas.remove(index);
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the index of the component by ID, or -1 if not found
|
||||
public int index(UUID id)
|
||||
{
|
||||
int ret=-1;
|
||||
Iterator<PlayerContainer> it = datas.iterator();
|
||||
while(it.hasNext())
|
||||
{
|
||||
PlayerContainer comp = it.next();
|
||||
if(comp.ID.equals(id))
|
||||
{
|
||||
ret=datas.indexOf(comp);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ import net.minecraft.nbt.CompoundTag;
|
|||
import net.minecraft.nbt.NbtUtils;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
|
||||
public class WorldPosition
|
||||
{
|
||||
|
@ -36,6 +37,11 @@ public class WorldPosition
|
|||
calcDimSafe();
|
||||
}
|
||||
|
||||
public WorldPosition(ServerPlayer player)
|
||||
{
|
||||
this(new Vector3(player.position()), player.getLevel());
|
||||
}
|
||||
|
||||
public WorldPosition(Vector3 pos, ServerLevel lvl)
|
||||
{
|
||||
Position=pos;
|
||||
|
|
|
@ -19,7 +19,7 @@ modId="libzontreck" #mandatory
|
|||
# The version number of the mod - there's a few well known ${} variables useable here or just hardcode it
|
||||
# ${file.jarVersion} will substitute the value of the Implementation-Version as read from the mod's JAR file metadata
|
||||
# see the associated build.gradle script for how to populate this completely automatically during a build
|
||||
version="1.0.4.3" #mandatory
|
||||
version="1.0.4.4" #mandatory
|
||||
# A display name for the mod
|
||||
displayName="LibZontreck" #mandatory
|
||||
# A URL to query for updates for this mod. See the JSON update specification https://mcforge.readthedocs.io/en/latest/gettingstarted/autoupdate/
|
||||
|
|
Reference in a new issue