Migrate piston relay to proper per-world WSD

This commit is contained in:
Vincent Lee committed 2020-02-20 22:59:12 -06:00
1 parent fe0bf9407b
commit 612d8f886e
3 files changed
+45 -68

No files matched your search

@@ -22,6 +22,7 @@ import net.minecraft.entity.item.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.nbt.NBTDynamicOps;
import net.minecraft.server.MinecraftServer;
@@ -60,9 +61,6 @@ public class BlockPistonRelay extends BlockMod implements IWandable {
// Currently active binding attempts
public final Map<UUID, GlobalPos> activeBindingAttempts = new HashMap<>();
// Bindings
public final Map<GlobalPos, GlobalPos> mappedPositions = new HashMap<>();
private final Set<GlobalPos> removeQueue = new HashSet<>();
private final Set<GlobalPos> checkedCoords = new HashSet<>();
private final Map<GlobalPos, Integer> coordsToCheck = new HashMap<>();
@@ -121,12 +119,12 @@ public class BlockPistonRelay extends BlockMod implements IWandable {
activeBindingAttempts.put(player.getUniqueID(), clicked);
world.playSound(null, pos, ModSounds.ding, SoundCategory.BLOCKS, 0.5F, 1F);
} else {
GlobalPos dest = mappedPositions.get(clicked);
BlockPos dest = WorldData.get(world).mapping.get(pos);
if (dest != null) {
PacketHandler.sendToNearby(world, pos,
new PacketBotaniaEffect(PacketBotaniaEffect.EffectType.PARTICLE_BEAM,
pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5,
dest.getPos().getX(), dest.getPos().getY(), dest.getPos().getZ()));
dest.getX(), dest.getY(), dest.getZ()));
}
}
} else {
@@ -137,21 +135,10 @@ public class BlockPistonRelay extends BlockMod implements IWandable {
return true;
}
@SubscribeEvent
public void onWorldLoad(WorldEvent.Load event) {
if(!event.getWorld().getWorld().isRemote && event.getWorld().getDimension().getType() == DimensionType.OVERWORLD)
WorldData.get(event.getWorld().getWorld());
}
@SubscribeEvent
public void onWorldUnload(WorldEvent.Unload event) {
if(!event.getWorld().getWorld().isRemote && event.getWorld().getDimension().getType() == DimensionType.OVERWORLD)
WorldData.get(event.getWorld().getWorld()).markDirty();
}
public static class WorldData extends WorldSavedData {
private static final String ID = "PistonRelayPairs";
public final Map<BlockPos, BlockPos> mapping = new HashMap<>();
public WorldData() {
super(ID);
@@ -159,16 +146,16 @@ public class BlockPistonRelay extends BlockMod implements IWandable {
@Override
public void read(@Nonnull CompoundNBT cmp) {
((BlockPistonRelay) ModBlocks.pistonRelay).mappedPositions.clear();
mapping.clear();
ListNBT list = cmp.getList("list", Constants.NBT.TAG_COMPOUND);
ListNBT list = cmp.getList("list", Constants.NBT.TAG_INT_ARRAY);
for (int i = 0; i < list.size(); i += 2) {
CompoundNBT from = list.getCompound(i);
CompoundNBT to = list.getCompound(i + 1);
GlobalPos fromPos = GlobalPos.deserialize(new Dynamic<>(NBTDynamicOps.INSTANCE, from));
GlobalPos toPos = GlobalPos.deserialize(new Dynamic<>(NBTDynamicOps.INSTANCE, to));
INBT from = list.get(i);
INBT to = list.get(i + 1);
BlockPos fromPos = BlockPos.deserialize(new Dynamic<>(NBTDynamicOps.INSTANCE, from));
BlockPos toPos = BlockPos.deserialize(new Dynamic<>(NBTDynamicOps.INSTANCE, to));
((BlockPistonRelay) ModBlocks.pistonRelay).mappedPositions.put(fromPos, toPos);
mapping.put(fromPos, toPos);
}
}
@@ -176,7 +163,7 @@ public class BlockPistonRelay extends BlockMod implements IWandable {
@Override
public CompoundNBT write(@Nonnull CompoundNBT cmp) {
ListNBT list = new ListNBT();
for (Map.Entry<GlobalPos, GlobalPos> e : ((BlockPistonRelay) ModBlocks.pistonRelay).mappedPositions.entrySet()) {
for (Map.Entry<BlockPos, BlockPos> e : mapping.entrySet()) {
list.add(e.getKey().serialize(NBTDynamicOps.INSTANCE));
list.add(e.getValue().serialize(NBTDynamicOps.INSTANCE));
}
@@ -198,7 +185,11 @@ public class BlockPistonRelay extends BlockMod implements IWandable {
@SubscribeEvent
public void tickEnd(TickEvent.ServerTickEvent event) {
if(event.type == TickEvent.Type.SERVER && event.phase == TickEvent.Phase.END) {
MinecraftServer server = ServerLifecycleHooks.getCurrentServer();
for(GlobalPos s : coordsToCheck.keySet()) {
ServerWorld world = server.getWorld(s.getDimension());
WorldData data = WorldData.get(world);
decrCoords(s);
if(checkedCoords.contains(s))
continue;
@@ -209,16 +200,13 @@ public class BlockPistonRelay extends BlockMod implements IWandable {
boolean sticky = PistonType.STICKY == state.get(MovingPistonBlock.TYPE);
Direction dir = state.get(MovingPistonBlock.FACING);
MinecraftServer server = ServerLifecycleHooks.getCurrentServer();
if(server != null && getTimeInCoords(s) == 0) {
GlobalPos newPos;
if(getTimeInCoords(s) == 0) {
BlockPos newPos;
// Put the relay back, or drop it
{
int x = s.getPos().getX(), y = s.getPos().getY(), z = s.getPos().getZ();
BlockPos pos = s.getPos();
World world = server.getWorld(s.getDimension());
if(world.isAirBlock(pos.offset(dir)))
world.setBlockState(pos.offset(dir), ModBlocks.pistonRelay.getDefaultState());
else if(!world.isRemote) {
@@ -226,14 +214,12 @@ public class BlockPistonRelay extends BlockMod implements IWandable {
world.addEntity(new ItemEntity(world, x + dir.getXOffset(), y + dir.getYOffset(), z + dir.getZOffset(), stack));
}
checkedCoords.add(s);
newPos = GlobalPos.of(world.getDimension().getType(), pos.offset(dir));
newPos = pos.offset(dir);
}
// Move the linked block and update the mapping
if(mappedPositions.containsKey(s)) {
GlobalPos dest = mappedPositions.get(s);
BlockPos destPos = dest.getPos();
World world = server.getWorld(dest.getDimension());
if(data.mapping.containsKey(s.getPos())) {
BlockPos destPos = data.mapping.get(s.getPos());
BlockState srcState = world.getBlockState(destPos);
TileEntity tile = world.getTileEntity(destPos);
@@ -243,14 +229,14 @@ public class BlockPistonRelay extends BlockMod implements IWandable {
if(world.isAirBlock(destPos.offset(dir)) || destMat.isReplaceable()) {
world.setBlockState(destPos, Blocks.AIR.getDefaultState());
world.setBlockState(destPos.offset(dir), srcState);
mappedPositions.put(s, GlobalPos.of(world.getDimension().getType(), destPos.offset(dir)));
data.mapping.put(s.getPos(), destPos.offset(dir));
}
}
dest = mappedPositions.get(s);
mappedPositions.remove(s);
mappedPositions.put(newPos, dest);
save(world);
destPos = data.mapping.get(s.getPos());
data.mapping.remove(s.getPos());
data.mapping.put(newPos, destPos);
data.markDirty();
}
}
}
@@ -261,10 +247,4 @@ public class BlockPistonRelay extends BlockMod implements IWandable {
checkedCoords.removeAll(removeQueue);
removeQueue.clear();
}
private void save(World world) {
WorldData data = WorldData.get(world);
if(data != null)
data.markDirty();
}
}
@@ -145,20 +145,17 @@ public class ItemTwigWand extends Item implements ICoordBoundItem {
BlockPos pos = ctx.getPos();
PlayerEntity player = ctx.getPlayer();
if(((BlockPistonRelay) ModBlocks.pistonRelay).activeBindingAttempts.containsKey(player.getUniqueID())) {
GlobalPos bindPos = ((BlockPistonRelay) ModBlocks.pistonRelay).activeBindingAttempts.get(player.getUniqueID());
GlobalPos currentPos = GlobalPos.of(world.getDimension().getType(), pos.toImmutable());
GlobalPos bindPos = ((BlockPistonRelay) ModBlocks.pistonRelay).activeBindingAttempts.get(player.getUniqueID());
if(bindPos != null && bindPos.getDimension() == world.getDimension().getType()) {
((BlockPistonRelay) ModBlocks.pistonRelay).activeBindingAttempts.remove(player.getUniqueID());
((BlockPistonRelay) ModBlocks.pistonRelay).mappedPositions.put(bindPos, currentPos);
BlockPistonRelay.WorldData.get(world).markDirty();
BlockPistonRelay.WorldData data = BlockPistonRelay.WorldData.get(world);
data.mapping.put(bindPos.getPos(), pos.toImmutable());
data.markDirty();
if(bindPos.getDimension() == currentPos.getDimension()) {
PacketHandler.sendToNearby(world, pos,
new PacketBotaniaEffect(PacketBotaniaEffect.EffectType.PARTICLE_BEAM,
bindPos.getPos().getX() + 0.5, bindPos.getPos().getY() + 0.5, bindPos.getPos().getZ() + 0.5,
pos.getX(), pos.getY(), pos.getZ()));
}
PacketHandler.sendToNearby(world, pos,
new PacketBotaniaEffect(PacketBotaniaEffect.EffectType.PARTICLE_BEAM,
bindPos.getPos().getX() + 0.5, bindPos.getPos().getY() + 0.5, bindPos.getPos().getZ() + 0.5,
pos.getX(), pos.getY(), pos.getZ()));
world.playSound(null, player.getX(), player.getY(), player.getZ(), ModSounds.ding, SoundCategory.PLAYERS, 1F, 1F);
return true;
@@ -34,16 +34,16 @@ public class LensWarp extends Lens {
BlockPos hit = ((BlockRayTraceResult) pos).getPos();
Block block = entity.world.getBlockState(hit).getBlock();
if(block == ModBlocks.pistonRelay) {
GlobalPos key = ((BlockPistonRelay) ModBlocks.pistonRelay).mappedPositions.get(GlobalPos.of(entity.world.getDimension().getType(), hit));
if(key != null) {
if(key.getDimension() == entity.world.getDimension().getType()) {
entity.setPosition(key.getPos().getX() + 0.5, key.getPos().getY() + 0.5, key.getPos().getZ() + 0.5);
burst.setCollidedAt(key.getPos());
entity.getPersistentData().putBoolean(TAG_WARPED, true);
return false;
}
BlockPistonRelay.WorldData data = BlockPistonRelay.WorldData.get(entity.world);
BlockPos dest = data.mapping.get(hit);
if(dest != null) {
entity.setPosition(dest.getX() + 0.5, dest.getY() + 0.5, dest.getZ() + 0.5);
burst.setCollidedAt(dest);
entity.getPersistentData().putBoolean(TAG_WARPED, true);
return false;
}
}
return dead;