NaturesAura/src/main/java/de/ellpeck/naturesaura/items/ItemRangeVisualizer.java

97 lines
4.3 KiB
Java
Raw Normal View History

2019-01-27 13:57:34 +01:00
package de.ellpeck.naturesaura.items;
2019-03-20 20:51:24 +01:00
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.render.IVisualizable;
2021-12-15 16:24:53 +01:00
import net.minecraft.core.BlockPos;
2022-06-27 15:24:04 +02:00
import net.minecraft.network.chat.Component;
2021-12-15 16:24:53 +01:00
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
2019-10-20 22:30:49 +02:00
import net.minecraftforge.eventbus.api.SubscribeEvent;
2019-01-27 13:57:34 +01:00
2020-01-26 01:41:49 +01:00
public class ItemRangeVisualizer extends ItemImpl {
2019-01-27 13:57:34 +01:00
2020-09-22 15:38:58 +02:00
public static final ListMultimap<ResourceLocation, BlockPos> VISUALIZED_BLOCKS = ArrayListMultimap.create();
public static final ListMultimap<ResourceLocation, Entity> VISUALIZED_ENTITIES = ArrayListMultimap.create();
public static final ListMultimap<ResourceLocation, BlockPos> VISUALIZED_RAILS = ArrayListMultimap.create();
2019-01-27 13:57:34 +01:00
2020-01-26 01:41:49 +01:00
public ItemRangeVisualizer() {
2021-12-15 16:24:53 +01:00
super("range_visualizer", new Properties().stacksTo(1));
MinecraftForge.EVENT_BUS.register(new EventHandler());
2019-01-27 13:57:34 +01:00
}
2020-02-07 15:22:30 +01:00
public static void clear() {
2022-06-27 15:24:04 +02:00
if (!ItemRangeVisualizer.VISUALIZED_BLOCKS.isEmpty())
ItemRangeVisualizer.VISUALIZED_BLOCKS.clear();
if (!ItemRangeVisualizer.VISUALIZED_ENTITIES.isEmpty())
ItemRangeVisualizer.VISUALIZED_ENTITIES.clear();
if (!ItemRangeVisualizer.VISUALIZED_RAILS.isEmpty())
ItemRangeVisualizer.VISUALIZED_RAILS.clear();
2020-02-07 15:22:30 +01:00
}
2021-12-04 15:40:09 +01:00
public static <T> void visualize(Player player, ListMultimap<ResourceLocation, T> map, ResourceLocation dim, T value) {
2020-02-07 15:22:30 +01:00
if (map.containsEntry(dim, value)) {
map.remove(dim, value);
2022-06-27 15:24:04 +02:00
player.displayClientMessage(Component.translatable("info." + NaturesAura.MOD_ID + ".range_visualizer.end"), true);
2020-02-07 15:22:30 +01:00
} else {
map.put(dim, value);
2022-06-27 15:24:04 +02:00
player.displayClientMessage(Component.translatable("info." + NaturesAura.MOD_ID + ".range_visualizer.start"), true);
2020-02-07 15:22:30 +01:00
}
}
@Override
2021-12-15 16:24:53 +01:00
public InteractionResultHolder<ItemStack> use(Level levelIn, Player playerIn, InteractionHand handIn) {
2021-12-15 16:30:22 +01:00
var stack = playerIn.getItemInHand(handIn);
if (playerIn.isShiftKeyDown()) {
2022-06-27 15:24:04 +02:00
ItemRangeVisualizer.clear();
playerIn.displayClientMessage(Component.translatable("info." + NaturesAura.MOD_ID + ".range_visualizer.end_all"), true);
2021-12-15 16:24:53 +01:00
return new InteractionResultHolder<>(InteractionResult.SUCCESS, stack);
}
2021-12-15 16:24:53 +01:00
return new InteractionResultHolder<>(InteractionResult.PASS, stack);
}
2019-01-27 13:57:34 +01:00
@Override
2021-12-15 16:24:53 +01:00
public InteractionResult useOn(UseOnContext context) {
2021-12-15 16:30:22 +01:00
var level = context.getLevel();
var pos = context.getClickedPos();
var state = level.getBlockState(pos);
var block = state.getBlock();
if (block instanceof IVisualizable) {
2021-12-04 15:40:09 +01:00
if (level.isClientSide)
2022-06-27 15:24:04 +02:00
ItemRangeVisualizer.visualize(context.getPlayer(), ItemRangeVisualizer.VISUALIZED_BLOCKS, level.dimension().location(), pos);
2021-12-04 15:40:09 +01:00
return InteractionResult.SUCCESS;
2019-01-27 13:57:34 +01:00
}
2021-12-04 15:40:09 +01:00
return InteractionResult.PASS;
2019-01-27 13:57:34 +01:00
}
public class EventHandler {
@SubscribeEvent
public void onInteract(PlayerInteractEvent.EntityInteractSpecific event) {
2021-12-15 16:30:22 +01:00
var stack = event.getItemStack();
2020-01-26 01:41:49 +01:00
if (stack.isEmpty() || stack.getItem() != ItemRangeVisualizer.this)
return;
2021-12-15 16:30:22 +01:00
var entity = event.getTarget();
if (entity instanceof IVisualizable) {
2021-12-04 15:40:09 +01:00
if (entity.level.isClientSide) {
2021-12-15 16:30:22 +01:00
var dim = entity.level.dimension().location();
ItemRangeVisualizer.visualize(event.getEntity(), ItemRangeVisualizer.VISUALIZED_ENTITIES, dim, entity);
}
event.getEntity().swing(event.getHand());
2021-12-04 15:40:09 +01:00
event.setCancellationResult(InteractionResult.SUCCESS);
event.setCanceled(true);
}
}
}
2019-01-27 13:57:34 +01:00
}