Skip to content

Commit

Permalink
ghast
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Dec 14, 2023
1 parent 8bbfec4 commit 1962cb1
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import xiamomc.morph.MorphPlugin;
import xiamomc.morph.backends.DisguiseWrapper;
import xiamomc.morph.backends.server.renderer.network.datawatcher.ValueIndex;
import xiamomc.morph.backends.server.renderer.network.datawatcher.values.GhastValues;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.SingleWatcher;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.GhastWatcher;
import xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types.SlimeWatcher;
import xiamomc.morph.backends.server.renderer.network.registries.EntryIndex;
import xiamomc.morph.misc.DisguiseEquipment;
Expand Down Expand Up @@ -240,6 +242,18 @@ public boolean isSaddled()
return instance.saddled;
}

private boolean aggressive;

@Override
public void setAggressive(boolean aggressive)
{
super.setAggressive(aggressive);

this.aggressive = aggressive;
if (bindingWatcher instanceof GhastWatcher ghastWatcher)
ghastWatcher.write(ValueIndex.GHAST.CHARGING, aggressive);
}

private Player bindingPlayer;

public Player getBindingPlayer()
Expand Down Expand Up @@ -273,5 +287,8 @@ private void refreshRegistry()
bindingWatcher.write(EntryIndex.PROFILE, this.instance.profile);
bindingWatcher.write(EntryIndex.DISPLAY_FAKE_EQUIPMENT, shouldDisplayCustomEquipment);
bindingWatcher.write(EntryIndex.EQUIPMENT, this.equipment);

if (bindingWatcher instanceof GhastWatcher ghastWatcher)
ghastWatcher.write(ValueIndex.GHAST.CHARGING, aggressive);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public class ValueIndex
public static final AllayValues ALLAY = new AllayValues();
public static final PlayerValues PLAYER = new PlayerValues();
public static final SlimeValues SLIME_MAGMA = new SlimeValues();
public static final GhastValues GHAST = new GhastValues();
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public WatcherIndex()
setTypeWatcher(EntityType.ARMOR_STAND, ArmorStandWatcher::new);
setTypeWatcher(EntityType.SLIME, SlimeWatcher::new);
setTypeWatcher(EntityType.MAGMA_CUBE, MagmaWatcher::new);
setTypeWatcher(EntityType.GHAST, GhastWatcher::new);
}

private void setTypeWatcher(EntityType type, Function<Player, SingleWatcher> func)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ protected <X> SingleValue<X> getSingle(X val)

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;
}
Expand Down
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 GhastValues extends MobValues
{
public final SingleValue<Boolean> CHARGING = getSingle(false);

public GhastValues()
{
super();

registerSingle(CHARGING);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ public SingleValue(Class<T> type, int index, @NotNull T defaultValue)
this.defaultValue = defaultValue;
}

@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if (!(obj instanceof SingleValue<?> other)) return false;

return this.index == other.index && this.type.equals(other.type);
}

@Override
public String toString()
{
return "SingleValue[type='%s', index='%s']".formatted(type, index);
}

