13
0
geforkt von Mirrors/Paper

SPIGOT-7881: CTRL+Pick Block saves position data into item

By: md_5 <git@md-5.net>
Dieser Commit ist enthalten in:
CraftBukkit/Spigot 2024-09-15 17:17:10 +10:00
Ursprung 4b1526b609
Commit 5f1cd5748b
2 geänderte Dateien mit 57 neuen und 8 gelöschten Zeilen

Datei anzeigen

@ -109,12 +109,20 @@ public class CraftBlockEntityState<T extends TileEntity> extends CraftBlockState
return snapshot.saveWithFullMetadata(getRegistryAccess()); return snapshot.saveWithFullMetadata(getRegistryAccess());
} }
public NBTTagCompound getSnapshotNBTWithoutComponents() { public NBTTagCompound getItemNBT() {
NBTTagCompound nbt = getSnapshotNBT(); // update snapshot
applyTo(snapshot);
// See TileEntity#saveToItem
NBTTagCompound nbt = snapshot.saveCustomOnly(getRegistryAccess());
snapshot.removeComponentsFromTag(nbt); snapshot.removeComponentsFromTag(nbt);
return nbt; return nbt;
} }
public void addEntityType(NBTTagCompound nbt) {
TileEntity.addEntityType(nbt, snapshot.getType());
}
// gets the packet data of the TileEntity represented by this block state // gets the packet data of the TileEntity represented by this block state
public NBTTagCompound getUpdateNBT() { public NBTTagCompound getUpdateNBT() {
// update snapshot // update snapshot

Datei anzeigen

@ -26,6 +26,7 @@ import org.bukkit.craftbukkit.block.CraftBlockEntityState;
import org.bukkit.craftbukkit.block.CraftBlockStates; import org.bukkit.craftbukkit.block.CraftBlockStates;
import org.bukkit.craftbukkit.util.CraftMagicNumbers; import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.inventory.meta.BlockStateMeta; import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.util.BlockVector;
@DelegateDeserialization(SerializableMeta.class) @DelegateDeserialization(SerializableMeta.class)
public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta { public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta {
@ -55,6 +56,7 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
final Material material; final Material material;
private CraftBlockEntityState<?> blockEntityTag; private CraftBlockEntityState<?> blockEntityTag;
private BlockVector position;
private NBTTagCompound internalTag; private NBTTagCompound internalTag;
CraftMetaBlockState(CraftMetaItem meta, Material material) { CraftMetaBlockState(CraftMetaItem meta, Material material) {
@ -69,14 +71,20 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
CraftMetaBlockState te = (CraftMetaBlockState) meta; CraftMetaBlockState te = (CraftMetaBlockState) meta;
this.blockEntityTag = te.blockEntityTag; this.blockEntityTag = te.blockEntityTag;
this.position = te.position;
} }
CraftMetaBlockState(DataComponentPatch tag, Material material) { CraftMetaBlockState(DataComponentPatch tag, Material material) {
super(tag); super(tag);
this.material = material; this.material = material;
getOrEmpty(tag, BLOCK_ENTITY_TAG).ifPresent((nbt) -> { getOrEmpty(tag, BLOCK_ENTITY_TAG).ifPresent((blockTag) -> {
blockEntityTag = getBlockState(material, nbt.copyTag()); NBTTagCompound nbt = blockTag.copyTag();
blockEntityTag = getBlockState(material, nbt);
if (nbt.contains("x", CraftMagicNumbers.NBT.TAG_ANY_NUMBER) && nbt.contains("y", CraftMagicNumbers.NBT.TAG_ANY_NUMBER) && nbt.contains("z", CraftMagicNumbers.NBT.TAG_ANY_NUMBER)) {
position = new BlockVector(nbt.getInt("x"), nbt.getInt("y"), nbt.getInt("z"));
}
}); });
if (!tag.isEmpty()) { if (!tag.isEmpty()) {
@ -114,19 +122,39 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
blockEntityTag = getBlockState(material, internalTag); blockEntityTag = getBlockState(material, internalTag);
internalTag = null; internalTag = null;
} }
position = SerializableMeta.getObject(BlockVector.class, map, "blockPosition", true);
} }
@Override @Override
void applyToItem(CraftMetaItem.Applicator tag) { void applyToItem(CraftMetaItem.Applicator tag) {
super.applyToItem(tag); super.applyToItem(tag);
NBTTagCompound nbt = null;
if (blockEntityTag != null) { if (blockEntityTag != null) {
tag.put(BLOCK_ENTITY_TAG, CustomData.of(blockEntityTag.getSnapshotNBTWithoutComponents())); nbt = blockEntityTag.getItemNBT();
for (TypedDataComponent<?> component : blockEntityTag.collectComponents()) { for (TypedDataComponent<?> component : blockEntityTag.collectComponents()) {
tag.putIfAbsent(component); tag.putIfAbsent(component);
} }
} }
if (position != null) {
if (nbt == null) {
nbt = new NBTTagCompound();
}
nbt.putInt("x", position.getBlockX());
nbt.putInt("y", position.getBlockY());
nbt.putInt("z", position.getBlockZ());
}
if (nbt != null && !nbt.isEmpty()) {
CraftBlockEntityState<?> tile = (blockEntityTag != null) ? blockEntityTag : getBlockState(material, null);
// See ItemBlock#setBlockEntityData
tile.addEntityType(nbt);
tag.put(BLOCK_ENTITY_TAG, CustomData.of(nbt));
}
} }
@Override @Override
@ -149,6 +177,9 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
ImmutableMap.Builder<String, Object> serialize(ImmutableMap.Builder<String, Object> builder) { ImmutableMap.Builder<String, Object> serialize(ImmutableMap.Builder<String, Object> builder) {
super.serialize(builder); super.serialize(builder);
builder.put("blockMaterial", material.name()); builder.put("blockMaterial", material.name());
if (position != null) {
builder.put("blockPosition", position);
}
return builder; return builder;
} }
@ -159,6 +190,9 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
if (blockEntityTag != null) { if (blockEntityTag != null) {
hash = 61 * hash + this.blockEntityTag.hashCode(); hash = 61 * hash + this.blockEntityTag.hashCode();
} }
if (position != null) {
hash = 61 * hash + this.position.hashCode();
}
return original != hash ? CraftMetaBlockState.class.hashCode() ^ hash : hash; return original != hash ? CraftMetaBlockState.class.hashCode() ^ hash : hash;
} }
@ -170,19 +204,23 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
if (meta instanceof CraftMetaBlockState) { if (meta instanceof CraftMetaBlockState) {
CraftMetaBlockState that = (CraftMetaBlockState) meta; CraftMetaBlockState that = (CraftMetaBlockState) meta;
return Objects.equal(this.blockEntityTag, that.blockEntityTag); return Objects.equal(this.blockEntityTag, that.blockEntityTag) && Objects.equal(this.position, that.position);
} }
return true; return true;
} }
boolean isBlockStateEmpty() {
return !(blockEntityTag != null || position != null);
}
@Override @Override
boolean notUncommon(CraftMetaItem meta) { boolean notUncommon(CraftMetaItem meta) {
return super.notUncommon(meta) && (meta instanceof CraftMetaBlockState || blockEntityTag == null); return super.notUncommon(meta) && (meta instanceof CraftMetaBlockState || isBlockStateEmpty());
} }
@Override @Override
boolean isEmpty() { boolean isEmpty() {
return super.isEmpty() && blockEntityTag == null; return super.isEmpty() && isBlockStateEmpty();
} }
@Override @Override
@ -191,6 +229,9 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
if (blockEntityTag != null) { if (blockEntityTag != null) {
meta.blockEntityTag = blockEntityTag.copy(); meta.blockEntityTag = blockEntityTag.copy();
} }
if (position != null) {
meta.position = position.clone();
}
return meta; return meta;
} }