Updated block event handling

- reuse BlockPos to reduce memory load
- support multi-block placement
- prevent Nether portal opening outside world border
Fixed bad multi-threading when enabling/disabling accelerator points with CC or OC
This commit is contained in:
Unknown 2019-07-29 10:46:37 +02:00 committed by unknown
parent 73ae4e9039
commit f6fe697199
15 changed files with 188 additions and 55 deletions

View file

@ -2,10 +2,13 @@ package cr0s.warpdrive.api;
import cr0s.warpdrive.api.computer.ICoreSignature;
import cr0s.warpdrive.data.EnumStarMapEntryType;
import cr0s.warpdrive.data.VectorI;
import javax.annotation.Nullable;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
public interface IStarMapRegistryTileEntity extends ICoreSignature {
@ -21,6 +24,6 @@ public interface IStarMapRegistryTileEntity extends ICoreSignature {
// isolation rate from radars
double getIsolationRate();
// report an update in the area
void onBlockUpdatedInArea(final VectorI vector, final IBlockState blockState);
// report an update in the area, return false to cancel it
boolean onBlockUpdatingInArea(@Nullable final Entity entity, final BlockPos blockPos, final IBlockState blockState);
}

View file

@ -165,7 +165,7 @@ public abstract class TileEntityAbstractBase extends TileEntity implements IBloc
isDirty = false;
final IBlockState blockState = world.getBlockState(pos);
world.notifyBlockUpdate(pos, blockState, blockState, 3);
WarpDrive.starMap.onBlockUpdated(world, pos, blockState);
WarpDrive.starMap.onBlockUpdating(null, world, pos, blockState);
} else {
isDirty = true;
}

View file

@ -195,6 +195,8 @@ public abstract class TileEntityAbstractMachine extends TileEntityAbstractInterf
public void setIsEnabled(final boolean isEnabled) {
this.isEnabled = isEnabled;
// force update through main thread since CC & OC are running outside the main thread
markDirty();
}
// Common OC/CC methods

View file

@ -75,7 +75,7 @@ public class TileEntityAcceleratorControlPoint extends TileEntityAbstractMachine
if (WarpDriveConfig.LOGGING_VIDEO_CHANNEL) {
WarpDrive.logger.info(this + " Accelerator control point controlChannel channel set to " + controlChannel);
}
// force update through main thread since CC runs on server as 'client'
// force update through main thread since CC & OC are running outside the main thread
markDirty();
}
}
@ -128,12 +128,6 @@ public class TileEntityAcceleratorControlPoint extends TileEntityAbstractMachine
readFromNBT(tagCompound);
}
@Override
public void setIsEnabled(final boolean isEnabled) {
super.setIsEnabled(isEnabled);
WarpDrive.starMap.onBlockUpdated(world, pos, world.getBlockState(pos));
}
// Common OC/CC methods
public Object[] controlChannel(final Object[] arguments) {
if ( arguments != null

View file

@ -29,6 +29,7 @@ import li.cil.oc.api.machine.Callback;
import li.cil.oc.api.machine.Context;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@ -1167,22 +1168,23 @@ public class TileEntityAcceleratorCore extends TileEntityAbstractEnergyCoreOrCon
}
@Override
public void onBlockUpdatedInArea(final VectorI vector, final IBlockState blockState) {
public boolean onBlockUpdatingInArea(@Nullable final Entity entity, final BlockPos blockPos, final IBlockState blockState) {
// skip in case of explosion, etc.
if (isDirtyAssembly()) {
return;
return true;
}
// check for significant change
// (we don't check the controller itself: it'll be triggered in invalidate() and we don't want to reevaluate the setup at that point)
if ( blockState.getBlock() instanceof BlockAbstractAccelerator
|| blockState.getBlock() instanceof BlockCapacitor ) {
if (WarpDriveConfig.LOGGING_ACCELERATOR) {
WarpDrive.logger.info(String.format("onBlockUpdatingInArea %s %s",
blockState,
Commons.format(world, blockPos) ));
}
markDirtyAssembly();
return;
}
if (WarpDriveConfig.LOGGING_ACCELERATOR) {
WarpDrive.logger.info(String.format("onBlockUpdatedInArea block %s",
blockState ));
}
return true;
}
}

View file

@ -76,7 +76,7 @@ public class TileEntityCamera extends TileEntityAbstractMachine implements IVide
WarpDrive.logger.info(this + " Video channel set to " + videoChannel);
}
markDirty();
// force update through main thread since CC runs on server as 'client'
// force update through main thread since CC & OC are running outside the main thread
packetSendTicks = 0;
registryUpdateTicks = 0;
}

