|
| 1 | +package cn.taskeren.minequery.callback; |
| 2 | + |
| 3 | +import cn.taskeren.minequery.config.MineQueryConfig; |
| 4 | +import net.fabricmc.fabric.api.client.item.v1.ItemTooltipCallback; |
| 5 | +import net.fabricmc.fabric.api.event.player.UseBlockCallback; |
| 6 | +import net.minecraft.client.item.TooltipContext; |
| 7 | +import net.minecraft.entity.player.PlayerEntity; |
| 8 | +import net.minecraft.item.BlockItem; |
| 9 | +import net.minecraft.item.ItemStack; |
| 10 | +import net.minecraft.item.Items; |
| 11 | +import net.minecraft.text.Text; |
| 12 | +import net.minecraft.text.TranslatableText; |
| 13 | +import net.minecraft.util.ActionResult; |
| 14 | +import net.minecraft.util.Hand; |
| 15 | +import net.minecraft.util.hit.BlockHitResult; |
| 16 | +import net.minecraft.util.math.Direction; |
| 17 | +import net.minecraft.world.World; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static cn.taskeren.minequery.MineQueryMod.config; |
| 22 | + |
| 23 | +public class NotPlace implements UseBlockCallback, ItemTooltipCallback { |
| 24 | + |
| 25 | + public static final NotPlace INSTANCE = new NotPlace(); |
| 26 | + |
| 27 | + private NotPlace() {} |
| 28 | + |
| 29 | + private static boolean validateItemStack(ItemStack stack) { |
| 30 | + return stack.isOf(Items.STICK) && stack.getName().getString().equalsIgnoreCase("NotPlace"); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public ActionResult interact(PlayerEntity player, World world, Hand hand, BlockHitResult hitResult) { |
| 35 | + if(hand != Hand.MAIN_HAND || world.isClient) { |
| 36 | + return ActionResult.PASS; |
| 37 | + } |
| 38 | + |
| 39 | + MineQueryConfig.NotPlaceConfig opt = config().notPlaceConfig; |
| 40 | + |
| 41 | + ItemStack is = player.getStackInHand(hand); |
| 42 | + Direction direction = hitResult.getSide(); |
| 43 | + if(validateItemStack(is) && player.isSneaking()) { |
| 44 | + var flag = opt.toggle(direction); |
| 45 | + player.sendMessage(new TranslatableText("text.minequery.notplace.toggle."+(flag ? "prevented" : "allowed"), direction), false); |
| 46 | + return ActionResult.SUCCESS; |
| 47 | + } |
| 48 | + else if(is.getItem() instanceof BlockItem) { |
| 49 | + if(opt.isPrevented(direction)) { |
| 50 | + return ActionResult.FAIL; |
| 51 | + } |
| 52 | + } |
| 53 | + return ActionResult.PASS; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void getTooltip(ItemStack stack, TooltipContext context, List<Text> lines) { |
| 58 | + if(validateItemStack(stack)) { |
| 59 | + MineQueryConfig.NotPlaceConfig opt = config().notPlaceConfig; |
| 60 | + for(var direction : Direction.values()) { |
| 61 | + var prv = opt.isPrevented(direction); |
| 62 | + lines.add(new TranslatableText("text.minequery.notplace.tooltip."+(prv ? "prevented" : "allowed"), |
| 63 | + direction.getName())); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments