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;
|
2019-03-21 22:51:10 +01:00
|
|
|
import de.ellpeck.naturesaura.NaturesAura;
|
2019-01-28 15:43:21 +01:00
|
|
|
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;
|
2019-01-28 15:43:21 +01:00
|
|
|
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));
|
2020-01-24 20:49:09 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 22:51:10 +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);
|
2021-12-15 16:24:53 +01:00
|
|
|
if (playerIn.isCrouching()) {
|
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);
|
2019-03-21 22:51:10 +01:00
|
|
|
}
|
2021-12-15 16:24:53 +01:00
|
|
|
return new InteractionResultHolder<>(InteractionResult.PASS, stack);
|
2019-03-21 22:51:10 +01:00
|
|
|
}
|
|
|
|
|
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();
|
2019-01-28 15:43:21 +01:00
|
|
|
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
|
|
|
}
|
2019-01-28 15:43:21 +01:00
|
|
|
|
2020-01-24 20:49:09 +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)
|
2020-01-24 20:49:09 +01:00
|
|
|
return;
|
2021-12-15 16:30:22 +01:00
|
|
|
var entity = event.getTarget();
|
2020-01-24 20:49:09 +01:00
|
|
|
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();
|
2022-08-01 16:14:37 +02:00
|
|
|
ItemRangeVisualizer.visualize(event.getEntity(), ItemRangeVisualizer.VISUALIZED_ENTITIES, dim, entity);
|
2020-01-24 20:49:09 +01:00
|
|
|
}
|
2022-08-01 16:14:37 +02:00
|
|
|
event.getEntity().swing(event.getHand());
|
2021-12-04 15:40:09 +01:00
|
|
|
event.setCancellationResult(InteractionResult.SUCCESS);
|
2020-01-24 20:49:09 +01:00
|
|
|
event.setCanceled(true);
|
2019-01-28 15:43:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-27 13:57:34 +01:00
|
|
|
}
|