Skip to content

Commit

Permalink
More meta and watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Dec 13, 2023
1 parent 95d3440 commit d68705d
Show file tree
Hide file tree
Showing 23 changed files with 371 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import xiamomc.morph.MorphPlugin;
import xiamomc.morph.MorphPluginObject;
import xiamomc.morph.backends.server.renderer.network.RegistryParameters;
import xiamomc.morph.backends.server.renderer.network.RenderRegistry;
import xiamomc.morph.backends.server.renderer.network.listeners.SpawnPacketHandler;
import xiamomc.morph.backends.server.renderer.network.ProtocolHandler;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.Watchers;
import xiamomc.morph.backends.server.renderer.network.datawatcher.WatcherIndex;
import xiamomc.morph.backends.server.renderer.skins.SkinStore;
import xiamomc.pluginbase.Managers.DependencyManager;

public class ServerRenderer extends MorphPluginObject
{
Expand All @@ -30,7 +27,7 @@ public ServerRenderer()
public void renderEntity(Player player, EntityType entityType, String name)
{
registry.register(player.getUniqueId(),
new RegistryParameters(entityType, name, Watchers.getWatcherForType(player, entityType)));
new RegistryParameters(entityType, name, WatcherIndex.getInstance().getWatcherForType(player, entityType)));
}

public void unRenderEntity(Player player)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
package xiamomc.morph.backends.server.renderer.network;

import com.comphenix.protocol.ProtocolLibrary;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xiamomc.morph.MorphPluginObject;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.SingleWatcher;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.Watchers;
import xiamomc.morph.backends.server.renderer.network.listeners.MetaPacketListener;
import xiamomc.morph.backends.server.renderer.network.listeners.SpawnPacketHandler;
import xiamomc.pluginbase.Annotations.Initializer;
import xiamomc.pluginbase.Exceptions.NullDependencyException;

import java.util.Map;
import java.util.UUID;

public class ProtocolHandler extends MorphPluginObject
{
Expand All @@ -34,6 +23,8 @@ private void load()
var protocolMgr = ProtocolLibrary.getProtocolManager();

protocolMgr.addPacketListener(morphPacketListener);
protocolMgr.addPacketListener(new MetaPacketListener());
//protocolMgr.addPacketListener(new TestPacketListener());
}

private boolean disposed;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package xiamomc.morph.backends.server.renderer.network;

import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.world.entity.PlayerRideable;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xiamomc.morph.MorphPluginObject;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.SingleWatcher;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.Watchers;
import xiamomc.morph.backends.server.renderer.network.datawatcher.WatcherIndex;
import xiamomc.pluginbase.Exceptions.NullDependencyException;

import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Consumer;

