fix ItemDisplayMeta

This commit is contained in:
Tofaa 2024-01-19 08:14:14 +03:00
parent 4988e00ba8
commit b4cdf0e9cd
4 changed files with 77 additions and 8 deletions

View file

@ -5,7 +5,9 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="9d5d9b6f-43c8-41a4-bb42-a66ffc96c9b0" name="Changes" comment=""> <list default="true" id="9d5d9b6f-43c8-41a4-bb42-a66ffc96c9b0" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/src/main/java/me/tofaa/entitylib/entity/WrapperEntityCreature.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/me/tofaa/entitylib/entity/WrapperEntityCreature.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/me/tofaa/entitylib/meta/display/ItemDisplayMeta.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/me/tofaa/entitylib/meta/display/ItemDisplayMeta.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test-plugin/src/main/java/me/tofaa/entitylib/TestDisplayCommand.java" beforeDir="false" afterPath="$PROJECT_DIR$/test-plugin/src/main/java/me/tofaa/entitylib/TestDisplayCommand.java" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -210,7 +212,7 @@
<workItem from="1705192736239" duration="496000" /> <workItem from="1705192736239" duration="496000" />
<workItem from="1705534524814" duration="15424000" /> <workItem from="1705534524814" duration="15424000" />
<workItem from="1705578156456" duration="78000" /> <workItem from="1705578156456" duration="78000" />
<workItem from="1705636302508" duration="2201000" /> <workItem from="1705636302508" duration="4010000" />
</task> </task>
<servers /> <servers />
</component> </component>

View file

