Skip to content

Commit

Permalink
Several minor cleanups/fixes (#2580)
Browse files Browse the repository at this point in the history
* Several minor cleanups

* Remove `ClientboundAddPlayerPacket` references

* Fix `PlayerTag.skin_model` on 1.17
  • Loading branch information
tal5 committed Dec 19, 2023
1 parent 08e6dff commit fba327b
Show file tree
Hide file tree
Showing 17 changed files with 43 additions and 111 deletions.
Expand Up @@ -48,10 +48,12 @@ public class WardenChangesAngerLevelScriptEvent extends BukkitScriptEvent implem

public WardenChangesAngerLevelScriptEvent() {
registerCouldMatcher("warden changes anger level");
this.<WardenChangesAngerLevelScriptEvent, ElementTag>registerDetermination("anger", ElementTag.class, (evt, context, anger) -> {
this.<WardenChangesAngerLevelScriptEvent, ElementTag>registerOptionalDetermination("anger", ElementTag.class, (evt, context, anger) -> {
if (anger.isInt()) {
evt.event.setNewAnger(anger.asInt());
return true;
}
return false;
});
}

Expand Down
Expand Up @@ -90,7 +90,9 @@ default void setSpawnerSpawnedType(CreatureSpawner spawner, EntityTag entity) {
spawner.setSpawnedType(entity.getBukkitEntityType());
}

Color getMapColor(Block block);
default Color getMapColor(Block block) { // TODO: once 1.20 is the minimum supported version, remove from NMS
return block.getBlockData().getMapColor();
}

default void setVanillaTags(Material material, Set<String> tags) {
throw new UnsupportedOperationException();
Expand Down
Expand Up @@ -46,8 +46,6 @@ public void setSneaking(Entity entity, boolean sneak) {

public abstract void forceInteraction(Player player, Location location);

public abstract Entity getEntity(World world, UUID uuid);

public abstract CompoundTag getNbtData(Entity entity);

public abstract void setNbtData(Entity entity, CompoundTag compoundTag);
Expand Down Expand Up @@ -382,7 +380,9 @@ public void clientResetLoc(Entity entity) {

public abstract void setBoundingBox(Entity entity, BoundingBox box);

public abstract List<Player> getPlayersThatSee(Entity entity);
public List<Player> getPlayersThatSee(Entity entity) { // TODO: once the minimum supported version is 1.20, remove from NMS
return List.copyOf(entity.getTrackedBy());
}

public void sendAllUpdatePackets(Entity entity) {
throw new UnsupportedOperationException();
Expand Down
Expand Up @@ -335,13 +335,7 @@ public static Entity getEntityForID(UUID id) {
if (rememberedEntities.containsKey(id)) {
return rememberedEntities.get(id);
}
for (World world : Bukkit.getWorlds()) {
Entity entity = NMSHandler.entityHelper.getEntity(world, id);
if (entity != null) {
return entity;
}
}
return null;
return Bukkit.getEntity(id);
}

public static boolean matches(String arg) {
Expand Down
Expand Up @@ -2339,16 +2339,6 @@ else if (foodLevel / maxHunger < 1) {
return null;
});

// <--[tag]
// @attribute <PlayerTag.skin_model>
// @returns ElementTag
// @description
// Returns the player's skin model, either CLASSIC or SLIM.
// -->
registerOnlineOnlyTag(ElementTag.class, "skin_model", (attribute, object) -> {
return new ElementTag(object.getPlayerEntity().getPlayerProfile().getTextures().getSkinModel());
});

// <--[tag]
// @attribute <PlayerTag.fake_entities>
// @returns ListTag(EntityTag)
Expand Down Expand Up @@ -2567,6 +2557,19 @@ else if (foodLevel / maxHunger < 1) {
return result;
});

if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_18)) {

// <--[tag]
// @attribute <PlayerTag.skin_model>
// @returns ElementTag
// @description
// Returns the player's skin model, either CLASSIC or SLIM.
// -->
registerOnlineOnlyTag(ElementTag.class, "skin_model", (attribute, object) -> {
return MultiVersionHelper1_18.getSkinModel(object.getPlayerEntity());
});
}

if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_19)) {

// <--[tag]
Expand Down
Expand Up @@ -4,8 +4,8 @@
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.utilities.BukkitImplDeprecations;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import org.bukkit.TreeSpecies;
import org.bukkit.entity.Boat;
Expand Down Expand Up @@ -38,10 +38,9 @@ public String getPropertyId() {

@Override
public void setPropertyValue(ElementTag type, Mechanism mechanism) {
if (!mechanism.requireEnum(TreeSpecies.class)) {
return;
if (mechanism.requireEnum(TreeSpecies.class)) {
as(Boat.class).setWoodType(type.asEnum(TreeSpecies.class));
}
as(Boat.class).setWoodType(type.asEnum(TreeSpecies.class));
}

public static void register() {
Expand Down
@@ -0,0 +1,11 @@
package com.denizenscript.denizen.utilities;

import com.denizenscript.denizencore.objects.core.ElementTag;
import org.bukkit.entity.Player;

public class MultiVersionHelper1_18 {

public static ElementTag getSkinModel(Player player) {
return new ElementTag(player.getPlayerProfile().getTextures().getSkinModel());
}
}
Expand Up @@ -142,12 +142,6 @@ public void forceInteraction(Player player, Location location) {
new BlockHitResult(new Vec3(0, 0, 0), null, pos, false));
}

@Override
public Entity getEntity(World world, UUID uuid) {
net.minecraft.world.entity.Entity entity = ((CraftWorld) world).getHandle().getEntity(uuid);
return entity == null ? null : entity.getBukkitEntity();
}

@Override
public CompoundTag getNbtData(Entity entity) {
net.minecraft.nbt.CompoundTag compound = new net.minecraft.nbt.CompoundTag();
Expand Down
Expand Up @@ -159,12 +159,6 @@ public void forceInteraction(Player player, Location location) {
new BlockHitResult(new Vec3(0, 0, 0), null, pos, false));
}

@Override
public Entity getEntity(World world, UUID uuid) {
net.minecraft.world.entity.Entity entity = ((CraftWorld) world).getHandle().getEntity(uuid);
return entity == null ? null : entity.getBukkitEntity();
}

@Override
public CompoundTag getNbtData(Entity entity) {
net.minecraft.nbt.CompoundTag compound = new net.minecraft.nbt.CompoundTag();
Expand Down
Expand Up @@ -168,12 +168,6 @@ public void forceInteraction(Player player, Location location) {
new BlockHitResult(new Vec3(0, 0, 0), null, pos, false));
}

@Override
public Entity getEntity(World world, UUID uuid) {
net.minecraft.world.entity.Entity entity = ((CraftWorld) world).getHandle().getEntity(uuid);
return entity == null ? null : entity.getBukkitEntity();
}

@Override
public CompoundTag getNbtData(Entity entity) {
net.minecraft.nbt.CompoundTag compound = new net.minecraft.nbt.CompoundTag();
Expand Down
Expand Up @@ -277,12 +277,6 @@ public void setSpawnerCustomRules(CreatureSpawner spawner, int skyMin, int skyMa
}
}

@Override
public Color getMapColor(Block block) {
CraftBlock craftBlock = (CraftBlock) block;
return Color.fromRGB(craftBlock.getNMS().getMapColor(craftBlock.getHandle(), craftBlock.getPosition()).col);
}

public static final MethodHandle HOLDERSET_NAMED_BIND = ReflectionHelper.getMethodHandle(HolderSet.Named.class, ReflectionMappingsInfo.HolderSetNamed_bind_method, List.class);
public static final MethodHandle HOLDER_REFERENCE_BINDTAGS = ReflectionHelper.getMethodHandle(Holder.Reference.class, ReflectionMappingsInfo.HolderReference_bindTags_method, Collection.class);

Expand Down
Expand Up @@ -124,7 +124,7 @@ public double getDamageTo(LivingEntity attacker, Entity target) {
if (attrib != null) {
damage = attrib.getValue();
}
if (attacker.getEquipment() != null && attacker.getEquipment().getItemInMainHand() != null) {
if (attacker.getEquipment() != null) {
damage += EnchantmentHelper.getDamageBonus(CraftItemStack.asNMSCopy(attacker.getEquipment().getItemInMainHand()), monsterType);
}
if (damage <= 0) {
Expand Down Expand Up @@ -177,12 +177,6 @@ public void forceInteraction(Player player, Location location) {
new BlockHitResult(new Vec3(0, 0, 0), null, CraftLocation.toBlockPosition(location), false));
}

@Override
public Entity getEntity(World world, UUID uuid) {
net.minecraft.world.entity.Entity entity = ((CraftWorld) world).getHandle().getEntity(uuid);
return entity == null ? null : entity.getBukkitEntity();
}

@Override
public CompoundTag getNbtData(Entity entity) {
net.minecraft.nbt.CompoundTag compound = new net.minecraft.nbt.CompoundTag();
Expand Down Expand Up @@ -354,20 +348,6 @@ public void run() {
}
}

@Override
public List<Player> getPlayersThatSee(Entity entity) {
ChunkMap tracker = ((ServerLevel) ((CraftEntity) entity).getHandle().level()).getChunkSource().chunkMap;
ChunkMap.TrackedEntity entityTracker = tracker.entityMap.get(entity.getEntityId());
ArrayList<Player> output = new ArrayList<>();
if (entityTracker == null) {
return output;
}
for (ServerPlayerConnection player : entityTracker.seenBy) {
output.add(player.getPlayer().getBukkitEntity());
}
return output;
}

@Override
public void sendAllUpdatePackets(Entity entity) {
ChunkMap tracker = ((ServerLevel) ((CraftEntity) entity).getHandle().level()).getChunkSource().chunkMap;
Expand Down
Expand Up @@ -446,18 +446,7 @@ public void sendClimbableMaterials(Player player, List<Material> materials) {
public void refreshPlayer(Player player) {
ServerPlayer nmsPlayer = ((CraftPlayer) player).getHandle();
ServerLevel nmsWorld = (ServerLevel) nmsPlayer.level();
CommonPlayerSpawnInfo spawnInfo = new CommonPlayerSpawnInfo(
nmsWorld.dimensionTypeId(),
nmsWorld.dimension(),
BiomeManager.obfuscateSeed(nmsWorld.getSeed()),
nmsPlayer.gameMode.getGameModeForPlayer(),
nmsPlayer.gameMode.getPreviousGameModeForPlayer(),
nmsWorld.isDebug(),
nmsWorld.isFlat(),
nmsPlayer.getLastDeathLocation(),
nmsPlayer.getPortalCooldown()
);
nmsPlayer.connection.send(new ClientboundRespawnPacket(spawnInfo, ClientboundRespawnPacket.KEEP_ALL_DATA));
nmsPlayer.connection.send(new ClientboundRespawnPacket(nmsPlayer.createCommonSpawnInfo(nmsWorld), ClientboundRespawnPacket.KEEP_ALL_DATA));
nmsPlayer.connection.teleport(player.getLocation());
if (nmsPlayer.isPassenger()) {
nmsPlayer.connection.send(new ClientboundSetPassengersPacket(nmsPlayer.getVehicle()));
Expand Down
Expand Up @@ -20,6 +20,7 @@
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_20_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_20_R3.entity.CraftEntityType;
import org.bukkit.craftbukkit.v1_20_R3.util.CraftLocation;
import org.bukkit.entity.EntityType;

Expand Down Expand Up @@ -167,17 +168,7 @@ private List<EntityType> getSpawnableEntities(MobCategory creatureType) {
return entityTypes;
}
for (MobSpawnSettings.SpawnerData meta : typeSettingList.unwrap()) {
try {
String n = net.minecraft.world.entity.EntityType.getKey(meta.type).getPath();
EntityType et = EntityType.fromName(n);
if (et == null) {
et = EntityType.valueOf(n.toUpperCase(Locale.ENGLISH));
}
entityTypes.add(et);
}
catch (Throwable e) {
// Ignore the error. Likely from invalid entity type name output.
}
entityTypes.add(CraftEntityType.minecraftToBukkit(meta.type));
}
return entityTypes;
}
Expand Down
Expand Up @@ -395,8 +395,8 @@ public Packet<ClientGamePacketListener> processPacketHandlersFor(Packet<ClientGa
AttachPacketHandlers.registerHandlers();
BlockLightPacketHandlers.registerHandlers();
DenizenPacketHandlerPacketHandlers.registerHandlers();
DisguisePacketHandlers.registerHandlers();
EntityMetadataPacketHandlers.registerHandlers();
DisguisePacketHandlers.registerHandlers();
FakeBlocksPacketHandlers.registerHandlers();
FakeEquipmentPacketHandlers.registerHandlers();
FakePlayerPacketHandlers.registerHandlers();
Expand Down
Expand Up @@ -31,19 +31,13 @@ public class DisguisePacketHandlers {
public static void registerHandlers() {
registerPacketHandler(ClientboundSetEntityDataPacket.class, ClientboundSetEntityDataPacket::id, DisguisePacketHandlers::processEntityDataPacket);
registerPacketHandler(ClientboundUpdateAttributesPacket.class, ClientboundUpdateAttributesPacket::getEntityId, DisguisePacketHandlers::processAttributesPacket);
//registerPacketHandler(ClientboundAddPlayerPacket.class, ClientboundAddPlayerPacket::getEntityId, DisguisePacketHandlers::sendDisguiseForPacket);
registerPacketHandler(ClientboundAddEntityPacket.class, ClientboundAddEntityPacket::getId, DisguisePacketHandlers::sendDisguiseForPacket);
registerPacketHandler(ClientboundTeleportEntityPacket.class, ClientboundTeleportEntityPacket::getId, DisguisePacketHandlers::processTeleportPacket);
registerPacketHandler(ClientboundMoveEntityPacket.Rot.class, ClientboundMoveEntityPacket::getEntity, DisguisePacketHandlers::processMoveEntityRotPacket);
registerPacketHandler(ClientboundMoveEntityPacket.PosRot.class, ClientboundMoveEntityPacket::getEntity, DisguisePacketHandlers::processMoveEntityPosRotPacket);
}

public static final Field TELEPORT_PACKET_ENTITY_ID = ReflectionHelper.getFields(ClientboundTeleportEntityPacket.class).get(ReflectionMappingsInfo.ClientboundTeleportEntityPacket_id, int.class);
public static final Field TELEPORT_PACKET_X = ReflectionHelper.getFields(ClientboundTeleportEntityPacket.class).get(ReflectionMappingsInfo.ClientboundTeleportEntityPacket_x, double.class);
public static final Field TELEPORT_PACKET_Y = ReflectionHelper.getFields(ClientboundTeleportEntityPacket.class).get(ReflectionMappingsInfo.ClientboundTeleportEntityPacket_y, double.class);
public static final Field TELEPORT_PACKET_Z = ReflectionHelper.getFields(ClientboundTeleportEntityPacket.class).get(ReflectionMappingsInfo.ClientboundTeleportEntityPacket_z, double.class);
public static final Field TELEPORT_PACKET_YAW = ReflectionHelper.getFields(ClientboundTeleportEntityPacket.class).get(ReflectionMappingsInfo.ClientboundTeleportEntityPacket_yRot, byte.class);
public static final Field TELEPORT_PACKET_PITCH = ReflectionHelper.getFields(ClientboundTeleportEntityPacket.class).get(ReflectionMappingsInfo.ClientboundTeleportEntityPacket_xRot, byte.class);

private static boolean antiDuplicate = false;

Expand Down Expand Up @@ -120,13 +114,8 @@ public static ClientboundUpdateAttributesPacket processAttributesPacket(DenizenN

public static ClientboundTeleportEntityPacket processTeleportPacket(DenizenNetworkManagerImpl networkManager, ClientboundTeleportEntityPacket teleportEntityPacket, DisguiseCommand.TrackedDisguise disguise) throws IllegalAccessException {
if (disguise.as.getBukkitEntityType() == EntityType.ENDER_DRAGON) {
ClientboundTeleportEntityPacket pNew = new ClientboundTeleportEntityPacket(((CraftEntity) disguise.entity.getBukkitEntity()).getHandle());
TELEPORT_PACKET_ENTITY_ID.setInt(pNew, teleportEntityPacket.getId());
TELEPORT_PACKET_X.setDouble(pNew, teleportEntityPacket.getX());
TELEPORT_PACKET_Y.setDouble(pNew, teleportEntityPacket.getY());
TELEPORT_PACKET_Z.setDouble(pNew, teleportEntityPacket.getZ());
ClientboundTeleportEntityPacket pNew = new ClientboundTeleportEntityPacket(DenizenNetworkManagerImpl.copyPacket(teleportEntityPacket));
TELEPORT_PACKET_YAW.setByte(pNew, EntityAttachmentHelper.adaptedCompressedAngle(teleportEntityPacket.getyRot(), 180));
TELEPORT_PACKET_PITCH.setByte(pNew, teleportEntityPacket.getxRot());
return pNew;
}
return sendDisguiseForPacket(networkManager, teleportEntityPacket, disguise);
Expand Down
Expand Up @@ -11,7 +11,6 @@
public class HiddenEntitiesPacketHandlers {

public static void registerHandlers() {
//DenizenNetworkManagerImpl.registerPacketHandler(ClientboundAddPlayerPacket.class, HiddenEntitiesPacketHandlers::processHiddenEntitiesForPacket);
DenizenNetworkManagerImpl.registerPacketHandler(ClientboundAddEntityPacket.class, HiddenEntitiesPacketHandlers::processHiddenEntitiesForPacket);
DenizenNetworkManagerImpl.registerPacketHandler(ClientboundAddExperienceOrbPacket.class, HiddenEntitiesPacketHandlers::processHiddenEntitiesForPacket);
DenizenNetworkManagerImpl.registerPacketHandler(ClientboundMoveEntityPacket.Rot.class, HiddenEntitiesPacketHandlers::processHiddenEntitiesForPacket);
Expand All @@ -33,9 +32,6 @@ public static Packet<ClientGamePacketListener> processHiddenEntitiesForPacket(De
try {
int ider = -1;
Entity e = null;
/*if (packet instanceof ClientboundAddPlayerPacket) {
ider = ((ClientboundAddPlayerPacket) packet).getEntityId();
}*/
if (packet instanceof ClientboundAddEntityPacket) {
ider = ((ClientboundAddEntityPacket) packet).getId();
}
Expand Down

0 comments on commit fba327b

Please sign in to comment.