diff --git a/src/main/java/org/spongepowered/common/bridge/block/entity/AbstractFurnaceBlockEntityBridge.java b/src/main/java/org/spongepowered/common/bridge/block/entity/AbstractFurnaceBlockEntityBridge.java new file mode 100644 index 00000000000..e16bf8271d1 --- /dev/null +++ b/src/main/java/org/spongepowered/common/bridge/block/entity/AbstractFurnaceBlockEntityBridge.java @@ -0,0 +1,36 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.common.bridge.block.entity; + +import net.minecraft.world.item.crafting.AbstractCookingRecipe; +import net.minecraft.world.item.crafting.RecipeHolder; + +import java.util.Optional; + +public interface AbstractFurnaceBlockEntityBridge { + + Optional> bridge$getCurrentRecipe(); + +} diff --git a/src/mixins/java/org/spongepowered/common/mixin/core/world/level/block/entity/AbstractFurnaceBlockEntityMixin.java b/src/mixins/java/org/spongepowered/common/mixin/core/world/level/block/entity/AbstractFurnaceBlockEntityMixin.java index d48f33717fa..aa6073a701a 100644 --- a/src/mixins/java/org/spongepowered/common/mixin/core/world/level/block/entity/AbstractFurnaceBlockEntityMixin.java +++ b/src/mixins/java/org/spongepowered/common/mixin/core/world/level/block/entity/AbstractFurnaceBlockEntityMixin.java @@ -53,6 +53,7 @@ import org.spongepowered.asm.mixin.injection.Slice; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.common.SpongeCommon; +import org.spongepowered.common.bridge.block.entity.AbstractFurnaceBlockEntityBridge; import org.spongepowered.common.event.tracking.PhaseTracker; import org.spongepowered.common.item.util.ItemStackUtil; @@ -60,7 +61,7 @@ import java.util.Optional; @Mixin(AbstractFurnaceBlockEntity.class) -public abstract class AbstractFurnaceBlockEntityMixin extends BaseContainerBlockEntityMixin { +public abstract class AbstractFurnaceBlockEntityMixin extends BaseContainerBlockEntityMixin implements AbstractFurnaceBlockEntityBridge { // @Formatter:off @Shadow protected NonNullList items; @@ -80,9 +81,9 @@ public abstract class AbstractFurnaceBlockEntityMixin extends BaseContainerBlock final ItemStackSnapshot shrinkedFuel = ItemStackUtil.snapshotOf(ItemStackUtil.cloneDefensive(itemStack, itemStack.getCount() - 1)); final Transaction transaction = new Transaction<>(fuel, shrinkedFuel); - final var recipe = ((AbstractFurnaceBlockEntityMixin) (Object) entity).impl$getCurrentRecipe(); + final var recipe = ((AbstractFurnaceBlockEntityMixin) (Object) entity).bridge$getCurrentRecipe(); final CookingEvent.ConsumeFuel event = SpongeEventFactory.createCookingEventConsumeFuel(cause, (FurnaceBlockEntity) entity, Optional.of(fuel), - Optional.of((CookingRecipe) recipe.value()), Optional.of((ResourceKey) (Object) recipe.id()), Collections.singletonList(transaction)); + recipe.map(r -> (CookingRecipe) r.value()), recipe.map(r -> (ResourceKey) (Object) r.id()), Collections.singletonList(transaction)); SpongeCommon.post(event); if (event.isCancelled()) { ((AbstractFurnaceBlockEntityMixin) (Object) entity).cookingTotalTime = 0; @@ -100,8 +101,9 @@ public abstract class AbstractFurnaceBlockEntityMixin extends BaseContainerBlock } } - private RecipeHolder impl$getCurrentRecipe() { - return this.quickCheck.getRecipeFor((AbstractFurnaceBlockEntity) (Object) this, this.level).orElse(null); + @Override + public Optional> bridge$getCurrentRecipe() { + return this.quickCheck.getRecipeFor((AbstractFurnaceBlockEntity) (Object) this, this.level); } // Interrupt-Active - e.g. a player removing the currently smelting item @@ -144,9 +146,9 @@ public abstract class AbstractFurnaceBlockEntityMixin extends BaseContainerBlock if (this.cookingProgress > 0) { final ItemStackSnapshot fuel = ItemStackUtil.snapshotOf(this.items.get(1)); final Cause cause = PhaseTracker.getCauseStackManager().currentCause(); - final var recipe = this.impl$getCurrentRecipe(); + final var recipe = this.bridge$getCurrentRecipe(); final CookingEvent.Interrupt event = SpongeEventFactory.createCookingEventInterrupt(cause, (FurnaceBlockEntity) this, Optional.of(fuel), - Optional.ofNullable((CookingRecipe) recipe.value()), Optional.of((ResourceKey) (Object) recipe.id())); + recipe.map(r -> (CookingRecipe) r.value()), recipe.map(r -> (ResourceKey) (Object) r.id())); SpongeCommon.post(event); } } diff --git a/vanilla/src/mixins/java/org/spongepowered/vanilla/mixin/core/world/level/block/entity/AbstractFurnaceBlockEntityMixin_Vanilla.java b/vanilla/src/mixins/java/org/spongepowered/vanilla/mixin/core/world/level/block/entity/AbstractFurnaceBlockEntityMixin_Vanilla.java index 703b644dc50..4092d2f26e3 100644 --- a/vanilla/src/mixins/java/org/spongepowered/vanilla/mixin/core/world/level/block/entity/AbstractFurnaceBlockEntityMixin_Vanilla.java +++ b/vanilla/src/mixins/java/org/spongepowered/vanilla/mixin/core/world/level/block/entity/AbstractFurnaceBlockEntityMixin_Vanilla.java @@ -53,6 +53,7 @@ import org.spongepowered.asm.mixin.injection.callback.LocalCapture; import org.spongepowered.common.SpongeCommon; import org.spongepowered.common.accessor.world.level.block.entity.AbstractFurnaceBlockEntityAccessor; +import org.spongepowered.common.bridge.block.entity.AbstractFurnaceBlockEntityBridge; import org.spongepowered.common.event.tracking.PhaseTracker; import org.spongepowered.common.item.util.ItemStackUtil; import org.spongepowered.common.mixin.core.world.level.block.entity.BaseContainerBlockEntityMixin; @@ -61,7 +62,7 @@ import java.util.Optional; @Mixin(AbstractFurnaceBlockEntity.class) -public abstract class AbstractFurnaceBlockEntityMixin_Vanilla extends BaseContainerBlockEntityMixin { +public abstract class AbstractFurnaceBlockEntityMixin_Vanilla extends BaseContainerBlockEntityMixin implements AbstractFurnaceBlockEntityBridge { // @formatter:off @Shadow protected NonNullList items; @@ -69,10 +70,6 @@ public abstract class AbstractFurnaceBlockEntityMixin_Vanilla extends BaseContai @Shadow @Final private RecipeManager.CachedCheck quickCheck; // @formatter:on - private RecipeHolder impl$getCurrentRecipe() { - return this.quickCheck.getRecipeFor((AbstractFurnaceBlockEntity) (Object) this, this.level).orElse(null); - } - // Tick up and Start @Redirect(method = "serverTick", at = @At(value = "INVOKE", @@ -107,10 +104,10 @@ public abstract class AbstractFurnaceBlockEntityMixin_Vanilla extends BaseContai final int clampedCookTime = Mth.clamp(newCookTime, zero, totalCookTime); final ItemStackSnapshot fuel = ItemStackUtil.snapshotOf(((AbstractFurnaceBlockEntityMixin_Vanilla) (Object) entity).items.get(1)); final Cause cause = PhaseTracker.getCauseStackManager().currentCause(); - final var recipe = ((AbstractFurnaceBlockEntityMixin_Vanilla) (Object) entity).impl$getCurrentRecipe(); + final var recipe = ((AbstractFurnaceBlockEntityMixin_Vanilla) (Object) entity).bridge$getCurrentRecipe(); final ItemStackSnapshot cooking = ItemStackUtil.snapshotOf(((AbstractFurnaceBlockEntityMixin_Vanilla) (Object) entity).items.get(0)); final CookingEvent.Tick event = SpongeEventFactory.createCookingEventTick(cause, (FurnaceBlockEntity) entity, cooking, Optional.of(fuel), - Optional.of((CookingRecipe) recipe.value()), Optional.of((ResourceKey) (Object) recipe.id())); + recipe.map(r -> (CookingRecipe) r.value()), recipe.map(r -> (ResourceKey) (Object) r.id())); SpongeCommon.post(event); if (event.isCancelled()) { return ((AbstractFurnaceBlockEntityMixin_Vanilla) (Object) entity).cookingProgress; // dont tick down