@ -1,6 +1,7 @@
package me.tofaa.entitylib.meta.display; package me.tofaa.entitylib.meta.display;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes;
import com.github.retrooper.packetevents.protocol.item.ItemStack;
import me.tofaa.entitylib.meta.Metadata; import me.tofaa.entitylib.meta.Metadata;
import me.tofaa.entitylib.meta.types.DisplayMeta; import me.tofaa.entitylib.meta.types.DisplayMeta;
@ -13,12 +14,20 @@ public class ItemDisplayMeta extends DisplayMeta {
super(entityId, metadata); super(entityId, metadata);
} }
public ItemStack getItem() {
return super.metadata.getIndex(OFFSET, ItemStack.EMPTY);
}
public void setItem(ItemStack itemStack) {
super.metadata.setIndex(OFFSET, EntityDataTypes.ITEMSTACK, itemStack);
}
public DisplayType getDisplayType() { public DisplayType getDisplayType() {
return DisplayType.VALUES[super.metadata.getIndex(OFFSET, 0)]; return DisplayType.VALUES[super.metadata.getIndex(offset(OFFSET, 1), 0)];
} }
public void setDisplayType(DisplayType displayType) { public void setDisplayType(DisplayType displayType) {
super.metadata.setIndex(OFFSET, EntityDataTypes.BYTE, (byte) displayType.ordinal()); super.metadata.setIndex(offset(OFFSET, 1), EntityDataTypes.BYTE, (byte) displayType.ordinal());
} }
public enum DisplayType { public enum DisplayType {

View file

@ -19,9 +19,13 @@ public final class EntityLibPlugin extends JavaPlugin {
EntityLib.init(PacketEvents.getAPI()); EntityLib.init(PacketEvents.getAPI());
EntityLib.enableEntityInteractions(); EntityLib.enableEntityInteractions();
EntityLib.setInteractionProcessor((entity, action, hand, user) -> user.sendMessage("Hello World")); EntityLib.setInteractionProcessor((entity, action, hand, user) -> user.sendMessage("Hello World"));
TestDisplayCommand testDisplayCommand = new TestDisplayCommand();
getCommand("testdisplay").setExecutor(testDisplayCommand);
getCommand("testdisplay").setTabCompleter(testDisplayCommand);
getCommand("testapi").setExecutor(new TestCommand()); getCommand("testapi").setExecutor(new TestCommand());
getCommand("testentity").setExecutor(new TestEntityCommand()); getCommand("testentity").setExecutor(new TestEntityCommand());
getCommand("testdisplay").setExecutor(new TestDisplayCommand());
getCommand("spawnclickablefrog").setExecutor(new SpawnClickableFrogCommand()); getCommand("spawnclickablefrog").setExecutor(new SpawnClickableFrogCommand());
instance = this; instance = this;
} }

View file

@ -1,34 +1,88 @@
package me.tofaa.entitylib; package me.tofaa.entitylib;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.protocol.item.ItemStack;
import com.github.retrooper.packetevents.protocol.item.type.ItemTypes;
import com.github.retrooper.packetevents.util.Vector3f; import com.github.retrooper.packetevents.util.Vector3f;
import io.github.retrooper.packetevents.util.SpigotConversionUtil; import io.github.retrooper.packetevents.util.SpigotConversionUtil;
import me.tofaa.entitylib.entity.WrapperEntity; import me.tofaa.entitylib.entity.WrapperEntity;
import me.tofaa.entitylib.meta.display.BlockDisplayMeta;
import me.tofaa.entitylib.meta.display.ItemDisplayMeta;
import me.tofaa.entitylib.meta.display.TextDisplayMeta; import me.tofaa.entitylib.meta.display.TextDisplayMeta;
import me.tofaa.entitylib.meta.types.DisplayMeta; import me.tofaa.entitylib.meta.types.DisplayMeta;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.bukkit.Material;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.List;
import java.util.UUID; import java.util.UUID;
public class TestDisplayCommand implements CommandExecutor { public class TestDisplayCommand implements CommandExecutor, TabCompleter {
@Override @Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
if (strings.length == 0) return false;
String type = strings[0];
Player player = (Player) commandSender; Player player = (Player) commandSender;
switch (type) {
case "block":
block(player);
break;
case "text":
text(player);
break;
case "item":
item(player);
break;
}
return false;
}
private void block(Player player) {
WrapperEntity e = EntityLib.createEntity(UUID.randomUUID(), EntityTypes.BLOCK_DISPLAY);
BlockDisplayMeta meta = (BlockDisplayMeta) e.getMeta();
meta.setHasGlowingEffect(true);
meta.setBlockId(Material.AMETHYST_BLOCK.getId());
meta.setBillboardConstraints(DisplayMeta.BillboardConstraints.CENTER);
meta.setScale(new Vector3f(2, 2, 2));
e.addViewer(player.getUniqueId());
e.spawn(SpigotConversionUtil.fromBukkitLocation(player.getLocation()));
}
private void text(Player player) {
WrapperEntity e = EntityLib.createEntity(UUID.randomUUID(), EntityTypes.TEXT_DISPLAY); WrapperEntity e = EntityLib.createEntity(UUID.randomUUID(), EntityTypes.TEXT_DISPLAY);
TextDisplayMeta meta = (TextDisplayMeta) e.getMeta(); TextDisplayMeta meta = (TextDisplayMeta) e.getMeta();
meta.setHasGlowingEffect(true); meta.setHasGlowingEffect(true);
meta.setText(Component.text("Hello World!")); meta.setText(Component.text("Hello World!"));
meta.setBillboardConstraints(DisplayMeta.BillboardConstraints.CENTER); meta.setBillboardConstraints(DisplayMeta.BillboardConstraints.CENTER);
meta.setScale(new Vector3f(2, 2, 2)); meta.setScale(new Vector3f(2, 2, 2));
meta.setTranslation(new Vector3f(10, 0, 0));
e.addViewer(player.getUniqueId()); e.addViewer(player.getUniqueId());
e.spawn(SpigotConversionUtil.fromBukkitLocation(player.getLocation())); e.spawn(SpigotConversionUtil.fromBukkitLocation(player.getLocation()));
return false; }
private void item(Player player) {
WrapperEntity e = EntityLib.createEntity(UUID.randomUUID(), EntityTypes.ITEM_DISPLAY);
ItemDisplayMeta meta = (ItemDisplayMeta) e.getMeta();
meta.setDisplayType(ItemDisplayMeta.DisplayType.FIRST_PERSON_LEFT_HAND);
meta.setItem(ItemStack.builder()
.type(ItemTypes.ACACIA_BOAT).build()
);
e.addViewer(player.getUniqueId());
e.spawn(SpigotConversionUtil.fromBukkitLocation(player.getLocation()));
}
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
return strings.length == 1 ? Arrays.asList("block", "text", "item") : null;
} }
} }