Skip to content

Commit

Permalink
chore: generify event bridges
Browse files Browse the repository at this point in the history
Since we can assert the event is the same one being generated, we can
implicitly cast via the compiler. Just like DataProcessors do, we have
some liberty to do this with events.
  • Loading branch information
gabizou committed Jan 19, 2025
1 parent 7ed6f4f commit fbaf184
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.event.Event;

public interface ForgeEventBridge_Forge {
public interface ForgeEventBridge_Forge<E extends Event> {

/**
* Syncs the Sponge event to this Forge event
Expand All @@ -39,7 +39,7 @@ public interface ForgeEventBridge_Forge {
*
* @param event The Sponge event
*/
void bridge$syncFrom(Event event);
void bridge$syncFrom(E event);

/**
* Syncs the Forge event to this Sponge event
Expand All @@ -51,11 +51,11 @@ public interface ForgeEventBridge_Forge {
*
* @param event The Sponge event
*/
void bridge$syncTo(Event event);
void bridge$syncTo(E event);

/**
* Creates a Sponge event from this Forge event
*/
@Nullable Event bridge$createSpongeEvent();
@Nullable E bridge$createSpongeEvent();

}
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ public boolean post(final Event event) {

@Override
public boolean post(final Event event, final IEventBusInvokeDispatcher wrapper) {
if (event instanceof ForgeEventBridge_Forge) {
if (event instanceof ForgeEventBridge_Forge<?> forgeEvent) {
// intercept!
final ForgeEventBridge_Forge forgeEvent = (ForgeEventBridge_Forge) event;
final org.spongepowered.api.event.@Nullable Event spongeEvent = forgeEvent.bridge$createSpongeEvent();
if (spongeEvent != null) {
return this.postDualBus(spongeEvent, Collections.singleton(event), wrapper);
Expand Down Expand Up @@ -150,8 +149,10 @@ public boolean post(final org.spongepowered.api.event.Event event) {

// Implementation

private boolean postDualBus(final org.spongepowered.api.event.Event spongeEvent, final Collection<? extends Event> forgeEvents,
final IEventBusInvokeDispatcher dispatcher) {
@SuppressWarnings({"unchecked", "rawtypes"})
private boolean postDualBus(
final org.spongepowered.api.event.Event spongeEvent, final Collection<? extends Event> forgeEvents,
final IEventBusInvokeDispatcher dispatcher) {
try (final NoExceptionClosable ignored = this.preparePost(spongeEvent)) {
final RegisteredListener.Cache listeners = this.getHandlerCache(spongeEvent);
final List<RegisteredListener<?>> beforeModifications = listeners.beforeModifications();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import net.minecraftforge.event.entity.EntityEvent;
import net.minecraftforge.event.entity.EntityTravelToDimensionEvent;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.event.Event;
import org.spongepowered.api.event.SpongeEventFactory;
import org.spongepowered.api.event.entity.ChangeEntityWorldEvent;
import org.spongepowered.asm.mixin.Final;
Expand All @@ -43,30 +42,30 @@
import org.spongepowered.forge.launch.bridge.event.ForgeEventBridge_Forge;

@Mixin(value = EntityTravelToDimensionEvent.class, remap = false)
public abstract class EntityTravelToDimensionEventMixin_Forge implements ForgeEventBridge_Forge {
public abstract class EntityTravelToDimensionEventMixin_Forge extends EntityEvent implements ForgeEventBridge_Forge<ChangeEntityWorldEvent.Pre> {

// @formatter:off
@Shadow @Final @Mutable private ResourceKey<Level> dimension;
// @formatter:on

private EntityTravelToDimensionEventMixin_Forge(Entity entity) {
super(entity);
}

@Override
public void bridge$syncFrom(final Event event) {
if (event instanceof ChangeEntityWorldEvent.Pre) {
((net.minecraftforge.eventbus.api.Event) (Object) this).setCanceled(((ChangeEntityWorldEvent.Pre) event).isCancelled());
this.dimension = ((ServerLevel) ((ChangeEntityWorldEvent.Pre) event).destinationWorld()).dimension();
}
public void bridge$syncFrom(final ChangeEntityWorldEvent.Pre event) {
this.setCanceled(event.isCancelled());
this.dimension = ((ServerLevel) event.destinationWorld()).dimension();
}

@Override
public void bridge$syncTo(final Event event) {
if (event instanceof ChangeEntityWorldEvent.Pre) {
((ChangeEntityWorldEvent.Pre) event).setCancelled(((net.minecraftforge.eventbus.api.Event) (Object) this).isCanceled());
}
public void bridge$syncTo(final ChangeEntityWorldEvent.Pre event) {
event.setCancelled(this.isCanceled());
}

@Override
public @Nullable Event bridge$createSpongeEvent() {
final Entity entity = ((EntityEvent) (Object) this).getEntity();
public ChangeEntityWorldEvent.@Nullable Pre bridge$createSpongeEvent() {
final Entity entity = this.getEntity();
final ServerLevel toWorld = SpongeCommon.server().getLevel(this.dimension);
return SpongeEventFactory.createChangeEntityWorldEventPre(PhaseTracker.getCauseStackManager().currentCause(),
(org.spongepowered.api.entity.Entity) entity, (org.spongepowered.api.world.server.ServerWorld) entity.getCommandSenderWorld(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import net.minecraft.world.level.Level;
import net.minecraftforge.event.entity.player.PlayerEvent;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.event.Event;
import org.spongepowered.api.event.SpongeEventFactory;
import org.spongepowered.api.event.entity.ChangeEntityWorldEvent;
import org.spongepowered.api.world.server.ServerWorld;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -39,26 +39,26 @@
import org.spongepowered.forge.launch.bridge.event.ForgeEventBridge_Forge;

@Mixin(value = PlayerEvent.PlayerChangedDimensionEvent.class, remap = false)
public final class PlayerEvent_PlayerChangedDimensionEventMixin_Forge implements ForgeEventBridge_Forge {
public final class PlayerEvent_PlayerChangedDimensionEventMixin_Forge implements ForgeEventBridge_Forge<ChangeEntityWorldEvent.Post>{

// @formatter:off
@Shadow @Final private ResourceKey<Level> fromDim;
@Shadow @Final private ResourceKey<Level> toDim;
// @formatter:on

@Override
public void bridge$syncFrom(final Event event) {
public void bridge$syncFrom(final ChangeEntityWorldEvent.Post event) {
// nothing to do -- informational only
}

@Override
public void bridge$syncTo(final Event event) {
public void bridge$syncTo(final ChangeEntityWorldEvent.Post event) {
// nothing to do -- informational only
}

@SuppressWarnings("ConstantConditions")
@Override
public Event bridge$createSpongeEvent() {
public ChangeEntityWorldEvent.Post bridge$createSpongeEvent() {
final PlayerEvent.PlayerChangedDimensionEvent thisEvent = (PlayerEvent.PlayerChangedDimensionEvent) (Object) this;
return SpongeEventFactory.createChangeEntityWorldEventPost(
PhaseTracker.getCauseStackManager().currentCause(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.event.level.BlockEvent;
import net.minecraftforge.eventbus.api.Event;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(value = BlockEvent.class, remap = false)
public abstract class BlockEventMixin_Forge {
public abstract class BlockEventMixin_Forge extends Event {

// @formatter:off
@Shadow public abstract LevelAccessor shadow$getLevel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.spongepowered.api.block.BlockTypes;
import org.spongepowered.api.block.transaction.BlockTransaction;
import org.spongepowered.api.block.transaction.Operations;
import org.spongepowered.api.event.Event;
import org.spongepowered.api.event.SpongeEventFactory;
import org.spongepowered.api.event.block.ChangeBlockEvent;
import org.spongepowered.api.world.server.ServerWorld;
Expand All @@ -45,37 +44,33 @@
import java.util.Collections;

@Mixin(value = BlockEvent.BreakEvent.class, remap = false)
public abstract class BlockEvent_BreakEventMixin_Forge extends BlockEventMixin_Forge implements ForgeEventBridge_Forge {
public abstract class BlockEvent_BreakEventMixin_Forge extends BlockEventMixin_Forge implements ForgeEventBridge_Forge<ChangeBlockEvent.All> {

@Override
public void bridge$syncFrom(final Event event) {
if (event instanceof ChangeBlockEvent.All) {
final ChangeBlockEvent.All changeBlockEventAll = (ChangeBlockEvent.All) event;
final Vector3i pos = VecHelper.toVector3i(this.shadow$getPos());
if (changeBlockEventAll.isCancelled() ||
changeBlockEventAll.transactions()
.stream()
.filter(x -> x.original().position().equals(pos))
.anyMatch(x -> !x.isValid() || x.operation() != Operations.BREAK.get() || x.custom().isPresent())) {
((net.minecraftforge.eventbus.api.Event) (Object) this).setCanceled(true);
}
public void bridge$syncFrom(final ChangeBlockEvent.All event) {
final Vector3i pos = VecHelper.toVector3i(this.shadow$getPos());
if (event.isCancelled() ||
event.transactions()
.stream()
.filter(x -> x.original().position().equals(pos))
.anyMatch(x -> !x.isValid() || x.operation() != Operations.BREAK.get() || x.custom().isPresent())) {
this.setCanceled(true);
}
}

@Override
public void bridge$syncTo(final Event event) {
if (event instanceof ChangeBlockEvent.All && ((net.minecraftforge.eventbus.api.Event) (Object) this).isCanceled()) {
public void bridge$syncTo(final ChangeBlockEvent.All event) {
if (this.isCanceled()) {
final Vector3i pos = VecHelper.toVector3i(this.shadow$getPos());
((ChangeBlockEvent.All) event).transactions(Operations.BREAK.get()).filter(x -> x.original().position().equals(pos))
event.transactions(Operations.BREAK.get()).filter(x -> x.original().position().equals(pos))
.forEach(x -> x.setValid(false));
}
}

@Override
public @Nullable Event bridge$createSpongeEvent() {
public ChangeBlockEvent.@Nullable All bridge$createSpongeEvent() {
final LevelAccessor accessor = this.shadow$getLevel();
if (accessor instanceof ServerWorld) {
final ServerWorld serverWorld = (ServerWorld) accessor;
if (accessor instanceof ServerWorld serverWorld) {
final BlockTransaction transaction = new BlockTransaction(
SpongeBlockSnapshot.BuilderImpl
.pooled()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.event.Event;

public interface NeoEventBridge_Neo {
public interface NeoEventBridge_Neo<E extends Event> {

/**
* Syncs the Sponge event to this Forge event
Expand All @@ -39,7 +39,7 @@ public interface NeoEventBridge_Neo {
*
* @param event The Sponge event
*/
void bridge$syncFrom(Event event);
void bridge$syncFrom(E event);

/**
* Syncs the Forge event to this Sponge event
Expand All @@ -51,11 +51,11 @@ public interface NeoEventBridge_Neo {
*
* @param event The Sponge event
*/
void bridge$syncTo(Event event);
void bridge$syncTo(E event);

/**
* Creates a Sponge event from this Forge event
*/
@Nullable Event bridge$createSpongeEvent();
@Nullable E bridge$createSpongeEvent();

}
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,9 @@ public void unregister(final Object object) {

@Override
public <T extends Event> T post(T event) {
if (event instanceof NeoEventBridge_Neo) {
if (event instanceof NeoEventBridge_Neo bridgeEvent) {
// intercept!
final NeoEventBridge_Neo forgeEvent = (NeoEventBridge_Neo) event;
final org.spongepowered.api.event.@Nullable Event spongeEvent = forgeEvent.bridge$createSpongeEvent();
final org.spongepowered.api.event.@Nullable Event spongeEvent = bridgeEvent.bridge$createSpongeEvent();
if (spongeEvent != null) {
this.postDualBus(spongeEvent, Collections.singleton(event));
return event;
Expand Down Expand Up @@ -141,6 +140,7 @@ public boolean post(final org.spongepowered.api.event.Event event) {

// Implementation

@SuppressWarnings({"unchecked", "rawtypes"})
private boolean postDualBus(final org.spongepowered.api.event.Event spongeEvent, final Collection<? extends Event> forgeEvents) {
try (final NoExceptionClosable ignored = this.preparePost(spongeEvent)) {
final RegisteredListener.Cache listeners = this.getHandlerCache(spongeEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import net.neoforged.neoforge.event.entity.EntityEvent;
import net.neoforged.neoforge.event.entity.EntityTravelToDimensionEvent;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.event.Event;
import org.spongepowered.api.event.SpongeEventFactory;
import org.spongepowered.api.event.entity.ChangeEntityWorldEvent;
import org.spongepowered.asm.mixin.Final;
Expand All @@ -43,30 +42,30 @@
import org.spongepowered.common.event.tracking.PhaseTracker;
import org.spongepowered.neoforge.launch.bridge.event.NeoEventBridge_Neo;

@Mixin(value = EntityTravelToDimensionEvent.class, remap = false)
public abstract class EntityTravelToDimensionEventMixin_Neo implements NeoEventBridge_Neo {
@Mixin(value = EntityTravelToDimensionEvent.class)
public abstract class EntityTravelToDimensionEventMixin_Neo implements NeoEventBridge_Neo<ChangeEntityWorldEvent.Pre>, ICancellableEvent {

// @formatter:off
@Shadow @Final @Mutable private ResourceKey<Level> dimension;
// @formatter:on

@Override
public void bridge$syncFrom(final Event event) {
public void bridge$syncFrom(final ChangeEntityWorldEvent.Pre event) {
this.setCanceled(event.isCancelled());
this.dimension = ((ServerLevel) event.destinationWorld()).dimension();
if (event instanceof ChangeEntityWorldEvent.Pre) {
((ICancellableEvent) this).setCanceled(((ChangeEntityWorldEvent.Pre) event).isCancelled());
this.dimension = ((ServerLevel) ((ChangeEntityWorldEvent.Pre) event).destinationWorld()).dimension();
}
}

@Override
public void bridge$syncTo(final Event event) {
public void bridge$syncTo(final ChangeEntityWorldEvent.Pre event) {
if (event instanceof ChangeEntityWorldEvent.Pre) {
((ChangeEntityWorldEvent.Pre) event).setCancelled(((ICancellableEvent) this).isCanceled());
event.setCancelled(this.isCanceled());
}
}

@Override
public @Nullable Event bridge$createSpongeEvent() {
public ChangeEntityWorldEvent.@Nullable Pre bridge$createSpongeEvent() {
final Entity entity = ((EntityEvent) (Object) this).getEntity();
final ServerLevel toWorld = SpongeCommon.server().getLevel(this.dimension);
return SpongeEventFactory.createChangeEntityWorldEventPre(PhaseTracker.getCauseStackManager().currentCause(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import org.spongepowered.neoforge.launch.bridge.event.NeoEventBridge_Neo;

@Mixin(value = PlayerEvent.PlayerChangedDimensionEvent.class, remap = false)
public final class PlayerEvent_PlayerChangedDimensionEventMixin_Neo implements NeoEventBridge_Neo {
public final class PlayerEvent_PlayerChangedDimensionEventMixin_Neo implements NeoEventBridge_Neo {

// @formatter:off
@Shadow @Final private ResourceKey<Level> fromDim;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.spongepowered.api.block.BlockTypes;
import org.spongepowered.api.block.transaction.BlockTransaction;
import org.spongepowered.api.block.transaction.Operations;
import org.spongepowered.api.event.Event;
import org.spongepowered.api.event.SpongeEventFactory;
import org.spongepowered.api.event.block.ChangeBlockEvent;
import org.spongepowered.api.world.server.ServerWorld;
Expand All @@ -46,37 +45,34 @@
import java.util.Collections;

@Mixin(value = BlockEvent.BreakEvent.class, remap = false)
public abstract class BlockEvent_BreakEventMixin_Neo extends BlockEventMixin_Neo implements NeoEventBridge_Neo {
public abstract class BlockEvent_BreakEventMixin_Neo extends BlockEventMixin_Neo implements NeoEventBridge_Neo<ChangeBlockEvent.All>, ICancellableEvent {

@Override
public void bridge$syncFrom(final Event event) {
if (event instanceof ChangeBlockEvent.All) {
final ChangeBlockEvent.All changeBlockEventAll = (ChangeBlockEvent.All) event;
final Vector3i pos = VecHelper.toVector3i(this.shadow$getPos());
if (changeBlockEventAll.isCancelled() ||
changeBlockEventAll.transactions()
.stream()
.filter(x -> x.original().position().equals(pos))
.anyMatch(x -> !x.isValid() || x.operation() != Operations.BREAK.get() || x.custom().isPresent())) {
((ICancellableEvent) this).setCanceled(true);
}
public void bridge$syncFrom(final ChangeBlockEvent.All event) {
final Vector3i pos = VecHelper.toVector3i(this.shadow$getPos());
if (event.isCancelled() ||
event.transactions()
.stream()
.filter(x -> x.original().position().equals(pos))
.anyMatch(x -> !x.isValid() || x.operation() != Operations.BREAK.get() || x.custom().isPresent())) {
this.setCanceled(true);
}
}

@Override
public void bridge$syncTo(final Event event) {
if (event instanceof ChangeBlockEvent.All && ((ICancellableEvent) this).isCanceled()) {
public void bridge$syncTo(final ChangeBlockEvent.All event) {
if (this.isCanceled()) {
final Vector3i pos = VecHelper.toVector3i(this.shadow$getPos());
((ChangeBlockEvent.All) event).transactions(Operations.BREAK.get()).filter(x -> x.original().position().equals(pos))
.forEach(x -> x.setValid(false));
event.transactions(Operations.BREAK.get())
.filter(x -> x.original().position().equals(pos))
.forEach(x -> x.setValid(false));
}
}

@Override
public @Nullable Event bridge$createSpongeEvent() {
public ChangeBlockEvent.@Nullable All bridge$createSpongeEvent() {
final LevelAccessor accessor = this.shadow$getLevel();
if (accessor instanceof ServerWorld) {
final ServerWorld serverWorld = (ServerWorld) accessor;
if (accessor instanceof ServerWorld serverWorld) {
final BlockTransaction transaction = new BlockTransaction(
SpongeBlockSnapshot.BuilderImpl
.pooled()
Expand Down
Loading

0 comments on commit fbaf184

Please sign in to comment.