View file

@ -62,7 +62,7 @@ public class TileEntityMonitor extends TileEntityAbstractMachine implements IVid
WarpDrive.logger.info(this + " Monitor video channel set to " + videoChannel);
}
markDirty();
// force update through main thread since CC runs on server as 'client'
// force update through main thread since CC & OC are running outside the main thread
packetSendTicks = 0;
}
}

View file

@ -7,18 +7,20 @@ import cr0s.warpdrive.block.TileEntityAbstractEnergyCoreOrController;
import cr0s.warpdrive.config.WarpDriveConfig;
import cr0s.warpdrive.data.EnumStarMapEntryType;
import cr0s.warpdrive.data.Vector3;
import cr0s.warpdrive.data.VectorI;
import cr0s.warpdrive.render.EntityFXBoundingBox;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.client.FMLClientHandler;
import net.minecraftforge.fml.relauncher.Side;
@ -199,8 +201,9 @@ public class TileEntityJumpGateCore extends TileEntityAbstractEnergyCoreOrContro
}
@Override
public void onBlockUpdatedInArea(final VectorI vector, final IBlockState blockState) {
public boolean onBlockUpdatingInArea(@Nullable final Entity entity, final BlockPos blockPos, final IBlockState blockState) {
// no operation
return true;
}
// Common OC/CC methods

View file

@ -27,6 +27,7 @@ import cr0s.warpdrive.event.JumpSequencer;
import cr0s.warpdrive.render.EntityFXBoundingBox;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Set;
@ -1206,8 +1207,9 @@ public class TileEntityShipCore extends TileEntityAbstractShipController impleme
}
@Override
public void onBlockUpdatedInArea(final VectorI vector, final IBlockState blockState) {
public boolean onBlockUpdatingInArea(@Nullable final Entity entity, final BlockPos blockPos, final IBlockState blockState) {
// no operation
return true;
}
// Common OC/CC methods

View file

@ -35,6 +35,7 @@ import li.cil.oc.api.machine.Callback;
import li.cil.oc.api.machine.Context;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@ -524,10 +525,10 @@ public class TileEntityTransporterCore extends TileEntityAbstractEnergyCoreOrCon
}
@Override
public void onBlockUpdatedInArea(final VectorI vector, final IBlockState blockState) {
public boolean onBlockUpdatingInArea(@Nullable final Entity entity, final BlockPos blockPos, final IBlockState blockState) {
// skip in case of explosion, etc.
if (isDirtyAssembly()) {
return;
return true;
}
// check for significant change
@ -535,21 +536,23 @@ public class TileEntityTransporterCore extends TileEntityAbstractEnergyCoreOrCon
if ( block instanceof BlockTransporterScanner
|| block instanceof BlockTransporterContainment) {
markDirtyAssembly();
return;
return true;
}
if ( aabbLocalScanners != null
&& vector.x >= aabbLocalScanners.minX
&& vector.y >= aabbLocalScanners.minY
&& vector.z >= aabbLocalScanners.minZ
&& vector.x < aabbLocalScanners.maxX
&& vector.y < aabbLocalScanners.maxY
&& vector.z < aabbLocalScanners.maxZ ) {
&& blockPos.getX() >= aabbLocalScanners.minX
&& blockPos.getY() >= aabbLocalScanners.minY
&& blockPos.getZ() >= aabbLocalScanners.minZ
&& blockPos.getX() < aabbLocalScanners.maxX
&& blockPos.getY() < aabbLocalScanners.maxY
&& blockPos.getZ() < aabbLocalScanners.maxZ ) {
if (WarpDriveConfig.LOGGING_TRANSPORTER) {
WarpDrive.logger.info(String.format("onBlockUpdatingInArea %s %s",
blockState,
Commons.format(world, blockPos) ));
}
markDirtyAssembly();
}
if (WarpDriveConfig.LOGGING_TRANSPORTER) {
WarpDrive.logger.info(String.format("onBlockUpdatedInArea block %s", block));
}
return true;
}
@Override

View file

@ -76,7 +76,7 @@ public class TileEntityLaserCamera extends TileEntityLaser implements IVideoChan
WarpDrive.logger.info(this + " Video channel updated from " + videoChannel + " to " + parVideoChannel);
}
markDirty();
// force update through main thread since CC runs on server as 'client'
// force update through main thread since CC & OC are running outside the main thread
packetSendTicks = 0;
registryUpdateTicks = 0;
}

View file

@ -1,5 +1,6 @@
package cr0s.warpdrive.data;
import cr0s.warpdrive.Commons;
import cr0s.warpdrive.WarpDrive;
import cr0s.warpdrive.config.InvalidXmlException;
import cr0s.warpdrive.config.XmlFileManager;
@ -19,6 +20,7 @@ import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;
@ -165,6 +167,38 @@ public class CelestialObjectManager extends XmlFileManager {
return nbtTagList;
}
public static boolean onOpeningNetherPortal(@Nonnull final World world, @Nonnull final BlockPos blockPos) {
// prevent creating a portal outside the world border
final CelestialObject celestialObjectPortal = get(world, blockPos.getX(), blockPos.getZ());
if (celestialObjectPortal != null) {
if (!celestialObjectPortal.isInsideBorder(blockPos.getX(), blockPos.getZ()) ) {
return false;
}
// @TODO prevent creating a portal in specific dimensions
}
// prevent creating a portal leading outside the world border
final boolean isInTheNether = world.provider.getDimension() == -1;
final CelestialObject celestialObjectExit = get(false, isInTheNether ? 0 : -1, 0, 0);
if (celestialObjectExit != null) {
final double factor = isInTheNether ? 8.0D : 1 / 8.0D;
final int xExit = (int) Math.floor(blockPos.getX() * factor);
final int zExit = (int) Math.floor(blockPos.getZ() * factor);
if ( Math.abs(xExit - celestialObjectExit.dimensionCenterX) > celestialObjectExit.borderRadiusX
|| Math.abs(zExit - celestialObjectExit.dimensionCenterZ) > celestialObjectExit.borderRadiusZ ) {
return false;
}
}
if (WarpDrive.isDev) {
WarpDrive.logger.info(String.format("Opening Nether portal %s",
Commons.format(world, blockPos) ));
}
return true;
}
// *** client side only ***
@SideOnly(Side.CLIENT)

View file

