Skip to content

Commit

Permalink
fix custom name properties to actually work
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofaa2 committed Feb 28, 2024
1 parent 1a8da28 commit 256e043
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 79 deletions.
81 changes: 48 additions & 33 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 17 additions & 6 deletions api/src/main/java/me/tofaa/entitylib/meta/EntityMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
import me.tofaa.entitylib.EntityLib;
import me.tofaa.entitylib.extras.InvalidVersionException;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;

Expand Down Expand Up @@ -111,6 +113,18 @@ public boolean hasGlowingEffect() {
return getMaskBit(OFFSET, HAS_GLOWING_EFFECT_BIT);
}

public boolean isGlowing() {
return hasGlowingEffect();
}

public void setHasGlowingEffect(boolean value) {
setMaskBit(OFFSET, HAS_GLOWING_EFFECT_BIT, value);
}

public void setGlowing(boolean value) {
setHasGlowingEffect(value);
}

public boolean isSwimming() {
return getMaskBit(OFFSET, SWIMMING_BIT);
}
Expand All @@ -119,10 +133,6 @@ public void setSwimming(boolean value) {
setMaskBit(OFFSET, SWIMMING_BIT, value);
}

public void setHasGlowingEffect(boolean value) {
setMaskBit(OFFSET, HAS_GLOWING_EFFECT_BIT, value);
}

public boolean isFlyingWithElytra() {
return getMaskBit(OFFSET, FLYING_WITH_ELYTRA_BIT);
}
Expand All @@ -140,11 +150,12 @@ public void setAirTicks(short value) {
}

public Component getCustomName() {
return this.metadata.getIndex(customNameOffset(), null);
Optional<Component> component = this.metadata.getIndex(customNameOffset(), null);
return component.orElse(null);
}

public void setCustomName(Component value) {
this.metadata.setIndex(customNameOffset(), EntityDataTypes.ADV_COMPONENT, value);
this.metadata.setIndex(customNameOffset(), EntityDataTypes.OPTIONAL_ADV_COMPONENT, Optional.ofNullable(value));
}

public boolean isCustomNameVisible() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package me.tofaa.entitylib.wrapper;

import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataType;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes;
import com.github.retrooper.packetevents.protocol.entity.pose.EntityPose;
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
import com.github.retrooper.packetevents.protocol.player.User;
import com.github.retrooper.packetevents.protocol.world.Location;
Expand Down Expand Up @@ -44,7 +48,6 @@ public WrapperEntity(int entityId, UUID uuid, EntityType entityType, EntityMeta
this.passengers = ConcurrentHashMap.newKeySet();
}


