Initial Commit. Alle basic Methods, based on Inventory Tweaks.

This commit is contained in:
0xSeb
2015-01-30 14:36:30 +01:00
commit eb45b90b81
6 changed files with 496 additions and 0 deletions
@@ -0,0 +1,168 @@
package de.skate702.craftingkeys;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
/**
* Managing Class to move Items in Inventory Containers.
*
* @author skate702
*
*/
public class ContainerManager {
/**
* The Container to work with.
*/
private Container container;
public ContainerManager(Container container) {
this.container = container;
}
/**
* Moves a full Stack from a slot to another.
*
* @param srcIndex
* The Source Slot Index of the Container
* @param destIndex
* The Destination Slot Index of the Container
*/
public void moveAll(int srcIndex, int destIndex) {
// TODO: Consistency - Use move()-Method
ItemStack source = getItemStack(srcIndex);
ItemStack destination = getItemStack(destIndex);
leftClick(srcIndex);
leftClick(destIndex);
Helper.debugPrint("moveAll(): Moved Stack from " + srcIndex + " to " + destIndex + "!");
// Stack Swapping
if (Helper.client.thePlayer.inventory.getItemStack() != null) {
Helper.debugPrint("moveAll(): Destination not empty!");
leftClick(srcIndex);
}
}
/**
* Moves a specified amount of Items from a slot to another. [Based on
* INVTW]
*
* @param srcIndex
* The Source Slot Index of the Container
* @param destIndex
* The Destination Slot Index of the Container
* @param amount
* The amount of items to move (can be bigger then Stack Size)
*/
public void move(int srcIndex, int destIndex, int amount) {
// TODO: What if not same type? Fallback Solution (Stack out of the way)
// Stacks
ItemStack source = getItemStack(srcIndex);
ItemStack destination = getItemStack(destIndex);
// Same Location?
if (source == null || srcIndex == destIndex) {
Helper.debugPrint("Move(): srcIndex == destIndex");
return;
}
// Test for max. moving Amount
int sourceSize = source.stackSize;
int movedAmount = Math.min(amount, sourceSize);
// Move some
if (destination == null || source.isItemEqual(destination)) {
leftClick(srcIndex);
for (int i = 0; i < movedAmount; i++) {
rightClick(destIndex);
}
// Move back
if (movedAmount < sourceSize) {
leftClick(srcIndex);
}
Helper.debugPrint("move(): Moved " + movedAmount + " from " + srcIndex + " to " + destIndex + "!");
} else {
Helper.debugPrint("Move(): Not the same block type!");
}
}
/**
* Returns the ItemStack in a slot [Based on INVTW]
*
* @param index
* The index of the slot in the container
* @return Returns the ItemStack
*/
private ItemStack getItemStack(int index) {
if (index >= 0 && index < container.inventorySlots.size()) {
Slot slot = (Slot) (container.inventorySlots.get(index));
return (slot == null) ? null : slot.getStack();
} else {
Helper.debugPrint("getItemStack(): Invalid index");
return null;
}
}
/**
* Executes a left mouse click on a slot. [Based on INVTW]
*
* @param index
* The index of the slot in the container
*/
private void leftClick(int index) {
slotClick(index, false);
}
/**
* Executes a right mouse click on a slot. [Based on INVTW]
*
* @param index
* The index of the slot in the container
*/
private void rightClick(int index) {
slotClick(index, true);
}
/**
* Executes a mouse click on a slot. [Based on INVTW]
*
* @param index
* The index of the slot in the container
* @param rightClick
* True, if the click is with the right mouse button
*/
private void slotClick(int index, boolean rightClick) {
Helper.debugPrint("slotClick(): Clicked @ Slot " + index + " with data " + rightClick + ".");
int rightClickData = (rightClick) ? 1 : 0;
CraftingKeys.instance.proxy.sendSlotClick(Helper.client.playerController, container.windowId, index,
rightClickData, 0, Helper.client.thePlayer);
}
}
@@ -0,0 +1,113 @@
package de.skate702.craftingkeys;
import org.lwjgl.input.Keyboard;
import net.minecraft.client.gui.inventory.GuiCrafting;
import net.minecraft.inventory.Slot;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import de.skate702.craftingkeys.proxies.CraftingKeysProxy;
/**
* The Main Class of the Mod with the important onTick-Method. Some Methods are
* base on the open-source Inventory Tweaks. Big Thanks for that!
*
* @author skate702
*
*/
@Mod(modid = "CraftingKeysID", name = "Crafting Keys", version = "1.0.0")
public class CraftingKeys {
/**
* Current Instance of CraftingKeys.
*/
@Instance(value = "CraftingKeysID")
public static CraftingKeys instance;
/**
* Current Proxy (Common or Client)
*/
@SidedProxy(clientSide = "de.skate702.craftingkeys.proxies.CraftingKeysClientProxy", serverSide = "de.skate702.craftingkeys.proxies.CraftingKeysProxy")
public static CraftingKeysProxy proxy;
/**
* This method will be executed before Init.
*
* @param event
* Input Event from FML
*/
@EventHandler
public void preInit(FMLPreInitializationEvent event) {
}
/**
* This method will be executed while loading.
*
* @param event
* Input Event from FML
*/
@EventHandler
public void load(FMLInitializationEvent event) {
// Regeistring
proxy.registerRenderers();
FMLCommonHandler.instance().bus().register(this);
Helper.debugPrint("load(): Loaded Mod successful");
}
/**
* This method will be executed after Init.
*
* @param event
* Input Event from FML
*/
@EventHandler
public void postInit(FMLPostInitializationEvent event) {
}
/**
* This method will be executed every Ingame Tick.
*
* @param tick
*/
@SubscribeEvent
public void onTick(TickEvent.ClientTickEvent tick) {
// if (mc.currentScreen != null) {
// if (mc.currentScreen instanceof
// net.minecraft.client.gui.inventory.GuiCrafting) {
// Keyboard.isKeyDown(Keyboard.KEY_Q)
// DEBUGGIN TEST START
if (Helper.client.currentScreen != null) {
if (Helper.client.currentScreen instanceof GuiCrafting) {
if (Keyboard.isKeyDown(Keyboard.KEY_C)) {
GuiCrafting guiCrafting = (GuiCrafting) Helper.client.currentScreen;
Slot currentHoveredSlot = Helper.getSlotAtMousePosition(guiCrafting);
ContainerManager con = new ContainerManager(guiCrafting.inventorySlots);
if (currentHoveredSlot != null) {
con.move(currentHoveredSlot.slotNumber, 8, 1);
} else {
Helper.debugPrint("onTick(): Outside of inventory");
}
}
}
}
// DEBUGGIN TEST END
}
}
@@ -0,0 +1,124 @@
package de.skate702.craftingkeys;
import org.lwjgl.input.Mouse;
import cpw.mods.fml.client.FMLClientHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
/**
* This helper class provides some static helping Methods and Constants.
*
* @author sebastian
*
*/
public class Helper {
/**
* Private Constructor. This is a helping class!
*/
private Helper() {
// Nope.
}
/**
* If true, there are Debug-Output-Prints from debugPrint(msg).
*/
public static final boolean DEBUG = true;
/**
* Current Instance Client, used for a lot of operations.
*/
public static Minecraft client = FMLClientHandler.instance().getClient();
/**
* Standart Output for Debug-Messages - Depends on DEBUG-Constant.
*
* @param message
* The Debug-Message. Syntax: "methodname(): Message"
*/
public static void debugPrint(String message) {
if (DEBUG) {
System.out.println("CK-DEBUG: " + message);
}
}
/**
* Returns the Slot at the current Mouse Position. [FROM GUICONTAINER]
*
* @param guiContainer
* The (Inventory) Container to work with.
* @return Returns the slot (or null)
*/
public static Slot getSlotAtMousePosition(GuiContainer guiContainer) {
if (guiContainer != null) {
Container container = guiContainer.inventorySlots;
int x = getMouseX(guiContainer);
int y = getMouseY(guiContainer);
for (int k = 0; k < container.inventorySlots.size(); k++) {
Slot slot = (Slot) container.inventorySlots.get(k);
if (getIsMouseOverSlot(guiContainer, slot, x, y)) {
return slot;
}
}
return null;
} else {
Helper.debugPrint("getSlotAtMousePosition(): guiContainer == null");
return null;
}
}
/**
* Returns, if the mouse cursor is over a specified slot [FROM GUICONTAINER]
*
* @param guiContainer
* the GuiContainer to work with
* @param slot
* The spcified slot
* @param x
* The Mouse x-Position
* @param y
* The Mouse y-Position
* @return True, if the mouse is positioned over this slot. Otherwise false
*/
private static boolean getIsMouseOverSlot(GuiContainer guiContainer, Slot slot, int x, int y) {
if (guiContainer != null) {
// Constants from Minecraft Source Code
x -= (guiContainer.width - 176) / 2;
y -= (guiContainer.height - 166) / 2;
return x >= slot.xDisplayPosition - 1 && x < slot.xDisplayPosition + 16 + 1
&& y >= slot.yDisplayPosition - 1 && y < slot.yDisplayPosition + 16 + 1;
} else {
return false;
}
}
/**
* Returns a calculated Mouse Position for getSlotAtMousePosition().
*
* @param guiContainer
* the GuiContainer to work with
* @return The relative Mouse-Position
*/
private static int getMouseX(GuiContainer guiContainer) {
return (Mouse.getEventX() * guiContainer.width) / client.displayWidth;
}
/**
* Returns a calculated Mouse Position for getSlotAtMousePosition().
*
* @param guiContainer
* the GuiContainer to work with
* @return The relative Mouse-Position
*/
private static int getMouseY(GuiContainer guiContainer) {
return guiContainer.height - (Mouse.getEventY() * guiContainer.height) / client.displayHeight - 1;
}
}
@@ -0,0 +1,30 @@
package de.skate702.craftingkeys.proxies;
import net.minecraft.client.multiplayer.PlayerControllerMP;
import net.minecraft.entity.player.EntityPlayer;
/**
* Client Proxy, extending Common Proxy.
*
* @author skate702
*
*/
public class CraftingKeysClientProxy extends CraftingKeysProxy {
@Override
public void registerRenderers() {
}
@Override
public void sendSlotClick(PlayerControllerMP controller, int windowId, int slot, int rightClick, int action,
EntityPlayer player) {
// TODO: Multiplayer friendly?
controller.windowClick(windowId, slot, rightClick, action, player);
// player.openContainer.slotClick(slot, rightClick, action, player);
}
}
@@ -0,0 +1,45 @@
package de.skate702.craftingkeys.proxies;
import net.minecraft.client.multiplayer.PlayerControllerMP;
import net.minecraft.entity.player.EntityPlayer;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
/**
* The common proxy.
*
* @author skate702
*
*/
public class CraftingKeysProxy {
/**
* Register Renderers (not needed here).
*/
public void registerRenderers() {
}
/**
* The Minecraft call to send a mouse click to a GUI. [Based on INVTW]
*
* @param controller
* The playerController (from Client)
* @param windowId
* The current Windows ID (from GUI)
* @param slot
* The slot to click on (slot index)
* @param rightClick
* 1, if right click. Otherwise 0
* @param action
* 0
* @param player
* The current player (from Client)
*/
@SideOnly(Side.CLIENT)
public void sendSlotClick(PlayerControllerMP controller, int windowId, int slot, int rightClick, int action,
EntityPlayer player) {
}
}