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

99 lines
4.1 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;
2019-01-27 13:57:34 +01:00
import net.minecraft.block.Block;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
2019-10-20 22:30:49 +02:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
2019-11-04 19:08:49 +01:00
import net.minecraft.item.ItemUseContext;
2021-01-14 23:15:02 +01:00
import net.minecraft.util.ActionResult;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.ResourceLocation;
2019-01-27 13:57:34 +01:00
import net.minecraft.util.math.BlockPos;
2019-10-20 22:30:49 +02:00
import net.minecraft.util.text.TranslationTextComponent;
2019-01-27 13:57:34 +01:00
import net.minecraft.world.World;
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() {
2020-02-07 23:50:16 +01:00
super("range_visualizer", new Properties().maxStackSize(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();
}
2020-09-22 15:38:58 +02:00
public static <T> void visualize(PlayerEntity 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);
player.sendStatusMessage(new TranslationTextComponent("info." + NaturesAura.MOD_ID + ".range_visualizer.end"), true);
} else {
map.put(dim, value);
player.sendStatusMessage(new TranslationTextComponent("info." + NaturesAura.MOD_ID + ".range_visualizer.start"), true);
}
}
@Override
2019-10-20 22:30:49 +02:00
public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
ItemStack stack = playerIn.getHeldItem(handIn);
2020-09-22 03:17:02 +02:00
if (playerIn.isSneaking()) {
clear();
2019-10-20 22:30:49 +02:00
playerIn.sendStatusMessage(new TranslationTextComponent("info." + NaturesAura.MOD_ID + ".range_visualizer.end_all"), true);
return new ActionResult<>(ActionResultType.SUCCESS, stack);
}
2019-10-20 22:30:49 +02:00
return new ActionResult<>(ActionResultType.PASS, stack);
}
2019-01-27 13:57:34 +01:00
@Override
2019-11-04 19:08:49 +01:00
public ActionResultType onItemUse(ItemUseContext context) {
World world = context.getWorld();
BlockPos pos = context.getPos();
BlockState state = world.getBlockState(pos);
2019-01-27 13:57:34 +01:00
Block block = state.getBlock();
if (block instanceof IVisualizable) {
2019-11-04 19:08:49 +01:00
if (world.isRemote)
2020-09-22 15:38:58 +02:00
visualize(context.getPlayer(), VISUALIZED_BLOCKS, world.func_234923_W_().func_240901_a_(), pos);
2019-10-20 22:30:49 +02:00
return ActionResultType.SUCCESS;
2019-01-27 13:57:34 +01:00
}
2019-10-20 22:30:49 +02:00
return ActionResultType.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) {
if (entity.world.isRemote) {
2020-09-22 15:38:58 +02:00
ResourceLocation dim = entity.world.func_234923_W_().func_240901_a_();
visualize(event.getPlayer(), VISUALIZED_ENTITIES, dim, entity);
}
event.getPlayer().swingArm(event.getHand());
event.setCancellationResult(ActionResultType.SUCCESS);
event.setCanceled(true);
}
}
}
2019-01-27 13:57:34 +01:00
}