Mirror von
https://github.com/PaperMC/Paper.git
synchronisiert 2024-12-16 19:40:07 +01:00
Add merging and matching of parsed BlockData
Dieser Commit ist enthalten in:
Ursprung
a9c796f12c
Commit
cfc67965f3
@ -27,6 +27,7 @@ import org.bukkit.craftbukkit.util.CraftMagicNumbers;
|
|||||||
public class CraftBlockData implements BlockData {
|
public class CraftBlockData implements BlockData {
|
||||||
|
|
||||||
private IBlockData state;
|
private IBlockData state;
|
||||||
|
private Set<IBlockState<?>> parsedStates;
|
||||||
|
|
||||||
protected CraftBlockData() {
|
protected CraftBlockData() {
|
||||||
throw new AssertionError("Template Constructor");
|
throw new AssertionError("Template Constructor");
|
||||||
@ -86,9 +87,51 @@ public class CraftBlockData implements BlockData {
|
|||||||
* @param <N> the NMS type
|
* @param <N> the NMS type
|
||||||
*/
|
*/
|
||||||
protected <B extends Enum<B>, N extends Enum<N> & INamable> void set(BlockStateEnum<N> nms, Enum<B> bukkit) {
|
protected <B extends Enum<B>, N extends Enum<N> & INamable> void set(BlockStateEnum<N> nms, Enum<B> bukkit) {
|
||||||
|
this.parsedStates = null;
|
||||||
this.state = this.state.set(nms, toNMS(bukkit, nms.b()));
|
this.state = this.state.set(nms, toNMS(bukkit, nms.b()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BlockData merge(BlockData data) {
|
||||||
|
CraftBlockData craft = (CraftBlockData) data;
|
||||||
|
Preconditions.checkArgument(craft.parsedStates != null, "Data not created via string parsing");
|
||||||
|
Preconditions.checkArgument(this.state.getBlock() == craft.state.getBlock(), "States have different types (got %s, expected %s)", data, this);
|
||||||
|
|
||||||
|
CraftBlockData clone = (CraftBlockData) this.clone();
|
||||||
|
clone.parsedStates = null;
|
||||||
|
|
||||||
|
for (IBlockState parsed : craft.parsedStates) {
|
||||||
|
clone.state = clone.state.set(parsed, craft.state.get(parsed));
|
||||||
|
}
|
||||||
|
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matches(BlockData data) {
|
||||||
|
if (data == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!(data instanceof CraftBlockData)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
CraftBlockData craft = (CraftBlockData) data;
|
||||||
|
if (this.state.getBlock() != craft.state.getBlock()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fastpath an exact match
|
||||||
|
boolean exactMatch = this.equals(data);
|
||||||
|
|
||||||
|
// If that failed, do a merge and check
|
||||||
|
if (!exactMatch && craft.parsedStates != null) {
|
||||||
|
return this.merge(data).equals(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
return exactMatch;
|
||||||
|
}
|
||||||
|
|
||||||
private static final Map<Class, BiMap<Enum<?>, Enum<?>>> classMappings = new HashMap<>();
|
private static final Map<Class, BiMap<Enum<?>, Enum<?>>> classMappings = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -187,6 +230,7 @@ public class CraftBlockData implements BlockData {
|
|||||||
*/
|
*/
|
||||||
public <T extends Comparable<T>, V extends T> void set(IBlockState<T> ibs, V v) {
|
public <T extends Comparable<T>, V extends T> void set(IBlockState<T> ibs, V v) {
|
||||||
// Straight integer or boolean setter
|
// Straight integer or boolean setter
|
||||||
|
this.parsedStates = null;
|
||||||
this.state = this.state.set(ibs, v);
|
this.state = this.state.set(ibs, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -421,6 +465,7 @@ public class CraftBlockData implements BlockData {
|
|||||||
|
|
||||||
IBlockData blockData;
|
IBlockData blockData;
|
||||||
Block block = CraftMagicNumbers.getBlock(material);
|
Block block = CraftMagicNumbers.getBlock(material);
|
||||||
|
Set<IBlockState<?>> parsed = null;
|
||||||
|
|
||||||
// Data provided, use it
|
// Data provided, use it
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
@ -435,6 +480,7 @@ public class CraftBlockData implements BlockData {
|
|||||||
Preconditions.checkArgument(!reader.canRead(), "Spurious trailing data");
|
Preconditions.checkArgument(!reader.canRead(), "Spurious trailing data");
|
||||||
|
|
||||||
blockData = arg.b();
|
blockData = arg.b();
|
||||||
|
parsed = arg.a().keySet();
|
||||||
} catch (CommandSyntaxException ex) {
|
} catch (CommandSyntaxException ex) {
|
||||||
throw new IllegalArgumentException("Could not parse data: " + data, ex);
|
throw new IllegalArgumentException("Could not parse data: " + data, ex);
|
||||||
}
|
}
|
||||||
@ -442,7 +488,9 @@ public class CraftBlockData implements BlockData {
|
|||||||
blockData = block.getBlockData();
|
blockData = block.getBlockData();
|
||||||
}
|
}
|
||||||
|
|
||||||
return fromData(blockData);
|
CraftBlockData craft = fromData(blockData);
|
||||||
|
craft.parsedStates = parsed;
|
||||||
|
return craft;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CraftBlockData fromData(IBlockData data) {
|
public static CraftBlockData fromData(IBlockData data) {
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
package org.bukkit;
|
package org.bukkit;
|
||||||
|
|
||||||
import net.minecraft.server.BlockCake;
|
import net.minecraft.server.BlockCake;
|
||||||
|
import net.minecraft.server.BlockChest;
|
||||||
import net.minecraft.server.Blocks;
|
import net.minecraft.server.Blocks;
|
||||||
|
import net.minecraft.server.EnumDirection;
|
||||||
|
import org.bukkit.block.BlockFace;
|
||||||
import org.bukkit.block.data.BlockData;
|
import org.bukkit.block.data.BlockData;
|
||||||
import org.bukkit.block.data.type.Cake;
|
import org.bukkit.block.data.type.Cake;
|
||||||
|
import org.bukkit.block.data.type.Chest;
|
||||||
import org.bukkit.craftbukkit.block.data.CraftBlockData;
|
import org.bukkit.craftbukkit.block.data.CraftBlockData;
|
||||||
import org.bukkit.support.AbstractTestingBase;
|
import org.bukkit.support.AbstractTestingBase;
|
||||||
import static org.hamcrest.Matchers.*;
|
import static org.hamcrest.Matchers.*;
|
||||||
@ -72,4 +76,80 @@ public class BlockDataTest extends AbstractTestingBase {
|
|||||||
clone.setBites(1);
|
clone.setBites(1);
|
||||||
Assert.assertThat("Clone is not actually clone", clone, is(not(cakeTest)));
|
Assert.assertThat("Clone is not actually clone", clone, is(not(cakeTest)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMerge() {
|
||||||
|
Chest trueTarget = (Chest) CraftBlockData.newData(null, "minecraft:chest[facing=east,waterlogged=true]");
|
||||||
|
Chest falseTarget = (Chest) CraftBlockData.newData(null, "minecraft:chest[facing=east,waterlogged=false]");
|
||||||
|
Chest waterlogged = (Chest) CraftBlockData.newData(null, "minecraft:chest[waterlogged=true]");
|
||||||
|
|
||||||
|
BlockData candidate;
|
||||||
|
|
||||||
|
Assert.assertFalse("Target and match are not yet equal", trueTarget.equals(waterlogged));
|
||||||
|
candidate = trueTarget.merge(waterlogged);
|
||||||
|
Assert.assertTrue("Target and candidate are now equal", trueTarget.equals(candidate));
|
||||||
|
|
||||||
|
Assert.assertFalse("Target and match are not yet equal", falseTarget.equals(waterlogged));
|
||||||
|
candidate = falseTarget.merge(waterlogged);
|
||||||
|
Assert.assertFalse("Target and candidate are still not equal", falseTarget.equals(candidate));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMergeAny() {
|
||||||
|
Chest trueTarget = (Chest) CraftBlockData.newData(null, "minecraft:chest[facing=east,waterlogged=true]");
|
||||||
|
Chest falseTarget = (Chest) CraftBlockData.newData(null, "minecraft:chest[facing=east,waterlogged=false]");
|
||||||
|
Chest any = (Chest) CraftBlockData.newData(null, "minecraft:chest");
|
||||||
|
|
||||||
|
BlockData candidate;
|
||||||
|
|
||||||
|
Assert.assertFalse("Target and match are not yet equal", trueTarget.equals(any));
|
||||||
|
candidate = trueTarget.merge(any);
|
||||||
|
Assert.assertTrue("Target and candidate are now equal", trueTarget.equals(candidate));
|
||||||
|
|
||||||
|
Assert.assertFalse("Target and match are not yet equal", falseTarget.equals(any));
|
||||||
|
candidate = falseTarget.merge(any);
|
||||||
|
Assert.assertTrue("Target and candidate are now equal", falseTarget.equals(candidate));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testCannotMerge1() {
|
||||||
|
Chest one = (Chest) CraftBlockData.newData(null, "minecraft:chest[facing=east,waterlogged=true]");
|
||||||
|
Chest two = (Chest) CraftBlockData.fromData(Blocks.CHEST.getBlockData());
|
||||||
|
|
||||||
|
one.merge(two);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testCannotMerge2() {
|
||||||
|
Chest one = (Chest) CraftBlockData.newData(null, "minecraft:chest[waterlogged=true]");
|
||||||
|
Chest two = (Chest) CraftBlockData.newData(null, "minecraft:chest[waterlogged=true]");
|
||||||
|
|
||||||
|
one.merge(two);
|
||||||
|
|
||||||
|
two.setFacing(BlockFace.NORTH);
|
||||||
|
one.merge(two);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testCannotMerge3() {
|
||||||
|
Chest one = (Chest) CraftBlockData.newData(null, "minecraft:chest[waterlogged=true]");
|
||||||
|
Chest two = (Chest) CraftBlockData.newData(null, "minecraft:trapped_chest[waterlogged=true]");
|
||||||
|
|
||||||
|
one.merge(two);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMatch() {
|
||||||
|
Assert.assertTrue(CraftBlockData.newData(null, "minecraft:chest[facing=east,waterlogged=true]").matches(CraftBlockData.newData(null, "minecraft:chest[waterlogged=true]")));
|
||||||
|
Assert.assertFalse(CraftBlockData.newData(null, "minecraft:chest[facing=east,waterlogged=false]").matches(CraftBlockData.newData(null, "minecraft:chest[waterlogged=true]")));
|
||||||
|
Assert.assertTrue(CraftBlockData.newData(null, "minecraft:chest[facing=east,waterlogged=true]").matches(CraftBlockData.newData(null, "minecraft:chest")));
|
||||||
|
Assert.assertFalse(CraftBlockData.newData(null, "minecraft:trapped_chest[facing=east,waterlogged=false]").matches(CraftBlockData.newData(null, "minecraft:chest[waterlogged=true]")));
|
||||||
|
Assert.assertTrue(CraftBlockData.newData(null, "minecraft:chest[facing=east,waterlogged=true]").matches(CraftBlockData.newData(null, "minecraft:chest[waterlogged=true,facing=east]")));
|
||||||
|
|
||||||
|
Chest one = (Chest) CraftBlockData.fromData(Blocks.CHEST.getBlockData().set(BlockChest.FACING, EnumDirection.EAST));
|
||||||
|
Chest two = (Chest) CraftBlockData.newData(null, "minecraft:chest[waterlogged=false]");
|
||||||
|
|
||||||
|
Assert.assertTrue(one.matches(two));
|
||||||
|
Assert.assertFalse(two.matches(one));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren