Archiviert
13
0

Add method to create wrapped block data from the spigot api

Fixes #525
Dieser Commit ist enthalten in:
Dan Mulloy 2018-09-18 17:02:19 -04:00
Ursprung 6fdf0f8255
Commit 7e87c90b8d
2 geänderte Dateien mit 53 neuen und 7 gelöschten Zeilen

Datei anzeigen

@ -26,6 +26,7 @@ import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.utility.MinecraftVersion;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
/**
* Represents a wrapper around IBlockData.
@ -45,6 +46,7 @@ public abstract class WrappedBlockData extends AbstractWrapper implements Clonab
private static MethodAccessor BLOCK_FROM_MATERIAL;
private static MethodAccessor GET_BLOCK_DATA;
private static MethodAccessor FROM_LEGACY_DATA;
private static MethodAccessor GET_HANDLE;
static {
if (MinecraftVersion.atOrAbove(MinecraftVersion.AQUATIC_UPDATE)) {
@ -98,6 +100,15 @@ public abstract class WrappedBlockData extends AbstractWrapper implements Clonab
.returnTypeExact(IBLOCK_DATA)
.build();
GET_BLOCK_DATA = Accessors.getMethodAccessor(fuzzy.getMethod(contract));
fuzzy = FuzzyReflection.fromClass(MinecraftReflection.getCraftBukkitClass("block.data.CraftBlockData"));
contract = FuzzyMethodContract
.newBuilder()
.banModifier(Modifier.STATIC)
.parameterCount(0)
.returnTypeExact(IBLOCK_DATA)
.build();
GET_HANDLE = Accessors.getMethodAccessor(fuzzy.getMethod(contract));
}
}
@ -145,6 +156,10 @@ public abstract class WrappedBlockData extends AbstractWrapper implements Clonab
private static WrappedBlockData createNewData(Material material, int data) {
return new NewBlockData(FROM_LEGACY_DATA.invoke(null, material, (byte) data));
}
private static WrappedBlockData createNewData(BlockData data) {
return new NewBlockData(GET_HANDLE.invoke(data));
}
}
private static class OldBlockData extends WrappedBlockData {
@ -304,6 +319,15 @@ public abstract class WrappedBlockData extends AbstractWrapper implements Clonab
: new OldBlockData(handle);
}
/**
* Creates a new Wrapped Block Data instance from a given Spigot Block Data
* @param data Spigot block data
* @return The new Wrapped Block Data
*/
public static WrappedBlockData createData(BlockData data) {
return NewBlockData.createNewData(data);
}
@Override
public String toString() {
return "WrappedBlockData[handle=" + handle + "]";
@ -320,9 +344,12 @@ public abstract class WrappedBlockData extends AbstractWrapper implements Clonab
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (o instanceof WrappedBlockData) {
WrappedBlockData that = (WrappedBlockData) o;
return this.getType() == that.getType() && getData() == that.getData();
return this.handle.equals(that.handle)
|| (this.getType() == that.getType() && this.getData() == that.getData());
}
return false;

Datei anzeigen

@ -16,15 +16,20 @@
*/
package com.comphenix.protocol.wrappers;
import static org.junit.Assert.assertEquals;
import com.comphenix.protocol.BukkitInitialization;
import net.minecraft.server.v1_13_R2.IBlockData;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.type.GlassPane;
import org.bukkit.craftbukkit.v1_13_R2.block.data.CraftBlockData;
import org.bukkit.craftbukkit.v1_13_R2.block.impl.CraftGlassPane;
import org.bukkit.craftbukkit.v1_13_R2.util.CraftMagicNumbers;
import org.junit.BeforeClass;
import org.junit.Test;
import com.comphenix.protocol.BukkitInitialization;
import com.comphenix.protocol.utility.MinecraftReflection;
import static org.junit.Assert.assertEquals;
/**
* @author dmulloy2
@ -33,12 +38,12 @@ import com.comphenix.protocol.utility.MinecraftReflection;
public class WrappedBlockDataTest {
@BeforeClass
public static void initializeBukkit() throws IllegalAccessException {
public static void initializeBukkit() {
BukkitInitialization.initializeItemMeta();
}
@Test
public void test() {
public void testMaterialCreation() {
Material type = Material.BLUE_WOOL;
WrappedBlockData wrapper = WrappedBlockData.createData(type);
@ -52,4 +57,18 @@ public class WrappedBlockDataTest {
assertEquals(wrapper.getType(), back.getType());
assertEquals(wrapper.getData(), back.getData());
}
@Test
public void testDataCreation() {
IBlockData nmsData = CraftMagicNumbers.getBlock(Material.CYAN_STAINED_GLASS_PANE).getBlockData();
GlassPane data = (GlassPane) CraftBlockData.fromData(nmsData);
data.setFace(BlockFace.EAST, true);
WrappedBlockData wrapper = WrappedBlockData.createData(data);
assertEquals(wrapper.getType(), Material.CYAN_STAINED_GLASS_PANE);
GlassPane back = new CraftGlassPane((IBlockData) wrapper.getHandle());
assertEquals(back.hasFace(BlockFace.EAST), data.hasFace(BlockFace.EAST));
assertEquals(back.hasFace(BlockFace.SOUTH), data.hasFace(BlockFace.SOUTH));
}
}