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
|
@ -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;
|
||||
}
|
||||
}
|
Reference in a new issue