public boolean spawn(Location location) {
if (spawned) return false;
this.location = location;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ final class InternalRegistryListener extends PacketListenerAbstract implements L
public void onPacketSend(PacketSendEvent event) {
final User user = event.getUser();
final PacketTypeCommon type = event.getPacketType();

if (type == PacketType.Play.Server.DESTROY_ENTITIES) {
WrapperPlayServerDestroyEntities packet = new WrapperPlayServerDestroyEntities(event);
int[] ids = packet.getEntityIds();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,39 @@ public boolean execute(@NotNull CommandSender commandSender, @NotNull String s,
return false;
}
String arg = strings[0].toLowerCase();
if (arg.equals("spawn")) {
UserProfile profile = new UserProfile(UUID.randomUUID(), "randomname", new ArrayList<>());
p = new WrapperPlayer(profile, EntityLib.getPlatform().getEntityIdProvider().provide(profile.getUUID(), EntityTypes.PLAYER));
p.spawn(SpigotConversionUtil.fromBukkitLocation(player.getLocation()));
p.addViewer(player.getUniqueId());
ItemStack stack = ItemStack.builder().type(ItemTypes.DIAMOND_BOOTS).build();
p.getEquipment().setBoots(stack);
}
else if (arg.equals( "texture")) {
p.setTextureProperties(ExtraConversionUtil.getProfileFromBukkitPlayer(player).getTextureProperties());
player.sendMessage("texture");
}
else if (arg.equals( "ping")) {
p.setLatency(1000);
player.sendMessage("Pong");
}
else if (arg.equals( "gamemode")) {
p.setGameMode(GameMode.CREATIVE);
player.sendMessage("Gamemode set to creative");
}
else if (arg.equals( "displayname")) {
p.setDisplayName(Component.text("Hello"));
player.sendMessage("Display name set to Hello");
}
else if (arg.equals( "tablist")) {
p.setInTablist(!p.isInTablist());
player.sendMessage("Tablist " + (p.isInTablist() ? "enabled" : "disabled"));
}
else if (arg.equals("remove")) {
p.remove();
player.sendMessage("Entity removed");
switch (arg) {
case "spawn":
UserProfile profile = new UserProfile(UUID.randomUUID(), "randomname", new ArrayList<>());
p = new WrapperPlayer(profile, EntityLib.getPlatform().getEntityIdProvider().provide(profile.getUUID(), EntityTypes.PLAYER));
p.spawn(SpigotConversionUtil.fromBukkitLocation(player.getLocation()));
p.addViewer(player.getUniqueId());
ItemStack stack = ItemStack.builder().type(ItemTypes.DIAMOND_BOOTS).build();
p.getEquipment().setBoots(stack);
break;
case "texture":
p.setTextureProperties(ExtraConversionUtil.getProfileFromBukkitPlayer(player).getTextureProperties());
player.sendMessage("texture");
break;
case "ping":
p.setLatency(1000);
player.sendMessage("Pong");
break;
case "gamemode":
p.setGameMode(GameMode.CREATIVE);
player.sendMessage("Gamemode set to creative");
break;
case "displayname":
p.setDisplayName(Component.text("Hello"));
player.sendMessage("Display name set to Hello");
break;
case "tablist":
p.setInTablist(!p.isInTablist());
player.sendMessage("Tablist " + (p.isInTablist() ? "enabled" : "disabled"));
break;
case "remove":
p.remove();
player.sendMessage("Entity removed");
break;
}
return true;
}
Expand All @@ -77,7 +79,7 @@ else if (arg.equals("remove")) {
@Override
public List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException {
if (args.length == 1) {
return Arrays.asList(new String[]{"spawn", "texture", "ping", "gamemode", "displayname", "tablist", "remove"});
return Arrays.asList("spawn", "texture", "ping", "gamemode", "displayname", "tablist", "remove", "sneak");
}
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.github.retrooper.packetevents.util.SpigotConversionUtil;
import me.tofaa.entitylib.EntityLib;
import me.tofaa.entitylib.meta.display.TextDisplayMeta;
import me.tofaa.entitylib.meta.mobs.passive.PigMeta;
import me.tofaa.entitylib.wrapper.WrapperEntity;
import net.kyori.adventure.text.Component;
import org.bukkit.command.CommandSender;
Expand All @@ -24,18 +25,15 @@ public boolean execute(@NotNull CommandSender commandSender, @NotNull String s,
if (!(commandSender instanceof Player)) return true;
Player player = (Player) commandSender;
if (e == null) {
e = EntityLib.getApi().createEntity(EntityTypes.TEXT_DISPLAY);
if (e == null) {
player.sendMessage("Failed to spawn entity");
return true;
}
e = EntityLib.getApi().createEntity(EntityTypes.PIG);
e.spawn(SpigotConversionUtil.fromBukkitLocation(player.getLocation()));
e.addViewer(player.getUniqueId());
player.sendMessage("Spawned");
}
String msg = String.join(" ", strings);
TextDisplayMeta meta = (TextDisplayMeta) e.getEntityMeta();
meta.setText(Component.text(msg));
PigMeta meta = (PigMeta) e.getEntityMeta();
meta.setCustomNameVisible(true);
meta.setCustomName(Component.text(msg));
player.sendMessage("Set text to: " + msg);
return true;
}
Expand Down

0 comments on commit 256e043

Please sign in to comment.