use of org.spongepowered.common.bridge.world.level.TrackableBlockEventDataBridge in project SpongeCommon by SpongePowered.
the class ServerLevelMixin_Tracker method tracker$associatePhaseContextDataWithBlockEvent.
@Redirect(method = "blockEvent", at = @At(value = "INVOKE", target = "Lit/unimi/dsi/fastutil/objects/ObjectLinkedOpenHashSet;add(Ljava/lang/Object;)Z", remap = false))
private boolean tracker$associatePhaseContextDataWithBlockEvent(final ObjectLinkedOpenHashSet<BlockEventData> list, final Object data, final BlockPos pos, final Block blockIn, final int eventID, final int eventParam) {
final PhaseContext<@NonNull ?> currentContext = PhaseTracker.getInstance().getPhaseContext();
final BlockEventData blockEventData = (BlockEventData) data;
final TrackableBlockEventDataBridge blockEvent = (TrackableBlockEventDataBridge) blockEventData;
// Short circuit phase states who do not track during block events
if (currentContext.ignoresBlockEvent()) {
return list.add(blockEventData);
}
final BlockState state = this.shadow$getBlockState(pos);
if (((TrackableBridge) blockIn).bridge$allowsBlockEventCreation()) {
blockEvent.bridge$setSourceUserUUID(currentContext.getActiveUserUUID());
if (((BlockStateBridge) state).bridge$hasTileEntity()) {
blockEvent.bridge$setTileEntity((BlockEntity) this.shadow$getBlockEntity(pos));
}
if (blockEvent.bridge$getTileEntity() == null) {
final LocatableBlock locatable = new SpongeLocatableBlockBuilder().world((org.spongepowered.api.world.server.ServerWorld) this).position(pos.getX(), pos.getY(), pos.getZ()).state((org.spongepowered.api.block.BlockState) state).build();
blockEvent.bridge$setTickingLocatable(locatable);
}
}
// allow tracking to take place for other/future phases
if (!((TrackableBridge) blockIn).bridge$allowsBlockEventCreation()) {
return list.add((BlockEventData) data);
}
// In pursuant with our block updates management, we chose to
// effectively allow the block event get added to the list, but
// we log the transaction so that we can call the change block event
// pre, and if needed, undo the add to the list.
currentContext.appendNotifierToBlockEvent(this, pos, blockEvent);
// This is very common with pistons as they add block events while blocks are being notified.
if (ShouldFire.CHANGE_BLOCK_EVENT_PRE) {
if (blockIn instanceof PistonBaseBlock) {
// We only fire pre events for pistons
if (SpongeCommonEventFactory.handlePistonEvent(this, pos, state, eventID)) {
return false;
}
} else {
if (SpongeCommonEventFactory.callChangeBlockEventPre((ServerLevelBridge) this, pos).isCancelled()) {
return false;
}
}
}
currentContext.getTransactor().logBlockEvent(state, this, pos, blockEvent);
return list.add(blockEventData);
}
use of org.spongepowered.common.bridge.world.level.TrackableBlockEventDataBridge in project SpongeCommon by SpongePowered.
the class TrackingUtil method fireMinecraftBlockEvent.
public static boolean fireMinecraftBlockEvent(final ServerLevel worldIn, final BlockEventData event, final net.minecraft.world.level.block.state.BlockState currentState) {
final TrackableBlockEventDataBridge blockEvent = (TrackableBlockEventDataBridge) event;
@Nullable final Object source = blockEvent.bridge$getTileEntity() != null ? blockEvent.bridge$getTileEntity() : blockEvent.bridge$getTickingLocatable();
if (source == null) {
// No source present which means we are ignoring the phase state
return currentState.triggerEvent(worldIn, event.getPos(), event.getParamA(), event.getParamB());
}
final BlockEventTickContext phaseContext = TickPhase.Tick.BLOCK_EVENT.createPhaseContext(PhaseTracker.SERVER);
phaseContext.source(source);
final UUID user = ((TrackableBlockEventDataBridge) event).bridge$getSourceUserUUID();
if (user != null) {
phaseContext.creator = user;
phaseContext.notifier = user;
}
boolean result = true;
try (final BlockEventTickContext o = phaseContext) {
o.buildAndSwitch();
phaseContext.setEventSucceeded(currentState.triggerEvent(worldIn, event.getPos(), event.getParamA(), event.getParamB()));
// We need to grab the result here as the phase context close will trigger a reset
result = phaseContext.wasNotCancelled();
}
// at which point we want to keep track of the return value from the target, and from the block events.
return result;
}
Aggregations