diff --git a/src/main/java/gregtech/common/items/behaviors/TerminalBehaviour.java b/src/main/java/gregtech/common/items/behaviors/TerminalBehaviour.java index 4c8a679d77a..4d490c087eb 100644 --- a/src/main/java/gregtech/common/items/behaviors/TerminalBehaviour.java +++ b/src/main/java/gregtech/common/items/behaviors/TerminalBehaviour.java @@ -16,6 +16,7 @@ import net.minecraft.client.resources.I18n; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -60,6 +61,13 @@ public ActionResult onItemRightClick(World world, EntityPlayer player public void onUpdate(ItemStack itemStack, Entity entity) { NBTTagCompound tabletNBT = itemStack.getOrCreateSubCompound("terminal"); if (entity.ticksExisted % 20 == 0 && tabletNBT.hasKey("_ar")) { + if (entity instanceof EntityLivingBase) { + EntityLivingBase livingBase = (EntityLivingBase) entity; + if (!livingBase.getHeldItemMainhand().isItemEqual(itemStack) && !livingBase.getHeldItemOffhand().isItemEqual(itemStack)) { + return; + } + } + String appName = tabletNBT.getString("_ar"); int tier = TerminalRegistry.getApplication(appName).getMaxTier(); if (!TerminalBehaviour.isCreative(itemStack)) { diff --git a/src/main/java/gregtech/common/terminal/app/worldprospector/WorldProspectorARApp.java b/src/main/java/gregtech/common/terminal/app/worldprospector/WorldProspectorARApp.java index ddd83827e00..31f5c82d9ea 100644 --- a/src/main/java/gregtech/common/terminal/app/worldprospector/WorldProspectorARApp.java +++ b/src/main/java/gregtech/common/terminal/app/worldprospector/WorldProspectorARApp.java @@ -1,11 +1,13 @@ package gregtech.common.terminal.app.worldprospector; +import com.google.common.collect.Lists; import gregtech.api.gui.IRenderContext; import gregtech.api.gui.resources.ColorRectTexture; import gregtech.api.gui.resources.ItemStackTexture; import gregtech.api.gui.resources.ShaderTexture; import gregtech.api.gui.widgets.ImageWidget; import gregtech.api.gui.widgets.LabelWidget; +import gregtech.api.gui.widgets.PhantomSlotUtil; import gregtech.api.gui.widgets.PhantomSlotWidget; import gregtech.api.terminal.app.ARApplication; import gregtech.api.terminal.app.AbstractApplication; @@ -13,13 +15,18 @@ import gregtech.api.terminal.gui.widgets.RectButtonWidget; import gregtech.api.terminal.os.TerminalDialogWidget; import gregtech.api.terminal.os.TerminalTheme; +import gregtech.api.unification.OreDictUnifier; +import gregtech.api.unification.stack.MaterialStack; +import gregtech.api.util.GTLog; import gregtech.client.shader.Shaders; import gregtech.client.utils.DepthTextureUtil; import gregtech.client.utils.RenderBufferHelper; +import gregtech.client.utils.TooltipHelper; import gregtech.common.inventory.handlers.SingleItemStackHandler; import gregtech.common.items.MetaItems; import gregtech.common.terminal.app.worldprospector.matcher.BlockStateMatcher; import gregtech.common.terminal.app.worldprospector.matcher.IMatcher; +import mezz.jei.api.gui.IGhostIngredientHandler; import net.minecraft.block.Block; import net.minecraft.block.BlockFalling; import net.minecraft.client.Minecraft; @@ -31,6 +38,7 @@ import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; +import net.minecraft.inventory.ClickType; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -44,9 +52,14 @@ import net.minecraftforge.client.event.RenderWorldLastEvent; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; +import org.lwjgl.input.Mouse; import org.lwjgl.opengl.GL11; +import javax.annotation.Nonnull; +import java.awt.*; +import java.io.IOException; import java.util.*; +import java.util.List; public class WorldProspectorARApp extends ARApplication { @@ -81,12 +94,48 @@ public AbstractApplication initApp() { } RectButtonWidget buttonWidget = new RectButtonWidget(x + i * 24, y + 18, 18, 18, 1); addWidget(new PhantomSlotWidget(handlers[i], 0, x + i * 24, y) { + + @Override + public List> getPhantomTargets(Object ingredient) { + if (!(ingredient instanceof ItemStack)) { + return Collections.emptyList(); + } + Rectangle rectangle = toRectangleBox(); + return Collections.singletonList(new IGhostIngredientHandler.Target() { + @Nonnull + @Override + public Rectangle getArea() { + return rectangle; + } + + @Override + public void accept(@Nonnull Object ingredient) { + if (ingredient instanceof ItemStack) { + int mouseButton = Mouse.getEventButton(); + boolean shiftDown = TooltipHelper.isShiftDown(); + ClickType clickType = shiftDown ? ClickType.QUICK_MOVE : ClickType.PICKUP; + PhantomSlotUtil.slotClickPhantom(slotReference, mouseButton, clickType, (ItemStack) ingredient); + updateBlockSelectionAndColor((ItemStack) ingredient, index, buttonWidget); + writeClientAction(1, buffer -> { + buffer.writeItemStack((ItemStack) ingredient); + buffer.writeVarInt(mouseButton); + buffer.writeBoolean(shiftDown); + }); + } + } + }); + } + + @Override public boolean mouseClicked(int mouseX, int mouseY, int button) { if (handlers[index].getStackInSlot(0).isEmpty() && isMouseOverElement(mouseX, mouseY)) { writeClientAction(-1, buffer -> {}); selectReference(index, buttonWidget); return true; + } else if (isMouseOverElement(mouseX, mouseY)) { + writeClientAction(0, buffer -> {}); + updateBlockSelectionAndColor(ItemStack.EMPTY, index, buttonWidget); } return super.mouseClicked(mouseX, mouseY, button); } @@ -95,6 +144,20 @@ public boolean mouseClicked(int mouseX, int mouseY, int button) { public void handleClientAction(int id, PacketBuffer buffer) { if (id == -1) { selectReference(index, buttonWidget); + } + else if (id == 0) { + updateBlockSelectionAndColor(ItemStack.EMPTY, index, buttonWidget); + } + else if (id == 1) { + try { + buffer.markReaderIndex(); // just want to reset reader index, not both with .clear + ItemStack stack = buffer.readItemStack(); + updateBlockSelectionAndColor(stack, index, buttonWidget); + buffer.resetReaderIndex(); + } catch (IOException e) { + GTLog.logger.error("Could not update block selection from world prospector buffer", e); + } + super.handleClientAction(id, buffer); } else { super.handleClientAction(id, buffer); } @@ -119,6 +182,33 @@ public void handleClientAction(int id, PacketBuffer buffer) { return this; } + private void updateBlockSelectionAndColor(ItemStack stack, int index, RectButtonWidget buttonWidget) { + if (stack.getItem() instanceof ItemBlock) { + ItemStack copy = stack.copy(); + copy.setCount(1); + handlers[index].setStackInSlot(0, copy); + + MaterialStack ms = OreDictUnifier.getMaterial(copy); + Block block = ((ItemBlock) copy.getItem()).getBlock(); + if (block instanceof BlockFalling) { + colors[index] = ((BlockFalling) block).getDustColor(block.getStateFromMeta(copy.getMetadata())); + } else if (ms != null) { + colors[index] = ms.material.getMaterialRGB(); + } else { + colors[index] = block.getStateFromMeta(copy.getMetadata()).getMaterial().getMaterialMapColor().colorValue; + } + if (colors[index] == 0) { + colors[index] = block.hashCode(); + } + colors[index] = colors[index] | 0xff000000; + buttonWidget.setFill(colors[index]); + } else if (stack.isEmpty()) { + handlers[index].setStackInSlot(0, stack); + colors[index] = 0x00000000; + buttonWidget.setFill(colors[index]); + } + } + @Override public NBTTagCompound closeApp() { NBTTagCompound slots = new NBTTagCompound(); @@ -154,27 +244,9 @@ protected void hookDrawInBackground(int mouseX, int mouseY, float partialTicks, private void selectReference(int index, RectButtonWidget buttonWidget) { TerminalDialogWidget.showItemSelector(getOs(), "terminal.world_prospector.reference", false, - stack-> stack.getItem() instanceof ItemBlock, - stack -> { - if (stack.getItem() instanceof ItemBlock) { - ItemStack copy = stack.copy(); - copy.setCount(1); - handlers[index].setStackInSlot(0, copy); - Block block = ((ItemBlock)copy.getItem()).getBlock(); - - if (block instanceof BlockFalling) { - colors[index] = ((BlockFalling) block).getDustColor(block.getStateFromMeta(copy.getMetadata())); - } else { - colors[index] = block.getStateFromMeta(copy.getMetadata()).getMaterial().getMaterialMapColor().colorValue; - } - if (colors[index] == 0) { - colors[index] = block.hashCode(); - } - colors[index] = colors[index] | 0xff000000; - buttonWidget.setFill(colors[index]); - } - - }).open(); + stack -> stack.getItem() instanceof ItemBlock, + stack -> updateBlockSelectionAndColor(stack, index, buttonWidget) + ).open(); } private int getMaxRadius() { @@ -228,6 +300,7 @@ public void onAROpened() { for (Tuple stack : getAllSlotStack()) { if (stack.getFirst().getItem() instanceof ItemBlock) { Block block = ((ItemBlock) stack.getFirst().getItem()).getBlock(); + if (block != Blocks.AIR) { matchers.add(new BlockStateMatcher(block.getStateFromMeta(stack.getFirst().getMetadata()), stack.getSecond())); } @@ -319,6 +392,8 @@ private static void addCluster(BlockPos pos, Map> f @Override public void tickAR(EntityPlayer player) { World world = player.world; + if (Minecraft.getMinecraft().isGamePaused()) return; + if (radius == 0 || lastPos == null) { lastPos = player.getPosition(); }