Fix the MultiBlockChangeInfo wrapper

Also add a few tests to make sure it keeps working
This commit is contained in:
Dan Mulloy 2015-06-15 00:58:16 -04:00
parent 0d18bd2734
commit 24d5b1739a
7 changed files with 153 additions and 17 deletions

View File

@ -1740,7 +1740,7 @@ public class MinecraftReflection {
* @return The MultiBlockChangeInfo class
*/
public static Class<?> getMultiBlockChangeInfoClass() {
return getMinecraftClass("PacketPlayOutMultiBlockChange$MultiBlockChangeInfo");
return getMinecraftClass("MultiBlockChangeInfo", "PacketPlayOutMultiBlockChange$MultiBlockChangeInfo");
}
/**

View File

@ -1,5 +1,18 @@
/**
* (c) 2015 dmulloy2
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
* Copyright (C) 2015 dmulloy2
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*/
package com.comphenix.protocol.wrappers;
@ -9,6 +22,7 @@ import java.lang.reflect.Constructor;
import org.bukkit.Location;
import org.bukkit.World;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.reflect.EquivalentConverter;
import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.utility.MinecraftReflection;
@ -20,6 +34,8 @@ import com.comphenix.protocol.utility.MinecraftReflection;
*/
public class MultiBlockChangeInfo {
private static Constructor<?> constructor;
private short location;
private WrappedBlockData data;
private ChunkCoordIntPair chunk;
@ -164,15 +180,19 @@ public class MultiBlockChangeInfo {
@Override
public Object getGeneric(Class<?> genericType, MultiBlockChangeInfo specific) {
try {
Constructor<?> constructor = MinecraftReflection.getMultiBlockChangeInfoClass().getConstructor(
short.class,
MinecraftReflection.getIBlockDataClass()
);
if (constructor == null) {
constructor = MinecraftReflection.getMultiBlockChangeInfoClass().getConstructor(
PacketType.Play.Server.MULTI_BLOCK_CHANGE.getPacketClass(),
short.class,
MinecraftReflection.getIBlockDataClass()
);
}
return constructor.newInstance(
null,
specific.location,
BukkitConverters.getWrappedBlockDataConverter().getGeneric(MinecraftReflection.getIBlockDataClass(), specific.data)
);
);
} catch (Throwable ex) {
throw new RuntimeException("Failed to construct MultiBlockChangeInfo instance.", ex);
}

View File

@ -117,4 +117,14 @@ public class WrappedBlockData extends AbstractWrapper {
public String toString() {
return "WrappedBlockData[handle=" + handle + "]";
}
@Override
public boolean equals(Object o) {
if (o instanceof WrappedBlockData) {
WrappedBlockData that = (WrappedBlockData) o;
return this.getType() == that.getType();
}
return false;
}
}

View File

@ -7,6 +7,7 @@ import static org.mockito.Mockito.verify;
import net.minecraft.server.v1_8_R3.ChatComponentText;
import net.minecraft.server.v1_8_R3.ChunkCoordIntPair;
import net.minecraft.server.v1_8_R3.DataWatcher.WatchableObject;
import net.minecraft.server.v1_8_R3.IBlockData;
import net.minecraft.server.v1_8_R3.IChatBaseComponent;
import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;
import net.minecraft.server.v1_8_R3.NBTCompressedStreamTools;
@ -95,6 +96,11 @@ public class MinecraftReflectionTest {
assertEquals(ChunkCoordIntPair.class, MinecraftReflection.getChunkCoordIntPair());
}
@Test
public void testIBlockData() {
assertEquals(IBlockData.class, MinecraftReflection.getIBlockDataClass());
}
@Test
public void testPlayerConnection() {
assertEquals(PlayerConnection.class, MinecraftReflection.getPlayerConnectionClass());

View File

@ -0,0 +1,59 @@
/**
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
* Copyright (C) 2015 dmulloy2
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*/
package com.comphenix.protocol.wrappers;
import static org.junit.Assert.assertEquals;
import org.bukkit.Material;
import org.junit.BeforeClass;
import org.junit.Test;
import com.comphenix.protocol.BukkitInitialization;
import com.comphenix.protocol.utility.MinecraftReflection;
/**
* @author dmulloy2
*/
public class MultiBlockChangeTest {
@BeforeClass
public static void initializeBukkit() throws IllegalAccessException {
BukkitInitialization.initializePackage();
}
@Test
public void test() {
int x = 10;
int y = 128;
int z = 7;
short location = (short) (x << 12 | z << 8 | y);
ChunkCoordIntPair chunk = new ChunkCoordIntPair(1, 2);
WrappedBlockData blockData = WrappedBlockData.createData(Material.STONE);
MultiBlockChangeInfo info = new MultiBlockChangeInfo(location, blockData, chunk);
Object generic = MultiBlockChangeInfo.getConverter(chunk).getGeneric(MinecraftReflection.getMultiBlockChangeInfoClass(), info);
MultiBlockChangeInfo back = MultiBlockChangeInfo.getConverter(chunk).getSpecific(generic);
assertEquals(info.getX(), back.getX());
assertEquals(info.getY(), back.getY());
assertEquals(info.getZ(), back.getZ());
assertEquals(info.getData(), back.getData());
}
}

View File

@ -4,7 +4,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import java.util.Collection;
import java.util.List;
import net.minecraft.server.v1_8_R3.AttributeModifier;
@ -86,16 +85,10 @@ public class WrappedAttributeTest {
modifiers.add((AttributeModifier) wrapper.getHandle());
}
class PacketAccessor extends PacketPlayOutUpdateAttributes {
public AttributeSnapshot attributeSnapshot(String string, double d, Collection<AttributeModifier> coll) {
return new AttributeSnapshot(string, d, coll);
}
}
return new PacketAccessor().attributeSnapshot(attribute.getAttributeKey(), attribute.getBaseValue(), modifiers);
PacketPlayOutUpdateAttributes accessor = new PacketPlayOutUpdateAttributes();
return accessor.new AttributeSnapshot(attribute.getAttributeKey(), attribute.getBaseValue(), modifiers);
}
private AttributeModifier getModifierCopy(WrappedAttributeModifier modifier) {
return new AttributeModifier(modifier.getUUID(), modifier.getName(), modifier.getAmount(), modifier.getOperation().getId());
}

View File

@ -0,0 +1,48 @@
/**
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
* Copyright (C) 2015 dmulloy2
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*/
package com.comphenix.protocol.wrappers;
import static org.junit.Assert.assertEquals;
import org.bukkit.Material;
import org.junit.BeforeClass;
import org.junit.Test;
import com.comphenix.protocol.BukkitInitialization;
import com.comphenix.protocol.utility.MinecraftReflection;
/**
* @author dmulloy2
*/
public class WrappedBlockDataTest {
@BeforeClass
public static void initializeBukkit() throws IllegalAccessException {
BukkitInitialization.initializePackage();
}
@Test
public void test() {
Material type = Material.STONE;
WrappedBlockData data = WrappedBlockData.createData(type);
Object generic = BukkitConverters.getWrappedBlockDataConverter().getGeneric(MinecraftReflection.getIBlockDataClass(), data);
WrappedBlockData back = BukkitConverters.getWrappedBlockDataConverter().getSpecific(generic);
assertEquals(data.getType(), back.getType());
}
}