Skip to content

Commit

Permalink
Living flags; Don't put values that doesn't change to dirtySingles
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Dec 17, 2023
1 parent 2292851 commit c018cb0
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ public void write(int index, Object value)

var prev = registry.getOrDefault(single, null);
registry.put(single, value);
dirtySingles.put(single, value);

if (!value.equals(prev))
dirtySingles.put(single, value);

onTrackerWrite(index, prev, value);

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

import io.papermc.paper.event.player.PlayerStopUsingItemEvent;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.MainHand;
import org.bukkit.potion.PotionEffect;
import xiamomc.morph.backends.server.renderer.network.datawatcher.ValueIndex;
import xiamomc.morph.misc.NmsRecord;

public class LivingEntityWatcher extends EntityWatcher
import java.util.List;
import java.util.Map;

public class LivingEntityWatcher extends EntityWatcher implements Listener
{
public LivingEntityWatcher(Player bindingPlayer, EntityType entityType)
{
super(bindingPlayer, entityType);

Bukkit.getPluginManager().registerEvents(this, plugin);
}

@Override
protected void initRegistry()
{
Expand All @@ -15,19 +38,70 @@ protected void initRegistry()
register(ValueIndex.BASE_LIVING);
}

public LivingEntityWatcher(Player bindingPlayer, EntityType entityType)
private final Map<Player, EquipmentSlot> handMap = new Object2ObjectOpenHashMap<>();

@EventHandler
public void onPlayerStartUsingItem(PlayerInteractEvent e)
{
super(bindingPlayer, entityType);
handMap.put(e.getPlayer(), e.getHand());
}

@Override
protected void doSync()
{
var player = getBindingPlayer();
var nmsPlayer = NmsRecord.ofPlayer(player);
var values = ValueIndex.BASE_LIVING;

write(values.HEALTH, (float)player.getHealth());

var flagBit = 0x00;

if (nmsPlayer.isUsingItem())
{
flagBit |= 0x01;

var handInUse = handMap.remove(player);

if (handInUse == null)
{
logger.warn("No hand in use but using item? Defaulting to HAND");
handInUse = EquipmentSlot.HAND;
}

boolean isOffhand = handInUse == EquipmentSlot.OFF_HAND;
if (isOffhand) flagBit |= 0x02;
}

if (player.isRiptiding())
flagBit |= 0x04;

write(values.LIVING_FLAGS, (byte)flagBit);

int potionColor = 0;
List<Color> colors = new ObjectArrayList<>();
boolean hasAmbient = false;
for (PotionEffect effect : player.getActivePotionEffects())
{
colors.add(effect.getType().getColor());

if (effect.isAmbient())
hasAmbient = effect.isAmbient();
}

if (!colors.isEmpty())
{
var firstColor = colors.remove(0);
var finalColor = firstColor.mixColors(colors.toArray(new Color[]{}));
potionColor = finalColor.asRGB();
}

write(values.POTION_COLOR, potionColor);
write(values.POTION_ISAMBIENT, hasAmbient);

write(values.STUCKED_ARROWS, player.getArrowsInBody());
write(values.BEE_STINGERS, player.getBeeStingersInBody());

super.doSync();
}
}

0 comments on commit c018cb0

Please sign in to comment.