Rework BlockSnapshot and fix client notifications. Closes #6807

This commit is contained in:
LexManos 2020-06-25 18:41:38 -07:00
parent 9cf9305fd0
commit baf32612db
5 changed files with 60 additions and 120 deletions

View file

@ -9,7 +9,7 @@
BlockState blockstate2 = p_185266_1_.func_180495_p(blockpos); BlockState blockstate2 = p_185266_1_.func_180495_p(blockpos);
- if (blockstate2.func_185904_a() == Material.field_151586_h && blockstate2.func_177229_b(FlowingFluidBlock.field_176367_b) == 0 && blockstate.func_196955_c(p_185266_1_, blockpos) && p_185266_1_.func_226663_a_(blockstate, blockpos, ISelectionContext.func_216377_a())) { - if (blockstate2.func_185904_a() == Material.field_151586_h && blockstate2.func_177229_b(FlowingFluidBlock.field_176367_b) == 0 && blockstate.func_196955_c(p_185266_1_, blockpos) && p_185266_1_.func_226663_a_(blockstate, blockpos, ISelectionContext.func_216377_a())) {
+ boolean isFull = blockstate2.func_177230_c() == Blocks.field_150355_j && blockstate2.func_177229_b(FlowingFluidBlock.field_176367_b) == 0; //TODO: Forge, modded waters? + boolean isFull = blockstate2.func_177230_c() == Blocks.field_150355_j && blockstate2.func_177229_b(FlowingFluidBlock.field_176367_b) == 0; //TODO: Forge, modded waters?
+ if (blockstate2.func_185904_a() == Material.field_151586_h && isFull && blockstate.func_196955_c(p_185266_1_, blockpos) && p_185266_1_.func_226663_a_(blockstate, blockpos, ISelectionContext.func_216377_a()) && !net.minecraftforge.event.ForgeEventFactory.onBlockPlace(p_185266_0_, new net.minecraftforge.common.util.BlockSnapshot(p_185266_1_, blockpos, blockstate2), net.minecraft.util.Direction.UP)) { + if (blockstate2.func_185904_a() == Material.field_151586_h && isFull && blockstate.func_196955_c(p_185266_1_, blockpos) && p_185266_1_.func_226663_a_(blockstate, blockpos, ISelectionContext.func_216377_a()) && !net.minecraftforge.event.ForgeEventFactory.onBlockPlace(p_185266_0_, net.minecraftforge.common.util.BlockSnapshot.create(p_185266_1_, blockpos), net.minecraft.util.Direction.UP)) {
p_185266_1_.func_175656_a(blockpos, blockstate); p_185266_1_.func_175656_a(blockpos, blockstate);
p_185266_1_.func_205220_G_().func_205360_a(blockpos, Blocks.field_185778_de, MathHelper.func_76136_a(p_185266_0_.func_70681_au(), 60, 120)); p_185266_1_.func_205220_G_().func_205360_a(blockpos, Blocks.field_185778_de, MathHelper.func_76136_a(p_185266_0_.func_70681_au(), 60, 120));
} }

View file

@ -41,7 +41,7 @@
BlockState blockstate1 = iworld.func_180495_p(blockpos1); BlockState blockstate1 = iworld.func_180495_p(blockpos1);
BlockState blockstate2 = this.field_179475_a.func_195405_dq(); BlockState blockstate2 = this.field_179475_a.func_195405_dq();
- if (blockstate2 != null && this.func_220836_a(iworld, blockpos, blockstate2, blockstate, blockstate1, blockpos1)) { - if (blockstate2 != null && this.func_220836_a(iworld, blockpos, blockstate2, blockstate, blockstate1, blockpos1)) {
+ if (blockstate2 != null && this.func_220836_a(iworld, blockpos, blockstate2, blockstate, blockstate1, blockpos1) && !net.minecraftforge.event.ForgeEventFactory.onBlockPlace(field_179475_a, new net.minecraftforge.common.util.BlockSnapshot(iworld, blockpos, blockstate1), net.minecraft.util.Direction.UP)) { + if (blockstate2 != null && this.func_220836_a(iworld, blockpos, blockstate2, blockstate, blockstate1, blockpos1) && !net.minecraftforge.event.ForgeEventFactory.onBlockPlace(field_179475_a, net.minecraftforge.common.util.BlockSnapshot.create(iworld, blockpos1), net.minecraft.util.Direction.UP)) {
iworld.func_180501_a(blockpos, blockstate2, 3); iworld.func_180501_a(blockpos, blockstate2, 3);
this.field_179475_a.func_195406_b((BlockState)null); this.field_179475_a.func_195406_b((BlockState)null);
} }

View file

@ -24,7 +24,7 @@
private final RegistryKey<DimensionType> field_234915_C_; private final RegistryKey<DimensionType> field_234915_C_;
+ public boolean restoringBlockSnapshots = false; + public boolean restoringBlockSnapshots = false;
+ public boolean captureBlockSnapshots = false; + public boolean captureBlockSnapshots = false;
+ public java.util.ArrayList<net.minecraftforge.common.util.BlockSnapshot> capturedBlockSnapshots = new java.util.ArrayList<net.minecraftforge.common.util.BlockSnapshot>(); + public java.util.ArrayList<net.minecraftforge.common.util.BlockSnapshot> capturedBlockSnapshots = new java.util.ArrayList<>();
protected World(ISpawnWorldInfo p_i231617_1_, RegistryKey<World> p_i231617_2_, RegistryKey<DimensionType> p_i231617_3_, DimensionType p_i231617_4_, Supplier<IProfiler> p_i231617_5_, boolean p_i231617_6_, boolean p_i231617_7_, long p_i231617_8_) { protected World(ISpawnWorldInfo p_i231617_1_, RegistryKey<World> p_i231617_2_, RegistryKey<DimensionType> p_i231617_3_, DimensionType p_i231617_4_, Supplier<IProfiler> p_i231617_5_, boolean p_i231617_6_, boolean p_i231617_7_, long p_i231617_8_) {
+ super(World.class); + super(World.class);
@ -39,7 +39,7 @@
+ p_241211_1_ = p_241211_1_.func_185334_h(); // Forge - prevent mutable BlockPos leaks + p_241211_1_ = p_241211_1_.func_185334_h(); // Forge - prevent mutable BlockPos leaks
+ net.minecraftforge.common.util.BlockSnapshot blockSnapshot = null; + net.minecraftforge.common.util.BlockSnapshot blockSnapshot = null;
+ if (this.captureBlockSnapshots && !this.field_72995_K) { + if (this.captureBlockSnapshots && !this.field_72995_K) {
+ blockSnapshot = net.minecraftforge.common.util.BlockSnapshot.getBlockSnapshot(this, p_241211_1_, p_241211_3_); + blockSnapshot = net.minecraftforge.common.util.BlockSnapshot.create(this, p_241211_1_, p_241211_3_);
+ this.capturedBlockSnapshots.add(blockSnapshot); + this.capturedBlockSnapshots.add(blockSnapshot);
+ } + }
+ +

View file

@ -603,7 +603,7 @@ public class ForgeHooks
world.captureBlockSnapshots = false; world.captureBlockSnapshots = false;
if (ret == ActionResultType.SUCCESS) if (ret.isSuccessOrConsume())
{ {
// save new item data // save new item data
int newSize = itemstack.getCount(); int newSize = itemstack.getCount();
@ -654,12 +654,12 @@ public class ForgeHooks
int updateFlag = snap.getFlag(); int updateFlag = snap.getFlag();
BlockState oldBlock = snap.getReplacedBlock(); BlockState oldBlock = snap.getReplacedBlock();
BlockState newBlock = world.getBlockState(snap.getPos()); BlockState newBlock = world.getBlockState(snap.getPos());
if (!newBlock.getBlock().hasTileEntity(newBlock)) // Containers get placed automatically if (!newBlock.hasTileEntity()) // Containers get placed automatically
{ {
newBlock.onBlockAdded(world, snap.getPos(), oldBlock, false); newBlock.onBlockAdded(world, snap.getPos(), oldBlock, false);
} }
world.markAndNotifyBlock(snap.getPos(), null, oldBlock, newBlock, updateFlag, 512); world.markAndNotifyBlock(snap.getPos(), world.getChunkAt(snap.getPos()), oldBlock, newBlock, updateFlag, 512);
} }
player.addStat(Stats.ITEM_USED.get(item)); player.addStat(Stats.ITEM_USED.get(item));
} }

View file

@ -23,8 +23,10 @@ import java.lang.ref.WeakReference;
import java.util.Objects; import java.util.Objects;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.RegistryKey;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld; import net.minecraft.world.IWorld;
@ -45,98 +47,60 @@ public class BlockSnapshot
{ {
private static final boolean DEBUG = Boolean.parseBoolean(System.getProperty("forge.debugBlockSnapshot", "false")); private static final boolean DEBUG = Boolean.parseBoolean(System.getProperty("forge.debugBlockSnapshot", "false"));
private final RegistryKey<World> dim;
private final BlockPos pos; private final BlockPos pos;
private final int dimId; private final int flags;
@Nullable private final BlockState block;
private BlockState replacedBlock;
private int flag;
@Nullable @Nullable
private final CompoundNBT nbt; private final CompoundNBT nbt;
@Nullable @Nullable
private WeakReference<IWorld> world; private WeakReference<IWorld> world;
private final ResourceLocation registryName; private String toString = null;
private final int meta = 0; // TODO BlockSnapshot needs a total refactor for the absence of metadata
public BlockSnapshot(IWorld world, BlockPos pos, BlockState state) private BlockSnapshot(IWorld world, BlockPos pos, BlockState state, @Nullable CompoundNBT nbt, int flags)
{ {
this(world, pos, state, getTileNBT(world.getTileEntity(pos))); this.dim = world.getWorld().func_234923_W_();
}
public BlockSnapshot(IWorld world, BlockPos pos, BlockState state, @Nullable CompoundNBT nbt)
{
this.setWorld(world);
this.dimId = 0; //world.getDimension().getType().getId(); //TODO
this.pos = pos.toImmutable(); this.pos = pos.toImmutable();
this.setReplacedBlock(state); this.block = state;
this.registryName = state.getBlock().getRegistryName(); this.flags = flags;
this.setFlag(3);
this.nbt = nbt; this.nbt = nbt;
this.world = new WeakReference<>(world);
if (DEBUG) if (DEBUG)
{ System.out.println("Created " + this.toString());
System.out.printf("Created BlockSnapshot - [World: %s ][Location: %d,%d,%d ][Block: %s ][Meta: %d ]", world.getWorldInfo()/*TODO .getWorldName()*/, pos.getX(), pos.getY(), pos.getZ(), getRegistryName(), getMeta());
}
} }
public BlockSnapshot(IWorld world, BlockPos pos, BlockState state, int flag) public static BlockSnapshot create(IWorld world, BlockPos pos)
{ {
this(world, pos, state); return create(world, pos, 3);
this.setFlag(flag);
} }
/** public static BlockSnapshot create(IWorld world, BlockPos pos, int flag)
* Raw constructor designed for serialization usages.
*/
public BlockSnapshot(int dimension, BlockPos pos, ResourceLocation registryName, int meta, int flag, @Nullable CompoundNBT nbt)
{ {
this.dimId = dimension; return new BlockSnapshot(world, pos, world.getBlockState(pos), getTileNBT(world.getTileEntity(pos)), flag);
this.pos = pos.toImmutable();
this.setFlag(flag);
this.registryName = registryName;
this.nbt = nbt;
}
public static BlockSnapshot getBlockSnapshot(IWorld world, BlockPos pos)
{
return new BlockSnapshot(world, pos, world.getBlockState(pos));
}
public static BlockSnapshot getBlockSnapshot(IWorld world, BlockPos pos, int flag)
{
return new BlockSnapshot(world, pos, world.getBlockState(pos), flag);
}
public static BlockSnapshot readFromNBT(CompoundNBT tag)
{
return new BlockSnapshot(
tag.getInt("dimension"),
new BlockPos(tag.getInt("posX"), tag.getInt("posY"), tag.getInt("posZ")),
new ResourceLocation(tag.getString("blockMod"), tag.getString("blockName")),
tag.getInt("metadata"),
tag.getInt("flag"),
tag.getBoolean("hasTE") ? tag.getCompound("tileEntity") : null);
} }
@Nullable @Nullable
private static CompoundNBT getTileNBT(@Nullable TileEntity te) private static CompoundNBT getTileNBT(@Nullable TileEntity te)
{ {
if (te == null) return null; return te == null ? null : te.write(new CompoundNBT());
CompoundNBT nbt = new CompoundNBT();
te.write(nbt);
return nbt;
} }
public BlockState getCurrentBlock() public BlockState getCurrentBlock()
{ {
return getWorld().getBlockState(getPos()); IWorld world = getWorld();
return world == null ? Blocks.AIR.getDefaultState() : world.getBlockState(this.pos);
} }
@Nullable
public IWorld getWorld() public IWorld getWorld()
{ {
IWorld world = this.world != null ? this.world.get() : null; IWorld world = this.world != null ? this.world.get() : null;
if (world == null) if (world == null)
{ {
//TODO world = ServerLifecycleHooks.getCurrentServer().getWorld(this.dim);
//world = ServerLifecycleHooks.getCurrentServer().getWorld(DimensionType.getById(getDimId()));
this.world = new WeakReference<IWorld>(world); this.world = new WeakReference<IWorld>(world);
} }
return world; return world;
@ -144,11 +108,7 @@ public class BlockSnapshot
public BlockState getReplacedBlock() public BlockState getReplacedBlock()
{ {
if (this.replacedBlock == null) return this.block;
{
this.replacedBlock = ForgeRegistries.BLOCKS.getValue(getRegistryName()).getStateById(getMeta());
}
return this.replacedBlock;
} }
@Nullable @Nullable
@ -182,14 +142,10 @@ public class BlockSnapshot
if (current != replaced) if (current != replaced)
{ {
if (force) if (force)
{
world.setBlockState(pos, replaced, flags); world.setBlockState(pos, replaced, flags);
}
else else
{
return false; return false;
} }
}
world.setBlockState(pos, replaced, flags); world.setBlockState(pos, replaced, flags);
if (world instanceof World) if (world instanceof World)
@ -207,31 +163,10 @@ public class BlockSnapshot
} }
if (DEBUG) if (DEBUG)
{ System.out.println("Restored " + this.toString());
System.out.printf("Restored BlockSnapshot with data [World: %s ][Location: %d,%d,%d ][State: %s ][Block: %s ][TileEntity: %s ][force: %s ][notifyNeighbors: %s]", world.getWorldInfo()/*TODO .getWorldName()*/, pos.getX(), pos.getY(), pos.getZ(), replaced, replaced.getBlock().delegate.name(), te, force, notifyNeighbors);
}
return true; return true;
} }
public void writeToNBT(CompoundNBT compound)
{
compound.putString("blockMod", getRegistryName().getNamespace());
compound.putString("blockName", getRegistryName().getPath());
compound.putInt("posX", getPos().getX());
compound.putInt("posY", getPos().getY());
compound.putInt("posZ", getPos().getZ());
compound.putInt("flag", getFlag());
compound.putInt("dimension", getDimId());
compound.putInt("metadata", getMeta());
compound.putBoolean("hasTE", getNbt() != null);
if (getNbt() != null)
{
compound.put("tileEntity", getNbt());
}
}
@Override @Override
public boolean equals(Object obj) public boolean equals(Object obj)
{ {
@ -241,43 +176,48 @@ public class BlockSnapshot
return false; return false;
final BlockSnapshot other = (BlockSnapshot) obj; final BlockSnapshot other = (BlockSnapshot) obj;
if (this.getMeta() != other.getMeta() || return this.dim.equals(other.dim) &&
this.getDimId() != other.getDimId() || this.pos.equals(other.pos) &&
!this.getPos().equals(other.getPos()) || this.block == other.block &&
!this.getRegistryName().equals(other.getRegistryName()) || this.flags == other.flags &&
!Objects.equals(this.getNbt(), other.getNbt())) Objects.equals(this.nbt, other.nbt);
return false;
return true;
} }
@Override @Override
public int hashCode() public int hashCode()
{ {
int hash = 7; int hash = 7;
hash = 73 * hash + this.getMeta(); hash = 73 * hash + this.dim.hashCode();
hash = 73 * hash + this.getDimId(); hash = 73 * hash + this.pos.hashCode();
hash = 73 * hash + this.getPos().hashCode(); hash = 73 * hash + this.block.hashCode();
hash = 73 * hash + this.getRegistryName().hashCode(); hash = 73 * hash + this.flags;
hash = 73 * hash + Objects.hashCode(this.getNbt()); hash = 73 * hash + Objects.hashCode(this.getNbt());
return hash; return hash;
} }
@Override
public String toString()
{
if (toString == null)
{
this.toString =
"BlockSnapshot[" +
"World:" + this.dim.func_240901_a_() + ',' +
"Pos: " + this.pos + ',' +
"State: " + this.block + ',' +
"Flags: " + this.flags + ',' +
"NBT: " + (this.nbt == null ? "null" : this.nbt.toString()) +
']';
}
return this.toString;
}
public BlockPos getPos() { return pos; } public BlockPos getPos() { return pos; }
public int getDimId() { return dimId; }
public void setReplacedBlock(BlockState replacedBlock) { this.replacedBlock = replacedBlock; } public int getFlag() { return flags; }
public int getFlag() { return flag; }
public void setFlag(int flag) { this.flag = flag; }
@Nullable @Nullable
public CompoundNBT getNbt() { return nbt; } public CompoundNBT getNbt() { return nbt; }
public void setWorld(IWorld world) { this.world = new WeakReference<>(world); }
public ResourceLocation getRegistryName() { return registryName; }
public int getMeta() { return meta; }
} }