Skip to content

Commit

Permalink
Cheat items to the mouse, like the creative menu. (with config option)
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Nov 21, 2017
1 parent efaf359 commit e0a4fae
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 44 deletions.
7 changes: 7 additions & 0 deletions src/main/java/mezz/jei/config/Config.java
Expand Up @@ -18,6 +18,7 @@
import mezz.jei.network.packets.PacketRequestCheatPermission;
import mezz.jei.startup.ForgeModIdHelper;
import mezz.jei.startup.IModIdHelper;
import mezz.jei.util.GiveMode;
import mezz.jei.util.Log;
import mezz.jei.util.Translator;
import net.minecraft.util.text.TextFormatting;
Expand Down Expand Up @@ -120,6 +121,10 @@ public static boolean isCenterSearchBarEnabled() {
return values.centerSearchBarEnabled;
}

public static GiveMode getGiveMode() {
return values.giveMode;
}

public static String getModNameFormat() {
String override = Config.modNameFormatOverride;
if (override != null) {
Expand Down Expand Up @@ -328,6 +333,8 @@ private static boolean syncConfig() {

values.centerSearchBarEnabled = config.getBoolean(CATEGORY_ADVANCED, "centerSearchBarEnabled", defaultValues.centerSearchBarEnabled);

values.giveMode = config.getEnum("giveMode", CATEGORY_ADVANCED, defaultValues.giveMode, GiveMode.values());

updateModNameFormat(config);

{
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/mezz/jei/config/ConfigValues.java
@@ -1,9 +1,12 @@
package mezz.jei.config;

import mezz.jei.util.GiveMode;

public class ConfigValues {
// advanced
public boolean debugModeEnabled = false;
public boolean centerSearchBarEnabled = false;
public GiveMode giveMode = GiveMode.MOUSE_PICKUP;
public final String modNameFormatFriendly = "blue italic";
public String modNameFormat = Config.parseFriendlyModNameFormat(modNameFormatFriendly);
public int maxSubtypes = 300;
Expand Down
30 changes: 21 additions & 9 deletions src/main/java/mezz/jei/gui/overlay/IngredientGrid.java
Expand Up @@ -17,13 +17,15 @@
import mezz.jei.render.IngredientListSlot;
import mezz.jei.render.IngredientRenderer;
import mezz.jei.runtime.JeiRuntime;
import mezz.jei.util.GiveMode;
import mezz.jei.util.MathUtil;
import mezz.jei.util.Translator;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.ItemHandlerHelper;

import javax.annotation.Nullable;
import java.awt.Rectangle;
Expand Down Expand Up @@ -87,7 +89,7 @@ public void draw(Minecraft minecraft, int mouseX, int mouseY) {

guiIngredientSlots.render(minecraft);

if (!shouldShowDeleteItemTooltip(minecraft) && isMouseOver(mouseX, mouseY)) {
if (!shouldDeleteItemOnClick(minecraft, mouseX, mouseY) && isMouseOver(mouseX, mouseY)) {
IngredientRenderer hovered = guiIngredientSlots.getHovered(mouseX, mouseY);
if (hovered != null) {
hovered.drawHighlight();
Expand All @@ -99,7 +101,7 @@ public void draw(Minecraft minecraft, int mouseX, int mouseY) {

public void drawTooltips(Minecraft minecraft, int mouseX, int mouseY) {
if (isMouseOver(mouseX, mouseY)) {
if (shouldShowDeleteItemTooltip(minecraft)) {
if (shouldDeleteItemOnClick(minecraft, mouseX, mouseY)) {
String deleteItem = Translator.translateToLocal("jei.tooltip.delete.item");
TooltipRenderer.drawHoveringText(minecraft, deleteItem, mouseX, mouseY);
} else {
Expand All @@ -111,12 +113,25 @@ public void drawTooltips(Minecraft minecraft, int mouseX, int mouseY) {
}
}

private static boolean shouldShowDeleteItemTooltip(Minecraft minecraft) {
private boolean shouldDeleteItemOnClick(Minecraft minecraft, int mouseX, int mouseY) {
if (Config.isDeleteItemsInCheatModeActive()) {
EntityPlayer player = minecraft.player;
if (!player.inventory.getItemStack().isEmpty()) {
ItemStack itemStack = player.inventory.getItemStack();
if (!itemStack.isEmpty()) {
JeiRuntime runtime = Internal.getRuntime();
return runtime == null || !runtime.getRecipesGui().isOpen();
if (runtime == null || !runtime.getRecipesGui().isOpen()) {
GiveMode giveMode = Config.getGiveMode();
if (giveMode == GiveMode.MOUSE_PICKUP) {
IClickedIngredient<?> ingredientUnderMouse = getIngredientUnderMouse(mouseX, mouseY);
if (ingredientUnderMouse != null && ingredientUnderMouse.getValue() instanceof ItemStack) {
ItemStack value = (ItemStack) ingredientUnderMouse.getValue();
if (ItemHandlerHelper.canItemStacksStack(itemStack, value)) {
return false;
}
}
}
return true;
}
}
}
return false;
Expand All @@ -129,10 +144,7 @@ public boolean isMouseOver(int mouseX, int mouseY) {
public boolean handleMouseClicked(int mouseX, int mouseY) {
if (isMouseOver(mouseX, mouseY)) {
Minecraft minecraft = Minecraft.getMinecraft();

JeiRuntime runtime = Internal.getRuntime();
if (Config.isDeleteItemsInCheatModeActive() && (runtime == null || !runtime.getRecipesGui().isOpen())) {

if (shouldDeleteItemOnClick(minecraft, mouseX, mouseY)) {
EntityPlayerSP player = minecraft.player;
ItemStack itemStack = player.inventory.getItemStack();
if (!itemStack.isEmpty()) {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/mezz/jei/gui/recipes/RecipeCategoryTab.java
Expand Up @@ -7,7 +7,6 @@
import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.ingredients.IIngredientRenderer;
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.config.Config;
import mezz.jei.ingredients.IngredientRegistry;
import mezz.jei.startup.ForgeModIdHelper;
import mezz.jei.util.LegacyUtil;
Expand Down
26 changes: 17 additions & 9 deletions src/main/java/mezz/jei/input/InputHandler.java
Expand Up @@ -4,6 +4,8 @@
import java.util.ArrayList;
import java.util.List;

import it.unimi.dsi.fastutil.ints.IntArraySet;
import it.unimi.dsi.fastutil.ints.IntSet;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.ingredients.IIngredientRegistry;
import mezz.jei.api.recipe.IFocus;
Expand All @@ -21,6 +23,7 @@
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.settings.GameSettings;
import net.minecraft.item.ItemStack;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
Expand All @@ -32,7 +35,7 @@ public class InputHandler {
private final IngredientListOverlay ingredientListOverlay;
private final List<IShowsRecipeFocuses> showsRecipeFocuses = new ArrayList<>();

private boolean clickHandled = false;
private IntSet clickHandled = new IntArraySet();

public InputHandler(JeiRuntime runtime, IngredientListOverlay ingredientListOverlay) {
this.recipeRegistry = runtime.getRecipeRegistry();
Expand All @@ -47,14 +50,17 @@ public InputHandler(JeiRuntime runtime, IngredientListOverlay ingredientListOver

public boolean handleMouseEvent(GuiScreen guiScreen, int mouseX, int mouseY) {
boolean cancelEvent = false;
if (Mouse.getEventButton() > -1) {
final int eventButton = Mouse.getEventButton();
if (eventButton > -1) {
if (Mouse.getEventButtonState()) {
if (!clickHandled) {
cancelEvent = handleMouseClick(guiScreen, Mouse.getEventButton(), mouseX, mouseY);
clickHandled = cancelEvent;
if (!clickHandled.contains(eventButton)) {
cancelEvent = handleMouseClick(guiScreen, eventButton, mouseX, mouseY);
if (cancelEvent) {
clickHandled.add(eventButton);
}
}
} else if (clickHandled) {
clickHandled = false;
} else if (clickHandled.contains(eventButton)) {
clickHandled.remove(eventButton);
cancelEvent = true;
}
} else if (Mouse.getEventDWheel() != 0) {
Expand Down Expand Up @@ -124,14 +130,16 @@ private <V> boolean handleMouseClickedFocus(int mouseButton, IClickedIngredient<
}

if (Config.isCheatItemsEnabled() && clicked.allowsCheating() && !recipesGui.isOpen()) {
final boolean fullStack = (mouseButton == 0);
Minecraft minecraft = Minecraft.getMinecraft();
GameSettings gameSettings = minecraft.gameSettings;
final boolean fullStack = (mouseButton == 0 || gameSettings.keyBindPickBlock.isActiveAndMatches(mouseButton - 100));
if (fullStack || mouseButton == 1) {
V focusValue = clicked.getValue();
IIngredientHelper<V> ingredientHelper = ingredientRegistry.getIngredientHelper(focusValue);

ItemStack itemStack = ingredientHelper.cheatIngredient(focusValue, fullStack);
if (!itemStack.isEmpty()) {
CommandUtil.giveStack(itemStack, fullStack);
CommandUtil.giveStack(itemStack, mouseButton);
}
return true;
}
Expand Down
Expand Up @@ -6,6 +6,7 @@
import mezz.jei.network.IPacketId;
import mezz.jei.network.PacketIdServer;
import mezz.jei.util.CommandUtilServer;
import mezz.jei.util.GiveMode;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
Expand All @@ -14,9 +15,11 @@

public class PacketGiveItemStack extends PacketJei {
private final ItemStack itemStack;
private final GiveMode giveMode;

public PacketGiveItemStack(ItemStack itemStack) {
public PacketGiveItemStack(ItemStack itemStack, GiveMode giveMode) {
this.itemStack = itemStack;
this.giveMode = giveMode;
}

@Override
Expand All @@ -28,6 +31,7 @@ public IPacketId getPacketId() {
public void writePacketData(PacketBuffer buf) {
NBTTagCompound nbt = itemStack.serializeNBT();
buf.writeCompoundTag(nbt);
buf.writeEnumValue(giveMode);
}

public static class Handler implements IPacketJeiHandler {
Expand All @@ -38,10 +42,11 @@ public void readPacketData(PacketBuffer buf, EntityPlayer player) throws IOExcep

NBTTagCompound itemStackSerialized = buf.readCompoundTag();
if (itemStackSerialized != null) {
GiveMode giveMode = buf.readEnumValue(GiveMode.class);
ItemStack itemStack = new ItemStack(itemStackSerialized);
if (!itemStack.isEmpty()) {
if (CommandUtilServer.hasPermission(sender, itemStack)) {
CommandUtilServer.executeGive(sender, itemStack);
CommandUtilServer.executeGive(sender, itemStack, giveMode);
} else {
JustEnoughItems.getProxy().sendPacketToClient(new PacketCheatPermission(false), sender);
}
Expand Down
Expand Up @@ -2,12 +2,9 @@

import javax.annotation.Nullable;

import com.google.common.collect.ImmutableList;
import mezz.jei.api.IGuiHelper;
import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.gui.IGuiFluidStackGroup;
import mezz.jei.api.gui.IGuiIngredient;
import mezz.jei.api.gui.IGuiIngredientGroup;
import mezz.jei.api.gui.IGuiItemStackGroup;
import mezz.jei.api.gui.IRecipeLayout;
import mezz.jei.api.ingredients.IIngredients;
Expand All @@ -17,8 +14,6 @@
import mezz.jei.util.Translator;
import net.minecraft.util.ResourceLocation;

import java.util.List;

public class IngredientInfoRecipeCategory implements IRecipeCategory<IngredientInfoRecipe> {
public static final int recipeWidth = 160;
public static final int recipeHeight = 125;
Expand Down
Expand Up @@ -14,7 +14,6 @@
import mezz.jei.api.recipe.wrapper.ICraftingRecipeWrapper;
import mezz.jei.api.recipe.wrapper.ICustomCraftingRecipeWrapper;
import mezz.jei.api.recipe.wrapper.IShapedCraftingRecipeWrapper;
import mezz.jei.config.Config;
import mezz.jei.config.Constants;
import mezz.jei.startup.ForgeModIdHelper;
import mezz.jei.util.Translator;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/mezz/jei/startup/AbstractModIdHelper.java
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.config.Config;
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/mezz/jei/util/CommandUtil.java
@@ -1,6 +1,7 @@
package mezz.jei.util;

import mezz.jei.JustEnoughItems;
import mezz.jei.config.Config;
import mezz.jei.config.SessionData;
import mezz.jei.network.packets.PacketGiveItemStack;
import net.minecraft.client.Minecraft;
Expand All @@ -19,20 +20,18 @@ public final class CommandUtil {
private CommandUtil() {
}

public static void giveStack(ItemStack itemstack, boolean fullStack) {
int amount = fullStack ? itemstack.getMaxStackSize() : 1;
giveStack(itemstack, amount);
}

/**
* /give <player> <item> [amount] [data] [dataTag]
*/
public static void giveStack(ItemStack itemStack, int amount) {
public static void giveStack(ItemStack itemStack, int mouseButton) {
if (SessionData.isJeiOnServer()) {
final GiveMode giveMode = Config.getGiveMode();
final int amount = giveMode.getStackSize(itemStack, mouseButton);
ItemStack sendStack = ItemHandlerHelper.copyStackWithSize(itemStack, amount);
PacketGiveItemStack packet = new PacketGiveItemStack(sendStack);
PacketGiveItemStack packet = new PacketGiveItemStack(sendStack, giveMode);
JustEnoughItems.getProxy().sendPacketToServer(packet);
} else {
int amount = GiveMode.INVENTORY.getStackSize(itemStack, mouseButton);
giveStackVanilla(itemStack, amount);
}
}
Expand Down
40 changes: 35 additions & 5 deletions src/main/java/mezz/jei/util/CommandUtilServer.java
Expand Up @@ -26,6 +26,7 @@
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.CommandEvent;
import net.minecraftforge.items.ItemHandlerHelper;

/**
* Server-side-safe utilities for commands.
Expand Down Expand Up @@ -92,7 +93,30 @@ public static boolean hasPermission(EntityPlayerMP sender, ItemStack itemStack)
*
* @see CommandGive#execute(MinecraftServer, ICommandSender, String[])
*/
public static void executeGive(EntityPlayerMP sender, ItemStack itemStack) {
public static void executeGive(EntityPlayerMP sender, ItemStack itemStack, GiveMode giveMode) {
if (giveMode == GiveMode.INVENTORY) {
giveToInventory(sender, itemStack);
} else if (giveMode == GiveMode.MOUSE_PICKUP) {
mousePickupItemStack(sender, itemStack);
}
}

private static void mousePickupItemStack(EntityPlayerMP sender, ItemStack itemStack) {
int giveCount;
ItemStack existingStack = sender.inventory.getItemStack();
if (ItemHandlerHelper.canItemStacksStack(existingStack, itemStack)) {
int newCount = Math.min(existingStack.getMaxStackSize(), existingStack.getCount() + itemStack.getCount());
giveCount = newCount - existingStack.getCount();
existingStack.setCount(newCount);
} else {
sender.inventory.setItemStack(itemStack);
giveCount = itemStack.getCount();
}
notifyGive(sender, itemStack, giveCount);
sender.updateHeldItem();
}

private static void giveToInventory(EntityPlayerMP sender, ItemStack itemStack) {
int count = itemStack.getCount();
boolean addedToInventory = sender.inventory.addItemStackToInventory(itemStack);

Expand All @@ -112,10 +136,16 @@ public static void executeGive(EntityPlayerMP sender, ItemStack itemStack) {
}
}

ICommand giveCommand = getGiveCommand(sender);
if (giveCommand != null) {
itemStack.setCount(1);
CommandBase.notifyCommandListener(sender, giveCommand, "commands.give.success", itemStack.getTextComponent(), count, sender.getName());
notifyGive(sender, itemStack, count);
}

private static void notifyGive(EntityPlayerMP sender, ItemStack itemStack, int count) {
if (!sender.isCreative() && count > 0) {
ICommand giveCommand = getGiveCommand(sender);
if (giveCommand != null) {
ItemStack copy = ItemHandlerHelper.copyStackWithSize(itemStack, 1);
CommandBase.notifyCommandListener(sender, giveCommand, "commands.give.success", copy.getTextComponent(), count, sender.getName());
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/main/java/mezz/jei/util/GiveMode.java
@@ -0,0 +1,23 @@
package mezz.jei.util;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.item.ItemStack;

public enum GiveMode {
INVENTORY {
@Override
public int getStackSize(ItemStack itemStack, int mouseButton) {
return (mouseButton == 0) ? itemStack.getMaxStackSize() : 1;
}
},
MOUSE_PICKUP {
@Override
public int getStackSize(ItemStack itemStack, int mouseButton) {
boolean modifierActive = GuiScreen.isShiftKeyDown() || Minecraft.getMinecraft().gameSettings.keyBindPickBlock.isActiveAndMatches(mouseButton - 100);
return modifierActive ? itemStack.getMaxStackSize() : 1;
}
};

public abstract int getStackSize(ItemStack itemStack, int mouseButton);
}

0 comments on commit e0a4fae

Please sign in to comment.