Change falling block to store the combined block id to fix older versions with blockdata

This commit is contained in:
libraryaddict 2021-05-15 16:29:09 +12:00
parent c3783b9bf0
commit 1e68eeb74d
3 changed files with 28 additions and 27 deletions

View File

@ -4,6 +4,7 @@ import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.StructureModifier;
import lombok.Getter;
import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
@ -21,8 +22,7 @@ import org.bukkit.inventory.ItemStack;
import java.lang.reflect.InvocationTargetException;
public class FallingBlockWatcher extends FlagWatcher {
private ItemStack block = new ItemStack(Material.STONE);
private BlockData blockData;
private int blockCombinedId = 1;
private boolean gridLocked;
public FallingBlockWatcher(Disguise disguise) {
@ -60,8 +60,7 @@ public class FallingBlockWatcher extends FlagWatcher {
relMove.getModifier().write(0, getDisguise().getEntity().getEntityId());
shorts.write(0, conRel(loc.getX(), loc.getBlockX() + 0.5));
shorts.write(1, conRel(loc.getY(),
loc.getBlockY() + (loc.getY() % 1 >= 0.85 ? 1 : loc.getY() % 1 >= 0.35 ? .5 : 0)));
shorts.write(1, conRel(loc.getY(), loc.getBlockY() + (loc.getY() % 1 >= 0.85 ? 1 : loc.getY() % 1 >= 0.35 ? .5 : 0)));
shorts.write(2, conRel(loc.getZ(), loc.getBlockZ() + 0.5));
try {
@ -86,7 +85,7 @@ public class FallingBlockWatcher extends FlagWatcher {
}
public ItemStack getBlock() {
return block;
return ReflectionManager.getItemStackByCombinedId(getBlockCombinedId());
}
public void setBlock(ItemStack block) {
@ -94,12 +93,11 @@ public class FallingBlockWatcher extends FlagWatcher {
block = new ItemStack(Material.STONE);
}
this.block = block;
this.blockCombinedId = ReflectionManager.getCombinedIdByItemStack(block);
if (!getDisguise().isCustomDisguiseName()) {
getDisguise().setDisguiseName(TranslateType.DISGUISE_OPTIONS_PARAMETERS.get("Block") + " " +
TranslateType.DISGUISE_OPTIONS_PARAMETERS
.get(ReflectionManager.toReadable(block.getType().name(), " ")));
TranslateType.DISGUISE_OPTIONS_PARAMETERS.get(ReflectionManager.toReadable(block.getType().name(), " ")));
}
if (DisguiseAPI.isDisguiseInUse(getDisguise()) && getDisguise().getWatcher() == this) {
@ -109,11 +107,7 @@ public class FallingBlockWatcher extends FlagWatcher {
@NmsAddedIn(NmsVersion.v1_13)
public BlockData getBlockData() {
if (block != null && blockData == null) {
return block.getType().createBlockData();
}
return blockData;
return ReflectionManager.getBlockDataByCombinedId(getBlockCombinedId());
}
@NmsAddedIn(NmsVersion.v1_13)
@ -123,17 +117,23 @@ public class FallingBlockWatcher extends FlagWatcher {
return;
}
this.block = new ItemStack(data.getMaterial());
this.blockData = data;
this.blockCombinedId = ReflectionManager.getCombinedIdByBlockData(data);
if (!getDisguise().isCustomDisguiseName()) {
getDisguise().setDisguiseName(TranslateType.DISGUISE_OPTIONS_PARAMETERS.get("Block") + " " +
TranslateType.DISGUISE_OPTIONS_PARAMETERS
.get(ReflectionManager.toReadable(block.getType().name(), " ")));
TranslateType.DISGUISE_OPTIONS_PARAMETERS.get(ReflectionManager.toReadable(data.getMaterial().name(), " ")));
}
if (DisguiseAPI.isDisguiseInUse(getDisguise()) && getDisguise().getWatcher() == this) {
DisguiseUtilities.refreshTrackers(getDisguise());
}
}
public int getBlockCombinedId() {
if (blockCombinedId < 1) {
blockCombinedId = 1;
}
return blockCombinedId;
}
}

View File

@ -292,14 +292,7 @@ public class PacketHandlerSpawn implements IPacketHandler {
double z = loc.getZ();
if (disguise.getType() == DisguiseType.FALLING_BLOCK) {
if (NmsVersion.v1_13.isSupported()) {
BlockData block = ((FallingBlockWatcher) disguise.getWatcher()).getBlockData();
data = ReflectionManager.getCombinedIdByBlockData(block);
} else {
ItemStack block = ((FallingBlockWatcher) disguise.getWatcher()).getBlock();
data = ReflectionManager.getCombinedIdByItemStack(block);
}
data = ((FallingBlockWatcher) disguise.getWatcher()).getBlockCombinedId();
if (((FallingBlockWatcher) disguise.getWatcher()).isGridLocked()) {
double yMod = disguise.getWatcher().getYModifier();

View File

@ -26,6 +26,7 @@ import org.bukkit.entity.*;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.material.MaterialData;
import org.bukkit.plugin.SimplePluginManager;
import org.bukkit.potion.PotionEffect;
import org.bukkit.scheduler.BukkitRunnable;
@ -94,6 +95,7 @@ public class ReflectionManager {
private static Method craftBlockDataGetState;
private static Method getOldItemAsBlock;
private static Method magicGetBlock;
private static Method magicGetMaterial;
private static Method getNmsItem;
private static Method getBlockData;
private static Method getBlockDataAsId;
@ -160,6 +162,7 @@ public class ReflectionManager {
if (NmsVersion.v1_13.isSupported()) {
craftBlockDataGetState = getCraftMethod("block.data.CraftBlockData", "getState");
magicGetBlock = getCraftMethod("util.CraftMagicNumbers", "getBlock", Material.class);
magicGetMaterial = getCraftMethod("util.CraftMagicNumbers", "getMaterial", getNmsClass("Block"));
entityTypesAMethod = getNmsMethod("EntityTypes", "a", String.class);
if (NmsVersion.v1_14.isSupported()) {
@ -1566,12 +1569,17 @@ public class ReflectionManager {
try {
Method idMethod = getNmsMethod("Block", "getByCombinedId", int.class);
Object iBlockData = idMethod.invoke(null, id);
Class iBlockClass = getNmsClass("IBlockData");
Method getBlock = getNmsMethod(iBlockClass, "getBlock");
Method getBlock = getNmsMethod(NmsVersion.v1_16.isSupported() ? iBlockClass.getSuperclass() : iBlockClass, "getBlock");
Object block = getBlock.invoke(iBlockData);
Method getItem = getNmsMethod("Block", "t", iBlockClass);
if (NmsVersion.v1_13.isSupported()) {
return new ItemStack((Material) magicGetMaterial.invoke(null, block));
}
Method getItem = getNmsMethod("Block", "u", iBlockClass);
return getBukkitItem(getItem.invoke(block, iBlockData));
} catch (Exception ex) {