public static <TVal> SingleValue<TVal> of(int index, @NotNull TVal val)
{
if (val == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package xiamomc.morph.backends.server.renderer.network.datawatcher.watchers;

import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectLists;
import net.minecraft.world.level.GameType;
import org.bukkit.Bukkit;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
Expand All @@ -17,6 +20,7 @@
import xiamomc.morph.backends.server.renderer.network.registries.EntryIndex;
import xiamomc.morph.backends.server.renderer.network.registries.RegistryKey;
import xiamomc.morph.misc.DisguiseEquipment;
import xiamomc.morph.misc.NmsRecord;
import xiamomc.pluginbase.Annotations.Resolved;
import xiamomc.pluginbase.Exceptions.NullDependencyException;

Expand Down Expand Up @@ -128,14 +132,14 @@ protected boolean register(AbstractValues values)
{
var allSuccess = true;
for (SingleValue<?> value : values.getValues())
allSuccess = allSuccess && register(value);
allSuccess = register(value) && allSuccess;

return allSuccess;
}

protected boolean register(SingleValue<?> singleValue)
{
if (registry.containsKey(singleValue)) return false;
if (registry.keySet().stream().anyMatch(sv -> sv.index() == singleValue.index())) return false;

registry.put(singleValue, singleValue.defaultValue());
return true;
Expand All @@ -155,8 +159,15 @@ public void write(int index, Object value)
if (!single.defaultValue().getClass().isInstance(value))
throw new IllegalArgumentException("Incompatable value for index '%s', excepted for '%s', but got '%s'".formatted(index, single.defaultValue().getClass(), value.getClass()));

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

onTrackerWrite(index, prev, value);
}

protected void onTrackerWrite(int index, Object oldVal, Object newVal)
{
}

public <X> X get(SingleValue<X> singleValue)
Expand Down Expand Up @@ -204,4 +215,25 @@ protected void doSync()
//region Networking

//endregion Networking

protected List<Player> getAffectedPlayers(Player sourcePlayer)
{
var players = sourcePlayer.getWorld().getPlayers();
players.remove(sourcePlayer);
if (NmsRecord.ofPlayer(sourcePlayer).gameMode.getGameModeForPlayer() == GameType.SPECTATOR)
{
players.removeIf(bukkitPlayer ->
NmsRecord.ofPlayer(bukkitPlayer).gameMode.getGameModeForPlayer() != GameType.SPECTATOR);
}

return players;
}

protected void sendPacketToAffectedPlayers(PacketContainer packet)
{
var players = getAffectedPlayers(getBindingPlayer());

var protocol = ProtocolLibrary.getProtocolManager();
players.forEach(p -> protocol.sendServerPacket(p, packet));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package xiamomc.morph.backends.server.renderer.network.datawatcher.watchers.types;

import com.comphenix.protocol.ProtocolLibrary;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import xiamomc.morph.backends.server.renderer.network.PacketFactory;
import xiamomc.morph.backends.server.renderer.network.datawatcher.ValueIndex;
import xiamomc.pluginbase.Annotations.Resolved;

public class GhastWatcher extends LivingEntityWatcher
{
@Override
protected void initRegistry()
{
super.initRegistry();

register(ValueIndex.GHAST);
}

public GhastWatcher(Player bindingPlayer)
{
super(bindingPlayer, EntityType.GHAST);
}

@Resolved(shouldSolveImmediately = true)
private PacketFactory packetFactory;

@Override
protected void onTrackerWrite(int index, Object oldVal, Object newVal)
{
super.onTrackerWrite(index, oldVal, newVal);

if (ValueIndex.GHAST.CHARGING.equals(getSingle(index)))
sendPacketToAffectedPlayers(packetFactory.buildMetaPacket(getBindingPlayer(), this));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,12 @@ protected void doSync()
@Resolved(shouldSolveImmediately = true)
private PacketFactory packetFactory;

private List<Player> getAffectedPlayers(Player sourcePlayer)
{
var players = sourcePlayer.getWorld().getPlayers();
players.remove(sourcePlayer);
if (NmsRecord.ofPlayer(sourcePlayer).gameMode.getGameModeForPlayer() == GameType.SPECTATOR)
{
players.removeIf(bukkitPlayer ->
NmsRecord.ofPlayer(bukkitPlayer).gameMode.getGameModeForPlayer() != GameType.SPECTATOR);
}

return players;
}

@Override
protected void onCustomWrite(RegistryKey<?> key, Object oldVal, Object newVal)
{
super.onCustomWrite(key, oldVal, newVal);

if (key.equals(EntryIndex.DISPLAY_FAKE_EQUIPMENT) || key.equals(EntryIndex.EQUIPMENT))
{
var packet = packetFactory.getEquipmentPacket(getBindingPlayer(), this);
var players = getAffectedPlayers(getBindingPlayer());

var protocol = ProtocolLibrary.getProtocolManager();
players.forEach(p -> protocol.sendServerPacket(p, packet));
}
sendPacketToAffectedPlayers(packetFactory.getEquipmentPacket(getBindingPlayer(), this));
}
}

0 comments on commit 1962cb1

Please sign in to comment.