Skip to content

Commit

Permalink
Add the flower pot modify event (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 authored Feb 10, 2024
1 parent 398c7f3 commit 54339bb
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
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);
}
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);
}
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/stimuli.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"block.DispenserBlockMixin",
"block.DropperBlockMixin",
"block.FarmlandBlockMixin",
"block.FlowerPotBlockMixin",
"block.TurtleEggBlockMixin",
"entity.LivingEntityMixin",
"entity.ShearableEntityMixin",
Expand Down

0 comments on commit 54339bb

Please sign in to comment.