Skip to content

Commit

Permalink
chore: merge upstream changes
Browse files Browse the repository at this point in the history
Small changes to replace certain Redirects and Overwrites with
WrapOperations and Shared local references. This brings better
mod compatibility moving forward.
  • Loading branch information
gabizou committed Jan 19, 2025
2 parents 68d8ce7 + 680b7f5 commit 72bf34f
Show file tree
Hide file tree
Showing 26 changed files with 962 additions and 705 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
@@ -0,0 +1,85 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.forge.mixin.core.minecraftforge.event.entity;

import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.entity.item.ItemEvent;
import net.minecraftforge.event.entity.item.ItemTossEvent;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.event.EventContextKeys;
import org.spongepowered.api.event.SpongeEventFactory;
import org.spongepowered.api.event.cause.entity.SpawnTypes;
import org.spongepowered.api.event.item.inventory.DropItemEvent;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.event.tracking.PhaseTracker;
import org.spongepowered.forge.launch.bridge.event.ForgeEventBridge_Forge;

import java.util.ArrayList;

@Mixin(ItemTossEvent.class)
public abstract class ItemTossEventMixin_Forge extends ItemEvent implements ForgeEventBridge_Forge<DropItemEvent.Dispense> {

//@formatter:off
@Shadow private @Final Player player;
//@formatter:on

private ItemTossEventMixin_Forge(ItemEntity itemEntity) {
super(itemEntity);
}

@Override
public void bridge$syncFrom(DropItemEvent.Dispense event) {
event.entities().stream()
.filter(e -> e instanceof ItemEntity)
.map(e -> (ItemEntity) e)
.findFirst()
.ifPresent(e -> this.getEntity().setItem(e.getItem()));
this.setCanceled(event.isCancelled());
}

@Override
public void bridge$syncTo(DropItemEvent.Dispense event) {
event.setCancelled(this.isCanceled());
}

@Override
public DropItemEvent.@Nullable Dispense bridge$createSpongeEvent() {
if (this.player.level().isClientSide()) {
return null;
}
try (final var frame = PhaseTracker.getInstance().pushCauseFrame()) {
frame.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.DROPPED_ITEM);
final var cause = frame.currentCause();
final var toSpawn = (Entity) this.getEntity();
final var list = new ArrayList<Entity>();
list.add(toSpawn);
return SpongeEventFactory.createDropItemEventDispense(cause, list);
}
}
}
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
3 changes: 2 additions & 1 deletion forge/src/mixins/resources/mixins.spongeforge.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"commands.CommandsMixin_Forge",
"minecraftforge.MinecraftForgeMixin_Forge",
"minecraftforge.event.entity.EntityTravelToDimensionEventMixin_Forge",
"minecraftforge.event.entity.player.PlayerEvent_PlayerChangedDimensionEventMixin_Forge",
"minecraftforge.event.entity.ItemTossEventMixin_Forge",
"minecraftforge.event.entity.player.PlayerEvent_PlayerChangedDimensionEventMixin_Forge",
"minecraftforge.event.world.BlockEvent_BreakEventMixin_Forge",
"minecraftforge.event.world.BlockEventMixin_Forge",
"minecraftforge.internal.BrandingControlMixin_Forge",
Expand Down
Loading

0 comments on commit 72bf34f

Please sign in to comment.