Expand Down Expand Up @@ -89,7 +87,7 @@ public void unregister(UUID uuid)
*/
public void register(@NotNull Player player, @NotNull org.bukkit.entity.EntityType bukkitType)
{
var watcher = Watchers.getWatcherForType(player, bukkitType);
var watcher = WatcherIndex.getInstance().getWatcherForType(player, bukkitType);
register(player.getUniqueId(), bukkitType, watcher);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private void load()
@Override
public void onPacketSending(PacketEvent event)
{
logger.info("SEND! " + event.getPacket().getHandle());
logger.info("SEND! type is '%s' handle is '%s'".formatted(event.getPacketType(), event.getPacket().getHandle()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package xiamomc.morph.backends.server.renderer.network.datawatcher;

import xiamomc.morph.backends.server.renderer.network.datawatcher.values.AbstractValues;
import xiamomc.morph.backends.server.renderer.network.datawatcher.values.EntityValues;
import xiamomc.morph.backends.server.renderer.network.datawatcher.values.LivingEntityValues;
import xiamomc.morph.backends.server.renderer.network.datawatcher.values.PlayerValues;
import xiamomc.morph.backends.server.renderer.network.datawatcher.values.*;

public class ValueIndex
{
public static final ArmorStandValues ARMOR_STAND = new ArmorStandValues();
public static final AllayValues ALLAY = new AllayValues();
public static final PlayerValues PLAYER = new PlayerValues();
public static final LivingEntityValues BASE_LIVING = new LivingEntityValues();
public static final EntityValues BASE_ENTITY = new EntityValues();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package xiamomc.morph.backends.server.renderer.network.datawatcher;

import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import xiamomc.morph.backends.server.renderer.network.datawatcher.values.ArmorStandValues;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.SingleWatcher;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.AllayWatcher;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.ArmorStandWatcher;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.LivingEntityWatcher;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.PlayerWatcher;

import java.util.Map;
import java.util.function.Function;

public class WatcherIndex
{
public static WatcherIndex getInstance()
{
if (instance == null) instance = new WatcherIndex();
return instance;
}

private static WatcherIndex instance;

public WatcherIndex()
{
instance = this;

setTypeWatcher(EntityType.PLAYER, PlayerWatcher::new);
setTypeWatcher(EntityType.ALLAY, AllayWatcher::new);
setTypeWatcher(EntityType.ARMOR_STAND, ArmorStandWatcher::new);
}

private void setTypeWatcher(EntityType type, Function<Player, SingleWatcher> func)
{
typeWatcherMap.put(type, func);
}

private final Map<EntityType, Function<Player, SingleWatcher>> typeWatcherMap = new Object2ObjectOpenHashMap<>();

public SingleWatcher getWatcherForType(Player bindingPlayer, EntityType entityType)
{
var watcherFunc = typeWatcherMap.getOrDefault(entityType, null);

if (watcherFunc == null)
return new LivingEntityWatcher(bindingPlayer, entityType);

return watcherFunc.apply(bindingPlayer);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
package xiamomc.morph.backends.server.renderer.network.datawatcher.values;

import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.core.BlockPos;
import org.slf4j.Logger;
import xiamomc.morph.MorphPlugin;
import xiamomc.morph.backends.server.renderer.utilties.ProtocolRegistryUtils;

import java.util.List;

public abstract class AbstractValues
{
private int currentIndex = 0;
protected <X> SingleValue<X> getSingle(X val)
{
if (val == null)
throw new IllegalArgumentException("May not pass a null value to getIndex()");

var sv = SingleValue.of(currentIndex, val);

logger.info(this.getClass().getSimpleName() + " :: Value '%s' class '%s' is on Index '%s'"
.formatted(val, val.getClass(), currentIndex));

currentIndex++;
return sv;
}

protected final Logger logger = MorphPlugin.getInstance().getSLF4JLogger();

protected final List<SingleValue<?>> values = new ObjectArrayList<>();

protected void registerValue(SingleValue<?>... value)
protected void registerSingle(SingleValue<?>... value)
{
for (SingleValue<?> singleValue : value)
registerSingle(singleValue);
Expand All @@ -30,7 +46,7 @@ protected void registerSingle(SingleValue<?> value)
}
catch (Throwable t)
{
MorphPlugin.getInstance().getSLF4JLogger().warn("No serializer for type '%s'!".formatted(value.type()));
logger.warn("No serializer for type '%s'!".formatted(value.type()));
}

values.add(value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package xiamomc.morph.backends.server.renderer.network.datawatcher.values;

public class AllayValues extends MobValues
{
public final SingleValue<Boolean> DANCING = getSingle(false);
public final SingleValue<Boolean> CAN_DUPLICATE = getSingle(false);

public AllayValues()
{
super();

this.registerSingle(DANCING);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package xiamomc.morph.backends.server.renderer.network.datawatcher.values;

import net.minecraft.core.Rotations;

public class ArmorStandValues extends LivingEntityValues
{
public final SingleValue<Byte> DATA_FLAGS = getSingle((byte)0);
public final SingleValue<Rotations> HEAD_ROTATION = getSingle(new Rotations(0, 0, 0));
public final SingleValue<Rotations> BODY_ROTATION = getSingle(new Rotations(0, 0, 0));
public final SingleValue<Rotations> LEFT_ARM_ROTATION = getSingle(new Rotations(-10, 0, -10));
public final SingleValue<Rotations> RIGHT_ARM_ROTATION = getSingle(new Rotations(-15, 0, 10));
public final SingleValue<Rotations> LEFT_LEG_ROTATION = getSingle(new Rotations(-1, 0, -1));
public final SingleValue<Rotations> RIGHT_LEG_ROTATION = getSingle(new Rotations(1, 0, 1));

public ArmorStandValues()
{
super();

registerSingle(DATA_FLAGS,
HEAD_ROTATION, BODY_ROTATION,
LEFT_ARM_ROTATION, RIGHT_ARM_ROTATION,
LEFT_LEG_ROTATION, RIGHT_LEG_ROTATION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@

public class EntityValues extends AbstractValues
{
public final SingleValue<Byte> GENERAL = SingleValue.of(0, (byte)0);
public final SingleValue<Integer> AIR_TICKS = SingleValue.of(1, 0);
public final SingleValue<Byte> GENERAL = getSingle((byte)0);
public final SingleValue<Integer> AIR_TICKS = getSingle(0);

@Deprecated
public final SingleValue<Optional<Component>> CUSTOMNAME = SingleValue.of(2, Optional.of(Component.empty()));
public final SingleValue<Optional<Component>> CUSTOMNAME = getSingle(Optional.of(Component.empty()));

public final SingleValue<Boolean> CUSTOMNAME_VISIBLE = SingleValue.of(3, false);
public final SingleValue<Boolean> SILENT = SingleValue.of(4, false);
public final SingleValue<Boolean> NO_GRAVITY = SingleValue.of(5, false);
public final SingleValue<Pose> POSE = SingleValue.of(6, Pose.STANDING);
public final SingleValue<Integer> FROZEN_TICKS = SingleValue.of(7, 0);
public final SingleValue<Boolean> CUSTOMNAME_VISIBLE = getSingle(false);
public final SingleValue<Boolean> SILENT = getSingle(false);
public final SingleValue<Boolean> NO_GRAVITY = getSingle(false);
public final SingleValue<Pose> POSE = getSingle(Pose.STANDING);
public final SingleValue<Integer> FROZEN_TICKS = getSingle(0);

public EntityValues()
{
registerValue(GENERAL, AIR_TICKS, CUSTOMNAME, CUSTOMNAME_VISIBLE, SILENT, NO_GRAVITY,
registerSingle(GENERAL, AIR_TICKS, CUSTOMNAME, CUSTOMNAME_VISIBLE, SILENT, NO_GRAVITY,
POSE, FROZEN_TICKS);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package xiamomc.morph.backends.server.renderer.network.datawatcher.values;

import net.minecraft.core.BlockPos;
import org.bukkit.Location;

import java.util.Optional;

public class LivingEntityValues extends AbstractValues
public class LivingEntityValues extends EntityValues
{
public final SingleValue<Byte> LIVING_FLAGS = SingleValue.of(8, (byte)0);
public final SingleValue<Float> HEALTH = SingleValue.of(9, 1f);
public final SingleValue<Integer> POTION_COLOR = SingleValue.of(10, 0);
public final SingleValue<Boolean> POTION_ISAMBIENT = SingleValue.of(11, false);
public final SingleValue<Integer> STUCKED_ARROWS = SingleValue.of(12, 0);
public final SingleValue<Integer> BEE_STINGERS = SingleValue.of(13, 0);
public final SingleValue<Optional<BlockPos>> BED_POS = SingleValue.of(14, Optional.of(new BlockPos(0,0,0)));
public final SingleValue<Byte> LIVING_FLAGS = getSingle((byte)0);
public final SingleValue<Float> HEALTH = getSingle(1f);
public final SingleValue<Integer> POTION_COLOR = getSingle(0);
public final SingleValue<Boolean> POTION_ISAMBIENT = getSingle(false);
public final SingleValue<Integer> STUCKED_ARROWS = getSingle(0);
public final SingleValue<Integer> BEE_STINGERS = getSingle(0);
public final SingleValue<Optional<BlockPos>> BED_POS = getSingle(Optional.of(new BlockPos(0,0,0)));

public LivingEntityValues()
{
super();

registerValue(LIVING_FLAGS, HEALTH, POTION_COLOR, POTION_ISAMBIENT, STUCKED_ARROWS, BED_POS, BEE_STINGERS);
registerSingle(LIVING_FLAGS, HEALTH, POTION_COLOR, POTION_ISAMBIENT, STUCKED_ARROWS, BED_POS, BEE_STINGERS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package xiamomc.morph.backends.server.renderer.network.datawatcher.values;

public class MobValues extends LivingEntityValues
{
public final SingleValue MOB_FLAGS = getSingle((byte)0);

public MobValues()
{
super();

this.registerSingle(MOB_FLAGS);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package xiamomc.morph.backends.server.renderer.network.datawatcher.values;

public class PlayerValues extends AbstractValues
public class PlayerValues extends LivingEntityValues
{
public final SingleValue<Float> ABSORPTION_AMOUNT = SingleValue.of(15, 0f);
public final SingleValue<Integer> SCORE = SingleValue.of(16, 0);
public final SingleValue<Byte> SKIN = SingleValue.of(17, (byte)127);
public final SingleValue<Byte> MAINHAND = SingleValue.of(18, (byte)1);
public final SingleValue<Float> ABSORPTION_AMOUNT = getSingle(0f);
public final SingleValue<Integer> SCORE = getSingle(0);
public final SingleValue<Byte> SKIN = getSingle((byte)0); //127
public final SingleValue<Byte> MAINHAND = getSingle((byte)1);

public PlayerValues()
{
super();

registerValue(ABSORPTION_AMOUNT, SCORE, SKIN, MAINHAND);
System.out.println("SKIN is on index " + SKIN.index());

registerSingle(ABSORPTION_AMOUNT, SCORE, SKIN, MAINHAND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected boolean register(SingleValue<?> singleValue)
return true;
}

public void write(SingleValue<?> singleValue, Object value)
public <X> void write(SingleValue<X> singleValue, X value)
{
write(singleValue.index(), value);
}
Expand Down

This file was deleted.

Loading

0 comments on commit d68705d

Please sign in to comment.