@ -11,6 +11,7 @@ import cr0s.warpdrive.block.movement.TileEntityShipCore;
import cr0s.warpdrive.config.WarpDriveConfig;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@ -262,19 +263,26 @@ public class StarMapRegistry {
return result;
}
public void onBlockUpdated(@Nonnull final World world, @Nonnull final BlockPos blockPos, final IBlockState blockState) {
public boolean onBlockUpdating(@Nullable final Entity entity, @Nonnull final World world, @Nonnull final BlockPos blockPos, final IBlockState blockState) {
if (!Commons.isSafeThread()) {
WarpDrive.logger.error(String.format("Non-threadsafe call to StarMapRegistry:onBlockUpdating outside main thread, for %s %s",
blockState, Commons.format(world, blockPos)));
return false;
}
final CopyOnWriteArraySet<StarMapRegistryItem> setStarMapRegistryItems = registry.get(world.provider.getDimension());
if (setStarMapRegistryItems == null) {
return;
return true;
}
boolean isAllowed = true;
for (final StarMapRegistryItem registryItem : setStarMapRegistryItems) {
if (registryItem.contains(blockPos)) {
final TileEntity tileEntity = world.getTileEntity(new BlockPos(registryItem.x, registryItem.y, registryItem.z));
if (tileEntity instanceof IStarMapRegistryTileEntity) {
((IStarMapRegistryTileEntity) tileEntity).onBlockUpdatedInArea(new VectorI(blockPos), blockState);
isAllowed = isAllowed && ((IStarMapRegistryTileEntity) tileEntity).onBlockUpdatingInArea(entity, blockPos, blockState);
}
}
}
return isAllowed;
}
public static double getGravity(final Entity entity) {

View file

@ -8,6 +8,7 @@ import cr0s.warpdrive.config.WarpDriveConfig;
import cr0s.warpdrive.data.ChunkData;
import cr0s.warpdrive.data.StateAir;
import javax.annotation.Nonnull;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.Iterator;
@ -16,6 +17,7 @@ import java.util.Map;
import java.util.Map.Entry;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.World;
@ -231,16 +233,16 @@ public class ChunkHandler {
}
@SuppressWarnings("ConstantConditions")
public static void onBlockUpdated(final World world, final int x, final int y, final int z) {
public static void onBlockUpdated(@Nonnull final World world, @Nonnull final BlockPos blockPos) {
if (!world.isRemote) {
final ChunkData chunkData = getChunkData(world, x, y, z);
final ChunkData chunkData = getChunkData(world, blockPos.getX(), blockPos.getY(), blockPos.getZ());
if (chunkData != null) {
chunkData.onBlockUpdated(x, y, z);
chunkData.onBlockUpdated(blockPos.getX(), blockPos.getY(), blockPos.getZ());
} else {
if (WarpDriveConfig.LOGGING_WORLD_GENERATION) {
WarpDrive.logger.error(String.format("%s block updating %s, while chunk isn't loaded!",
world.isRemote ? "Client" : "Server",
Commons.format(world, x, y, z)));
Commons.format(world, blockPos)));
Commons.dumpAllThreads();
}
}

View file

@ -1,17 +1,28 @@
package cr0s.warpdrive.event;
import cr0s.warpdrive.BreathingManager;
import cr0s.warpdrive.Commons;
import cr0s.warpdrive.WarpDrive;
import cr0s.warpdrive.data.CelestialObjectManager;
import cr0s.warpdrive.config.WarpDriveConfig;
import cr0s.warpdrive.data.CelestialObject;
import cr0s.warpdrive.network.PacketHandler;
import java.util.List;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.common.util.BlockSnapshot;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.event.world.ChunkWatchEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerChangedDimensionEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
@ -19,10 +30,6 @@ import net.minecraftforge.fml.common.gameevent.TickEvent.ServerTickEvent;
import net.minecraftforge.fml.common.network.FMLNetworkEvent.ClientConnectedToServerEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.event.world.ChunkWatchEvent;
import net.minecraftforge.event.world.WorldEvent;
/**
*
@ -122,17 +129,90 @@ public class WorldHandler {
LivingHandler.updateTick();
}
// BreakEvent = entity is breaking a block (no ancestor)
// EntityPlaceEvent = entity is EntityFallingBlock
// NeighborNotifyEvent = neighbours update, snow placed/removed by environment, WorldEdit (can't be cancelled)
// PlaceEvent (EntityPlaceEvent) = player is (re)placing a block
// PortalSpawnEvent = nether portal is opening (fire placed inside an obsidian frame)
@SuppressWarnings("ConstantConditions")
@SubscribeEvent
public void onBlockUpdated(final BlockEvent blockEvent) {
if (WarpDriveConfig.LOGGING_BREAK_PLACE && WarpDrive.isDev) {
WarpDrive.logger.info(String.format("onBlockUpdate args %s actual %s",
blockEvent.getState(), blockEvent.getWorld().getBlockState(blockEvent.getPos())));
}
public void onBlockEvent(final BlockEvent blockEvent) {
if ( WarpDriveConfig.isGregtechLoaded
&& blockEvent.getWorld().getWorldInfo().getWorldName().equals("DummyServer") ) {
return;
}
WarpDrive.starMap.onBlockUpdated(blockEvent.getWorld(), blockEvent.getPos(), blockEvent.getState());
ChunkHandler.onBlockUpdated(blockEvent.getWorld(), blockEvent.getPos().getX(), blockEvent.getPos().getY(), blockEvent.getPos().getZ());
final Entity entity;
final IBlockState blockStateBefore;
final IBlockState blockStatePlaced;
if (blockEvent instanceof BlockEvent.EntityPlaceEvent) {
final BlockEvent.EntityPlaceEvent entityPlaceEvent = (BlockEvent.EntityPlaceEvent) blockEvent;
entity = entityPlaceEvent.getEntity();
if (entity instanceof EntityPlayer) {
blockStateBefore = entityPlaceEvent.getBlockSnapshot().getReplacedBlock();
blockStatePlaced = entityPlaceEvent.getPlacedBlock();
} else {
blockStateBefore = entityPlaceEvent.getPlacedAgainst();
blockStatePlaced = entityPlaceEvent.getPlacedBlock();
}
} else if (blockEvent instanceof BlockEvent.BreakEvent) {
entity = ((BlockEvent.BreakEvent) blockEvent).getPlayer();
blockStateBefore = blockEvent.getWorld().getBlockState(blockEvent.getPos());
blockStatePlaced = blockEvent.getState();
} else {
entity = null;
blockStateBefore = blockEvent.getWorld().getBlockState(blockEvent.getPos());
blockStatePlaced = blockEvent.getState();
}
if (WarpDriveConfig.LOGGING_BREAK_PLACE && WarpDrive.isDev) {
if (blockStateBefore != blockStatePlaced) {
WarpDrive.logger.info(String.format("onBlockEvent %s %s -> %s %s by %s",
blockEvent.getClass().getSimpleName(),
blockStateBefore,
blockStatePlaced,
Commons.format(blockEvent.getWorld(), blockEvent.getPos()),
entity ));
} else {
WarpDrive.logger.info(String.format("onBlockEvent %s %s %s by %s",
blockEvent.getClass().getSimpleName(),
blockStatePlaced,
Commons.format(blockEvent.getWorld(), blockEvent.getPos()),
entity));
}
}
boolean isAllowed = true;
if ( blockEvent instanceof BlockEvent.MultiPlaceEvent
|| blockEvent instanceof BlockEvent.EntityMultiPlaceEvent ) {
final List<BlockSnapshot> listBlockSnapshots = blockEvent instanceof BlockEvent.MultiPlaceEvent
? ((BlockEvent.MultiPlaceEvent) blockEvent).getReplacedBlockSnapshots()
: ((BlockEvent.EntityMultiPlaceEvent) blockEvent).getReplacedBlockSnapshots();
for (final BlockSnapshot blockSnapshot : listBlockSnapshots) {
final IBlockState blockStateCurrent = blockSnapshot.getCurrentBlock();
isAllowed = isAllowed && WarpDrive.starMap.onBlockUpdating(entity, blockEvent.getWorld(), blockSnapshot.getPos(), blockStateCurrent);
if (blockStateCurrent != blockSnapshot.getReplacedBlock()) {
isAllowed = isAllowed && WarpDrive.starMap.onBlockUpdating(entity, blockEvent.getWorld(), blockSnapshot.getPos(), blockSnapshot.getReplacedBlock());
}
}
} else if (blockEvent instanceof BlockEvent.PortalSpawnEvent) {
isAllowed = isAllowed && CelestialObjectManager.onOpeningNetherPortal(blockEvent.getWorld(), blockEvent.getPos());
} else {
isAllowed = isAllowed && WarpDrive.starMap.onBlockUpdating(entity, blockEvent.getWorld(), blockEvent.getPos(), blockEvent.getState());
}
if (!isAllowed) {
blockEvent.setCanceled(true);
return;
}
if ( blockEvent instanceof BlockEvent.MultiPlaceEvent
|| blockEvent instanceof BlockEvent.EntityMultiPlaceEvent ) {
final List<BlockSnapshot> listBlockSnapshots = blockEvent instanceof BlockEvent.MultiPlaceEvent
? ((BlockEvent.MultiPlaceEvent) blockEvent).getReplacedBlockSnapshots()
: ((BlockEvent.EntityMultiPlaceEvent) blockEvent).getReplacedBlockSnapshots();
for (final BlockSnapshot blockSnapshot : listBlockSnapshots) {
ChunkHandler.onBlockUpdated(blockEvent.getWorld(), blockSnapshot.getPos());
}
} else {
ChunkHandler.onBlockUpdated(blockEvent.getWorld(), blockEvent.getPos());
}
}
}