-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the flower pot modify event (#47)
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/java/xyz/nucleoid/stimuli/event/block/FlowerPotModifyEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package xyz.nucleoid.stimuli.event.block; | ||
|
||
import net.minecraft.block.FlowerPotBlock; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.util.ActionResult; | ||
import net.minecraft.util.Hand; | ||
import net.minecraft.util.hit.BlockHitResult; | ||
import xyz.nucleoid.stimuli.event.StimulusEvent; | ||
|
||
/** | ||
* Called when a {@link FlowerPotBlock} is interacted with in a way that would cause the contents of the flower pot to change. | ||
* An empty stack in the given hand represents a flower pot being emptied, while a non-empty stack represents a flower being potted. | ||
* | ||
* <p>Upon return: | ||
* <ul> | ||
* <li>{@link ActionResult#SUCCESS} cancels further processing and allows the flower pot to be modified. | ||
* <li>{@link ActionResult#FAIL} cancels further processing and cancels the modification. | ||
* <li>{@link ActionResult#PASS} moves on to the next listener.</ul> | ||
* <p> | ||
* If all listeners return {@link ActionResult#PASS}, the modification succeeds and proceeds with normal logic. | ||
*/ | ||
public interface FlowerPotModifyEvent { | ||
StimulusEvent<FlowerPotModifyEvent> EVENT = StimulusEvent.create(FlowerPotModifyEvent.class, ctx -> (player, hand, hitResult) -> { | ||
try { | ||
for (var listener : ctx.getListeners()) { | ||
var result = listener.onModifyFlowerPot(player, hand, hitResult); | ||
if (result != ActionResult.PASS) { | ||
return result; | ||
} | ||
} | ||
} catch (Throwable t) { | ||
ctx.handleException(t); | ||
} | ||
return ActionResult.PASS; | ||
}); | ||
|
||
ActionResult onModifyFlowerPot(ServerPlayerEntity player, Hand hand, BlockHitResult hitResult); | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/xyz/nucleoid/stimuli/mixin/block/FlowerPotBlockMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package xyz.nucleoid.stimuli.mixin.block; | ||
|
||
import net.minecraft.block.BlockState; | ||
import net.minecraft.block.FlowerPotBlock; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.util.ActionResult; | ||
import net.minecraft.util.Hand; | ||
import net.minecraft.util.hit.BlockHitResult; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.world.World; | ||
import org.objectweb.asm.Opcodes; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
import xyz.nucleoid.stimuli.Stimuli; | ||
import xyz.nucleoid.stimuli.event.block.FlowerPotModifyEvent; | ||
|
||
@Mixin(FlowerPotBlock.class) | ||
public class FlowerPotBlockMixin { | ||
// After 'if (noPottedBlock != this.isEmpty()) {' | ||
@Inject(method = "onUse", at = @At(value = "JUMP", opcode = Opcodes.IF_ICMPEQ, ordinal = 0, shift = At.Shift.AFTER), cancellable = true) | ||
private void onModifyFlowerPot(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hitResult, CallbackInfoReturnable<ActionResult> ci) { | ||
if (!(player instanceof ServerPlayerEntity serverPlayer)) { | ||
return; | ||
} | ||
|
||
try (var invokers = Stimuli.select().forEntityAt(serverPlayer, pos)) { | ||
var result = invokers.get(FlowerPotModifyEvent.EVENT).onModifyFlowerPot(serverPlayer, hand, hitResult); | ||
|
||
if (result == ActionResult.FAIL) { | ||
// notify the client that this action did not go through | ||
int slot = hand == Hand.MAIN_HAND ? serverPlayer.getInventory().selectedSlot : 40; | ||
var stack = serverPlayer.getStackInHand(hand); | ||
serverPlayer.networkHandler.sendPacket(new ScreenHandlerSlotUpdateS2CPacket(ScreenHandlerSlotUpdateS2CPacket.UPDATE_PLAYER_INVENTORY_SYNC_ID, 0, slot, stack)); | ||
|
||
ci.setReturnValue(ActionResult.CONSUME); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters