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

99 lines
4.2 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;
import net.minecraft.network.chat.TranslatableComponent;
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.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
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() {
if (!VISUALIZED_BLOCKS.isEmpty())
VISUALIZED_BLOCKS.clear();
if (!VISUALIZED_ENTITIES.isEmpty())
VISUALIZED_ENTITIES.clear();
if (!VISUALIZED_RAILS.isEmpty())
VISUALIZED_RAILS.clear();
}
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);
2021-12-15 16:24:53 +01:00
player.displayClientMessage(new TranslatableComponent("info." + NaturesAura.MOD_ID + ".range_visualizer.end"), true);
2020-02-07 15:22:30 +01:00
} else {
map.put(dim, value);
2021-12-15 16:24:53 +01:00
player.displayClientMessage(new TranslatableComponent("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) {
ItemStack stack = playerIn.getItemInHand(handIn);
if (playerIn.isCrouching()) {
clear();
2021-12-15 16:24:53 +01:00
playerIn.displayClientMessage(new TranslatableComponent("info." + NaturesAura.MOD_ID + ".range_visualizer.end_all"), true);
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-04 15:40:09 +01:00
Level level = context.getLevel();
2021-12-15 16:24:53 +01:00
BlockPos pos = context.getClickedPos();
2021-12-04 15:40:09 +01:00
BlockState state = level.getBlockState(pos);
2019-01-27 13:57:34 +01:00
Block block = state.getBlock();
if (block instanceof IVisualizable) {
2021-12-04 15:40:09 +01:00
if (level.isClientSide)
2021-12-15 16:24:53 +01:00
visualize(context.getPlayer(), 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) {
ItemStack stack = event.getItemStack();
2020-01-26 01:41:49 +01:00
if (stack.isEmpty() || stack.getItem() != ItemRangeVisualizer.this)
return;
Entity entity = event.getTarget();
if (entity instanceof IVisualizable) {
2021-12-04 15:40:09 +01:00
if (entity.level.isClientSide) {
2021-12-15 16:24:53 +01:00
ResourceLocation dim = entity.level.dimension().location();
visualize(event.getPlayer(), VISUALIZED_ENTITIES, dim, entity);
}
2021-12-15 16:24:53 +01:00
event.getPlayer().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
}