Display Arm Interaction Points

- placed mechanical arms will show their in/out puts if you look at them while holding a wrench
This commit is contained in:
Zelophed 2020-07-03 23:56:50 +02:00
parent 5cf3dde0a1
commit 957f9355b7

View file

@ -6,6 +6,7 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import com.simibubi.create.AllBlocks; import com.simibubi.create.AllBlocks;
import com.simibubi.create.AllItems;
import com.simibubi.create.CreateClient; import com.simibubi.create.CreateClient;
import com.simibubi.create.content.logistics.block.mechanicalArm.ArmInteractionPoint.Mode; import com.simibubi.create.content.logistics.block.mechanicalArm.ArmInteractionPoint.Mode;
import com.simibubi.create.foundation.networking.AllPackets; import com.simibubi.create.foundation.networking.AllPackets;
@ -14,11 +15,15 @@ import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType; import net.minecraft.util.ActionResultType;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.shapes.VoxelShape; import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent; import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber; import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
@ -29,6 +34,8 @@ public class ArmInteractionPointHandler {
static Map<BlockPos, ArmInteractionPoint> currentSelection = new HashMap<>(); static Map<BlockPos, ArmInteractionPoint> currentSelection = new HashMap<>();
static ItemStack currentItem; static ItemStack currentItem;
static long lastBlockPos = -1;
@SubscribeEvent @SubscribeEvent
public static void rightClickingBlocksSelectsThem(PlayerInteractEvent.RightClickBlock event) { public static void rightClickingBlocksSelectsThem(PlayerInteractEvent.RightClickBlock event) {
if (currentItem == null) if (currentItem == null)
@ -74,21 +81,61 @@ public class ArmInteractionPointHandler {
public static void tick() { public static void tick() {
PlayerEntity player = Minecraft.getInstance().player; PlayerEntity player = Minecraft.getInstance().player;
World world = Minecraft.getInstance().world;
if (player == null) if (player == null)
return; return;
ItemStack heldItemMainhand = player.getHeldItemMainhand(); ItemStack heldItemMainhand = player.getHeldItemMainhand();
if (!AllBlocks.MECHANICAL_ARM.isIn(heldItemMainhand)) { if (!AllBlocks.MECHANICAL_ARM.isIn(heldItemMainhand)) {
currentItem = null; currentItem = null;
return; } else {
}
if (heldItemMainhand != currentItem) { if (heldItemMainhand != currentItem) {
currentSelection.clear(); currentSelection.clear();
currentItem = heldItemMainhand; currentItem = heldItemMainhand;
} }
for (Iterator<Entry<BlockPos, ArmInteractionPoint>> iterator = currentSelection.entrySet() drawOutlines(currentSelection);
}
checkForWrench(heldItemMainhand);
}
private static void checkForWrench(ItemStack heldItem) {
if(!AllItems.WRENCH.isIn(heldItem)) {
return;
}
RayTraceResult objectMouseOver = Minecraft.getInstance().objectMouseOver;
if (!(objectMouseOver instanceof BlockRayTraceResult)) {
return;
}
BlockRayTraceResult result = (BlockRayTraceResult) objectMouseOver;
BlockPos pos = result.getPos();
TileEntity te = Minecraft.getInstance().world.getTileEntity(pos);
if (!(te instanceof ArmTileEntity)) {
lastBlockPos = -1;
currentSelection.clear();
return;
}
if (lastBlockPos == -1 || lastBlockPos != pos.toLong()) {
currentSelection.clear();
ArmTileEntity arm = (ArmTileEntity) te;
arm.inputs.forEach(point -> currentSelection.put(point.pos, point));
arm.outputs.forEach(point -> currentSelection.put(point.pos, point));
lastBlockPos = pos.toLong();
}
if (lastBlockPos != -1) {
drawOutlines(currentSelection);
}
}
private static void drawOutlines(Map<BlockPos, ArmInteractionPoint> selection) {
World world = Minecraft.getInstance().world;
for (Iterator<Entry<BlockPos, ArmInteractionPoint>> iterator = selection.entrySet()
.iterator(); iterator.hasNext();) { .iterator(); iterator.hasNext();) {
Entry<BlockPos, ArmInteractionPoint> entry = iterator.next(); Entry<BlockPos, ArmInteractionPoint> entry = iterator.next();
BlockPos pos = entry.getKey(); BlockPos pos = entry.getKey();