generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from valoeghese/misaka_mikoto
Block Place Event
- Loading branch information
Showing
7 changed files
with
124 additions
and
4 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
51 changes: 51 additions & 0 deletions
51
src/main/java/io/github/fabriccommunity/events/mixin/item/MixinBlockItem.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,51 @@ | ||
package io.github.fabriccommunity.events.mixin.item; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Redirect; | ||
|
||
import io.github.fabriccommunity.events.play.ItemEvents; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.item.BlockItem; | ||
import net.minecraft.item.ItemPlacementContext; | ||
import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.util.ActionResult; | ||
|
||
@Mixin(BlockItem.class) | ||
public abstract class MixinBlockItem { | ||
@Shadow | ||
protected abstract BlockState getPlacementState(ItemPlacementContext context); | ||
|
||
/** | ||
* @reason getPlacementState is often overriden (has two vanilla overrides, could have more in modded). Therefore using a redirect rather than inject at return in {@code getPlacementState} is more compatible with other mods. | ||
*/ | ||
@Redirect(at = @At(value = "INVOKE", target = "Lnet/minecraft/item/BlockItem;getPlacementState(Lnet/minecraft/item/ItemPlacementContext;)Lnet/minecraft/block/BlockState;"), | ||
method = "place") | ||
private BlockState onGetPlacementState(BlockItem self, ItemPlacementContext placementContext) { | ||
BlockState original = this.getPlacementState(placementContext); | ||
AtomicReference<BlockState> state = new AtomicReference<>(original); | ||
|
||
ActionResult result = ItemEvents.PLACED.invoker().onBlockPlaced(placementContext, original, state); | ||
BlockState modified = state.get(); | ||
|
||
if (result == ActionResult.FAIL) { | ||
PlayerEntity entity = placementContext.getPlayer(); | ||
|
||
if (entity instanceof ServerPlayerEntity) { | ||
int selectedSlot = entity.inventory.selectedSlot; | ||
((ServerPlayerEntity) entity).networkHandler.sendPacket(new ScreenHandlerSlotUpdateS2CPacket(-2, selectedSlot, entity.inventory.getStack(selectedSlot))); | ||
} | ||
|
||
return null; | ||
} else if (result == ActionResult.SUCCESS || (original != modified)) { | ||
return modified; | ||
} else { | ||
return original; | ||
} | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
src/test/java/io/github/fabriccommunity/events/test/BlockPlaceTest.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,29 @@ | ||
package io.github.fabriccommunity.events.test; | ||
|
||
import io.github.fabriccommunity.events.play.ItemEvents; | ||
import net.fabricmc.api.ModInitializer; | ||
import net.minecraft.block.Blocks; | ||
import net.minecraft.util.ActionResult; | ||
|
||
/** | ||
* @author Valoeghese | ||
*/ | ||
public class BlockPlaceTest implements ModInitializer { | ||
public static boolean ENABLED = true; | ||
|
||
@Override | ||
public void onInitialize() { | ||
if (ENABLED) { | ||
ItemEvents.PLACED.register((context, original, state) -> { | ||
if (context.getBlockPos().getY() == 63) { | ||
return ActionResult.FAIL; | ||
} else if (context.getBlockPos().getY() == 64) { | ||
state.set(Blocks.GOLD_BLOCK.getDefaultState()); | ||
return ActionResult.SUCCESS; | ||
} | ||
|
||
return ActionResult.PASS; | ||
}); | ||
} | ||
} | ||
} |
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