-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
111 additions
and
35 deletions.
There are no files selected for viewing
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
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
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
39 changes: 39 additions & 0 deletions
39
src/main/java/fr/hugman/build_rush/mixin/ItemStackMixin.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,39 @@ | ||
package fr.hugman.build_rush.mixin; | ||
|
||
import fr.hugman.build_rush.event.UseEvents; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.item.ItemUsageContext; | ||
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 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; | ||
|
||
@Mixin(ItemStack.class) | ||
public class ItemStackMixin { | ||
@Inject(method = "useOnBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/Item;useOnBlock(Lnet/minecraft/item/ItemUsageContext;)Lnet/minecraft/util/ActionResult;"), cancellable = true) | ||
private void useOnBlock(ItemUsageContext context, CallbackInfoReturnable<ActionResult> cir) { | ||
var world = context.getWorld(); | ||
if(!context.getWorld().isClient()) { | ||
var events = Stimuli.select(); | ||
var player = context.getPlayer(); | ||
var pos = context.getBlockPos(); | ||
try(var invokers = player == null ? events.at(world,pos) : events.forEntityAt(player, pos)) { | ||
var result = invokers.get(UseEvents.ITEM_ON_BLOCK).onItemUsedOnBlock((ItemStack) (Object) this, context); | ||
|
||
if (result == ActionResult.FAIL) { | ||
// notify the client that this action did not go through | ||
int slot = context.getHand() == Hand.MAIN_HAND ? player.getInventory().selectedSlot : 40; | ||
var stack = context.getStack(); | ||
((ServerPlayerEntity)player).networkHandler.sendPacket(new ScreenHandlerSlotUpdateS2CPacket(ScreenHandlerSlotUpdateS2CPacket.UPDATE_PLAYER_INVENTORY_SYNC_ID, 0, slot, stack)); | ||
|
||
cir.setReturnValue(ActionResult.FAIL); | ||
} | ||
} | ||
} | ||
} | ||
} |
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