Skip to content

Commit

Permalink
Packet helper cleanup (#2404)
Browse files Browse the repository at this point in the history
* Packet Helper Cleanup

* Push what I have so far to see diff

* Move `resetEquipment` back to NMS

* `respawn` to NMS, change back to `showSignEditor`

* `showSignEditor` can be `void`

* `edit_sign` - show error on older versions as well

* `edit_sign` - Don't convert location twice
  • Loading branch information
tal5 committed Dec 2, 2022
1 parent f2a8c47 commit b83f2a0
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 296 deletions.
Expand Up @@ -3,8 +3,10 @@
import com.denizenscript.denizen.nms.util.jnbt.CompoundTag;
import com.denizenscript.denizen.objects.ColorTag;
import com.denizenscript.denizen.utilities.maps.MapImage;
import org.bukkit.Bukkit;
import org.bukkit.DyeColor;
import org.bukkit.Location;
import org.bukkit.WorldBorder;
import org.bukkit.block.banner.Pattern;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
Expand All @@ -22,9 +24,24 @@ public interface PacketHelper {

void setFakeAbsorption(Player player, float value);

void resetWorldBorder(Player player);
default void resetWorldBorder(Player player) { // TODO: once minimum version is 1.18 or higher, remove from NMS
player.setWorldBorder(null);
}

void setWorldBorder(Player player, Location center, double size, double currSize, long time, int warningDistance, int warningTime);
default void setWorldBorder(Player player, Location center, double size, double currSize, long time, int warningDistance, int warningTime) { // TODO: once minimum version is 1.18 or higher, remove from NMS
WorldBorder border = Bukkit.createWorldBorder();
border.setCenter(center);
if (time > 0) {
border.setSize(currSize);
border.setSize(size, time / 1000);
}
else {
border.setSize(size);
}
border.setWarningDistance(warningDistance);
border.setWarningTime(warningTime);
player.setWorldBorder(border);
}

void setSlot(Player player, int slot, ItemStack itemStack, boolean playerOnly);

Expand All @@ -34,8 +51,8 @@ public interface PacketHelper {

void setVision(Player player, EntityType entityType);

default void showDemoScreen(Player player) {
throw new UnsupportedOperationException();
default void showDemoScreen(Player player) { // TODO: once minimum version is 1.18 or higher, remove from NMS
player.showDemoScreen();
}

void showBlockAction(Player player, Location location, int action, int state);
Expand All @@ -48,16 +65,14 @@ default void showDemoScreen(Player player) {

void showTabListHeaderFooter(Player player, String header, String footer);

void resetTabListHeaderFooter(Player player);

void showTitle(Player player, String title, String subtitle, int fadeIn, int stay, int fadeOut);

void showEquipment(Player player, LivingEntity entity, EquipmentSlot equipmentSlot, ItemStack itemStack);
default void showEquipment(Player player, LivingEntity entity, EquipmentSlot equipmentSlot, ItemStack itemStack) { // TODO: once minimum version is 1.18 or higher, remove from NMS
player.sendEquipmentChange(entity, equipmentSlot, itemStack);
}

void resetEquipment(Player player, LivingEntity entity);

void openBook(Player player, EquipmentSlot hand);

void showHealth(Player player, float health, int food, float saturation);

default void showMobHealth(Player player, LivingEntity mob, double health, double maxHealth) {
Expand All @@ -66,11 +81,7 @@ default void showMobHealth(Player player, LivingEntity mob, double health, doubl

void resetHealth(Player player);

void showExperience(Player player, float experience, int level);

void resetExperience(Player player);

boolean showSignEditor(Player player, Location location);
void showSignEditor(Player player, Location location); // TODO: once minimum version is 1.18 or higher, change to "showFakeSignEditor" and remove location param

void forceSpectate(Player player, Entity entity);

Expand Down
Expand Up @@ -11,11 +11,7 @@
import com.denizenscript.denizen.scripts.commands.player.SidebarCommand;
import com.denizenscript.denizen.scripts.commands.server.BossBarCommand;
import com.denizenscript.denizen.tags.core.PlayerTagBase;
import com.denizenscript.denizen.utilities.PaperAPITools;
import com.denizenscript.denizen.utilities.BukkitImplDeprecations;
import com.denizenscript.denizen.utilities.FormattedTextHelper;
import com.denizenscript.denizen.utilities.ScoreboardHelper;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.*;
import com.denizenscript.denizen.utilities.blocks.FakeBlock;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizen.utilities.entity.BossBarHelper;
Expand Down Expand Up @@ -45,13 +41,14 @@
import org.bukkit.*;
import org.bukkit.advancement.Advancement;
import org.bukkit.advancement.AdvancementProgress;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.block.banner.PatternType;
import org.bukkit.boss.BossBar;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.*;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.*;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.map.MapView;
import org.bukkit.util.RayTraceResult;

Expand Down Expand Up @@ -3292,20 +3289,18 @@ public void adjust(Mechanism mechanism) {
String[] split = mechanism.getValue().asString().split("\\|", 2);
if (split.length > 0 && new ElementTag(split[0]).isFloat()) {
if (split.length > 1 && new ElementTag(split[1]).isInt()) {
NMSHandler.packetHelper.showExperience(getPlayerEntity(),
new ElementTag(split[0]).asFloat(), new ElementTag(split[1]).asInt());
getPlayerEntity().sendExperienceChange(new ElementTag(split[0]).asFloat(), new ElementTag(split[1]).asInt());
}
else {
NMSHandler.packetHelper.showExperience(getPlayerEntity(),
new ElementTag(split[0]).asFloat(), getPlayerEntity().getLevel());
getPlayerEntity().sendExperienceChange(new ElementTag(split[0]).asFloat());
}
}
else {
Debug.echoError("'" + split[0] + "' is not a valid decimal number!");
mechanism.echoError("'" + split[0] + "' is not a valid decimal number!");
}
}
else {
NMSHandler.packetHelper.resetExperience(getPlayerEntity());
getPlayerEntity().sendExperienceChange(getPlayerEntity().getExp(), getPlayerEntity().getLevel());
}
}

Expand Down Expand Up @@ -3516,12 +3511,7 @@ else if (split.length > 1) {
// Shows the player the demo screen.
// -->
if (mechanism.matches("show_demo")) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_18)) {
getPlayerEntity().showDemoScreen();
}
else {
NMSHandler.packetHelper.showDemoScreen(getPlayerEntity());
}
NMSHandler.packetHelper.showDemoScreen(getPlayerEntity());
}

// <--[mechanism]
Expand Down Expand Up @@ -3570,7 +3560,10 @@ else if (getPlayerEntity().getGameMode() == GameMode.SPECTATOR) {
// The book can safely be removed from the player's hand without the player closing the book.
// -->
if (mechanism.matches("open_book")) {
NMSHandler.packetHelper.openBook(getPlayerEntity(), EquipmentSlot.HAND);
ItemStack book = getPlayerEntity().getEquipment().getItemInMainHand();
if (book.getType() == Material.WRITTEN_BOOK) {
getPlayerEntity().openBook(book);
}
}

// <--[mechanism]
Expand All @@ -3582,7 +3575,10 @@ else if (getPlayerEntity().getGameMode() == GameMode.SPECTATOR) {
// The book can safely be removed from the player's offhand without the player closing the book.
// -->
if (mechanism.matches("open_offhand_book")) {
NMSHandler.packetHelper.openBook(getPlayerEntity(), EquipmentSlot.OFF_HAND);
ItemStack book = getPlayerEntity().getEquipment().getItemInOffHand();
if (book.getType() == Material.WRITTEN_BOOK) {
getPlayerEntity().openBook(book);
}
}

// <--[mechanism]
Expand All @@ -3596,15 +3592,11 @@ else if (getPlayerEntity().getGameMode() == GameMode.SPECTATOR) {
if (mechanism.matches("show_book")
&& mechanism.requireObject(ItemTag.class)) {
ItemTag book = mechanism.valueAsType(ItemTag.class);
if (!(book.getItemMeta() instanceof BookMeta)) {
Debug.echoError("show_book mechanism must have a book as input.");
if (book.getBukkitMaterial() != Material.WRITTEN_BOOK) {
mechanism.echoError("show_book mechanism must have a written book as input.");
return;
}
NMSHandler.packetHelper.showEquipment(getPlayerEntity(), getPlayerEntity(),
EquipmentSlot.OFF_HAND, book.getItemStack());
NMSHandler.packetHelper.openBook(getPlayerEntity(), EquipmentSlot.OFF_HAND);
NMSHandler.packetHelper.showEquipment(getPlayerEntity(), getPlayerEntity(),
EquipmentSlot.OFF_HAND, getPlayerEntity().getEquipment().getItemInOffHand());
getPlayerEntity().openBook(book.getItemStack());
}

// <--[mechanism]
Expand Down Expand Up @@ -3688,8 +3680,20 @@ else if (getPlayerEntity().getGameMode() == GameMode.SPECTATOR) {
// Give no input to make a fake edit interface.
// -->
if (mechanism.matches("edit_sign")) {
if (!NMSHandler.packetHelper.showSignEditor(getPlayerEntity(), mechanism.hasValue() ? mechanism.valueAsType(LocationTag.class) : null)) {
Debug.echoError("Can't edit non-sign materials!");
if (mechanism.hasValue() && mechanism.requireObject(LocationTag.class)) {
BlockState state = mechanism.valueAsType(LocationTag.class).getBlockState();
if (!(state instanceof Sign)) {
mechanism.echoError("Invalid location specified: must be a sign.");
return;
}
if (!NMSHandler.getVersion().isAtLeast(NMSVersion.v1_18)) {
NMSHandler.packetHelper.showSignEditor(getPlayerEntity(), state.getLocation());
return;
}
getPlayerEntity().openSign((Sign) state);
}
else {
NMSHandler.packetHelper.showSignEditor(getPlayerEntity(), null);
}
}

Expand All @@ -3706,18 +3710,15 @@ else if (getPlayerEntity().getGameMode() == GameMode.SPECTATOR) {
String[] split = mechanism.getValue().asString().split("\\|", 2);
if (split.length > 0) {
String header = split[0];
String footer = "";
if (split.length > 1) {
footer = split[1];
}
String footer = split.length > 1 ? split[1] : "";
NMSHandler.packetHelper.showTabListHeaderFooter(getPlayerEntity(), header, footer);
}
else {
Debug.echoError("Must specify a header and footer to show!");
mechanism.echoError("Must specify a header and footer to show!");
}
}
else {
NMSHandler.packetHelper.resetTabListHeaderFooter(getPlayerEntity());
getPlayerEntity().setPlayerListHeaderFooter(null, null);
}
}

Expand Down
@@ -1,22 +1,22 @@
package com.denizenscript.denizen.nms.v1_16.helpers;

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.interfaces.PacketHelper;
import com.denizenscript.denizen.nms.util.jnbt.CompoundTag;
import com.denizenscript.denizen.nms.util.jnbt.JNBTListTag;
import com.denizenscript.denizen.nms.v1_16.Handler;
import com.denizenscript.denizen.nms.v1_16.impl.SidebarImpl;
import com.denizenscript.denizen.nms.v1_16.impl.jnbt.CompoundTagImpl;
import com.denizenscript.denizen.nms.v1_16.impl.network.handlers.DenizenNetworkManagerImpl;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.FormattedTextHelper;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.blocks.FakeBlock;
import com.denizenscript.denizen.utilities.maps.MapImage;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizen.nms.interfaces.PacketHelper;
import com.denizenscript.denizen.nms.util.jnbt.CompoundTag;
import com.denizenscript.denizen.nms.util.jnbt.JNBTListTag;
import com.denizenscript.denizen.utilities.FormattedTextHelper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.mojang.datafixers.util.Pair;
import net.md_5.bungee.api.ChatColor;
Expand Down Expand Up @@ -190,11 +190,6 @@ public void showTabListHeaderFooter(Player player, String header, String footer)
sendPacket(player, packet);
}

@Override
public void resetTabListHeaderFooter(Player player) {
showTabListHeaderFooter(player, "", "");
}

@Override
public void showTitle(Player player, String title, String subtitle, int fadeInTicks, int stayTicks, int fadeOutTicks) {
sendPacket(player, new PacketPlayOutTitle(fadeInTicks, stayTicks, fadeOutTicks));
Expand Down Expand Up @@ -227,11 +222,6 @@ public void resetEquipment(Player player, LivingEntity entity) {
sendPacket(player, new PacketPlayOutEntityEquipment(entity.getEntityId(), pairList));
}

@Override
public void openBook(Player player, EquipmentSlot hand) {
sendPacket(player, new PacketPlayOutOpenBook(hand == EquipmentSlot.OFF_HAND ? EnumHand.OFF_HAND : EnumHand.MAIN_HAND));
}

@Override
public void showHealth(Player player, float health, int food, float saturation) {
sendPacket(player, new PacketPlayOutUpdateHealth(health, food, saturation));
Expand All @@ -243,25 +233,15 @@ public void resetHealth(Player player) {
}

@Override
public void showExperience(Player player, float experience, int level) {
sendPacket(player, new PacketPlayOutExperience(experience, 0, level));
}

@Override
public void resetExperience(Player player) {
showExperience(player, player.getExp(), player.getLevel());
}

@Override
public boolean showSignEditor(Player player, Location location) {
public void showSignEditor(Player player, Location location) {
if (location == null) {
LocationTag fakeSign = new LocationTag(player.getLocation());
fakeSign.setY(0);
FakeBlock.showFakeBlockTo(Collections.singletonList(new PlayerTag(player)), fakeSign, new MaterialTag(org.bukkit.Material.OAK_WALL_SIGN), new DurationTag(1), true);
BlockPosition pos = new BlockPosition(fakeSign.getX(), 0, fakeSign.getZ());
((DenizenNetworkManagerImpl) ((CraftPlayer) player).getHandle().playerConnection.networkManager).packetListener.fakeSignExpected = pos;
sendPacket(player, new PacketPlayOutOpenSignEditor(pos));
return true;
return;
}
TileEntity tileEntity = ((CraftWorld) location.getWorld()).getHandle().getTileEntity(new BlockPosition(location.getBlockX(),
location.getBlockY(), location.getBlockZ()));
Expand All @@ -272,10 +252,6 @@ public boolean showSignEditor(Player player, Location location) {
sign.isEditable = true;
sign.a((EntityHuman) ((CraftPlayer) player).getHandle());
sendPacket(player, new PacketPlayOutOpenSignEditor(sign.getPosition()));
return true;
}
else {
return false;
}
}

Expand Down

0 comments on commit b83f2a0

Please sign in to comment.