Started refactoring. Added KeyBindings.

This commit is contained in:
0xSeb
2015-10-20 01:06:12 +02:00
parent 5243dab2a2
commit dd2021bbed
6 changed files with 377 additions and 298 deletions
@@ -1,10 +1,11 @@
package de.skate702.craftingkeys;
import org.lwjgl.input.Keyboard;
import de.skate702.craftingkeys.manager.ContainerManager;
import de.skate702.craftingkeys.proxies.CraftingKeysProxy;
import net.minecraft.client.gui.inventory.GuiCrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.util.ChatComponentText;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
@@ -13,162 +14,158 @@ import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.Event;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.InputEvent;
import net.minecraftforge.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!
* based on the open-source Inventory Tweaks. Big Thanks for that!
*
* @author skate702
*
*/
@Mod(modid = "CraftingKeysID", name = "Crafting Keys", version = "1.0.0")
@Mod(modid = "craftingkeys", name = "Crafting Keys", version = "1.0.0")
public class CraftingKeys {
/**
* Current Instance of CraftingKeys.
*/
@Instance(value = "CraftingKeysID")
public static CraftingKeys instance;
/**
* Current Instance of CraftingKeys.
*/
@Instance(value = "CraftingKeys")
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;
/**
* 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 before Init.
*
* @param event Input Event from FML
*/
@EventHandler
public void preInit(FMLPreInitializationEvent event) {
KeyBindings.init();
/**
* 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);
/**
* This method will be executed while loading.
*
* @param event Input Event from FML
*/
@EventHandler
public void load(FMLInitializationEvent event) {
Helper.debugPrint("load(): Loaded CraftingKeys successful");
// Registering
proxy.registerRenderers();
FMLCommonHandler.instance().bus().register(this);
}
/**
* This method will be executed after Init.
*
* @param event
* Input Event from FML
*/
@EventHandler
public void postInit(FMLPostInitializationEvent event) {
}
Helper.debugPrint("load(): Loaded CraftingKeys successful");
/**
* To show a chat message at first start in minecraft.
*/
boolean firstInWorldTick = true;
}
/**
* This method will be executed every Ingame Tick.
*
* @param tick
*/
@SubscribeEvent
public void onTick(TickEvent.ClientTickEvent tick) {
@SubscribeEvent
public void onKeyInput(InputEvent.KeyInputEvent event) {
System.out.println(event.toString());
}
// Message
if (Helper.client.theWorld != null && firstInWorldTick) {
/**
* This method will be executed after Init.
*
* @param event Input Event from FML
*/
@EventHandler
public void postInit(FMLPostInitializationEvent event) {
}
Helper.client.thePlayer.addChatMessage(new ChatComponentText(
"Achtung: Crafting-Keys befindet sich in der Pre-Alpha!"));
Helper.client.thePlayer.addChatMessage(new ChatComponentText(
"Es gibt noch viele Fehler, und es fehlen noch einige Funktionen."));
Helper.client.thePlayer.addChatMessage(new ChatComponentText(
"Mehr Info's findest du auf: http://craftingkeys.codeplex.com!"));
/**
* This method will be executed every Ingame Tick.
*
* @param tick
*/
@SubscribeEvent
public void onTick(TickEvent.ClientTickEvent tick) {
firstInWorldTick = !firstInWorldTick;
}
// Message
if(Helper.isFirstInWorldTick()) {
Helper.printWarning();
}
// Case 1: Classic GUI Screen
if (Helper.isCraftingGUI(Helper.client.currentScreen)) {
GuiCrafting guiCrafting = (GuiCrafting) Helper.client.currentScreen;
Slot currentHoveredSlot = Helper.getSlotAtMousePosition(guiCrafting);
int keyDown = Helper.craftingKeyDownToSlotNumber();
ContainerManager con = new ContainerManager(guiCrafting.inventorySlots);
// Case 1: Classic GUI Screen
if (Helper.isCraftingGUI(Helper.client.currentScreen)) {
// Block Key Interval (avoid multiple Runs)
if (!Helper.isSameKey(keyDown)) {
GuiCrafting guiCrafting = (GuiCrafting) Helper.client.currentScreen;
Slot currentHoveredSlot = Helper.getSlotAtMousePosition(guiCrafting);
int keyDown = Helper.craftingKeyDownToSlotNumber();
// Moving item to crafting table
if (keyDown > 0 && currentHoveredSlot != null) {
ContainerManager con = new ContainerManager(guiCrafting.inventorySlots);
// Shift = Move all
if (guiCrafting.isShiftKeyDown()) {
con.moveAll(currentHoveredSlot.slotNumber, keyDown);
} else {
con.move(currentHoveredSlot.slotNumber, keyDown, 1);
}
// Block Key Interval (avoid multiple Runs)
if (!Helper.isSameKey(keyDown)) {
}
// Moving item to crafting table
if (keyDown > 0 && currentHoveredSlot != null) {
if (keyDown == -2) {
// Shift = Move all
if (guiCrafting.isShiftKeyDown()) {
con.moveAll(currentHoveredSlot.slotNumber, keyDown);
} else {
con.move(currentHoveredSlot.slotNumber, keyDown, 1);
}
// Space = Move all back
Helper.debugPrint("onTick(): Move all items back or drop them.");
}
for (int i = 1; i < 10; i++) {
if (keyDown == -2) {
con.putStackToNextEmptySlot(i, true, false);
// Space = Move all back
Helper.debugPrint("onTick(): Move all items back or drop them.");
}
}
for (int i = 1; i < 10; i++) {
// Strg = Take the output
if (guiCrafting.isCtrlKeyDown()) {
con.putStackToNextEmptySlot(i, true, false);
if (guiCrafting.isShiftKeyDown()) {
}
}
// Strg + Shift = Move all (resp. faster!)
con.clickOnCraftingOutput(true);
// Strg = Take the output
if (guiCrafting.isCtrlKeyDown()) {
} else {
if (guiCrafting.isShiftKeyDown()) {
// Send mouse click on crafting output (accept also
// holding)
int ticksdown = Helper.getStrgTimesDown(true);
if (ticksdown == 2 || ticksdown % 15 == 0 || (ticksdown > 60 && ticksdown % 8 == 0)) {
con.clickOnCraftingOutput(true);
}
// Strg + Shift = Move all (resp. faster!)
con.clickOnCraftingOutput(true);
}
} else {
} else {
// Send mouse click on crafting output (accept also
// holding)
int ticksdown = Helper.getStrgTimesDown(true);
if (ticksdown == 2 || ticksdown % 15 == 0 || (ticksdown > 60 && ticksdown % 8 == 0)) {
con.clickOnCraftingOutput(true);
}
// Reset Strg
Helper.getStrgTimesDown(false);
}
}
}
}
} else {
// Case 2: Inventory (2x2 Crafting, Quick-Armor)
// Reset Strg
Helper.getStrgTimesDown(false);
}
// TODO: Case 2
}
}
}
// Case 2: Inventory (2x2 Crafting, Quick-Armor)
// TODO: Case 2
}
}
+195 -180
View File
@@ -1,226 +1,241 @@
package de.skate702.craftingkeys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import net.minecraftforge.fml.client.FMLClientHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.gui.inventory.GuiCrafting;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraftforge.fml.client.FMLClientHandler;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
/**
* 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();
/**
* 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();
/**
* Saves the last Key that was Pressed
*/
private static int lastKeyDown = -1;
/**
* Saves the times strg was pressed before reseting
*/
private static int strgTimesDown = 0;
private static boolean firstInWorldTick = true;
/**
* 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);
}
/**
* Private Constructor. This is a helping class!
*/
private Helper() {
// Nope.
}
}
/**
* Returns, if this is the first method call in a fresh opened world.
*
* @return True, if this is the first tick
*/
public static boolean isFirstInWorldTick() {
if (firstInWorldTick && client.theWorld != null) {
firstInWorldTick = false;
return true;
} else if (client.theWorld == null) {
firstInWorldTick = true;
}
return false;
}
/**
* Returns true, if the current Screen is a instance of GUI-Screen.
*
* @param screen
* The input Screen
* @return True, if GuiScreen; False if null
*/
public static boolean isCraftingGUI(GuiScreen screen) {
public static void printWarning() {
client.thePlayer.addChatMessage(new ChatComponentText(
"Achtung: Crafting-Keys befindet sich in der Alpha!"));
client.thePlayer.addChatMessage(new ChatComponentText(
"Es gibt noch viele Fehler, und es fehlen noch einige Funktionen."));
client.thePlayer.addChatMessage(new ChatComponentText(
"Mehr Info's findest du auf: https://github.com/0xSeb/CraftingKeys!"));
client.thePlayer.addChatMessage(new ChatComponentTranslation("key.topleft"));
}
if (screen != null) {
if (screen instanceof GuiCrafting) {
return true;
}
}
return false;
}
/**
* Standart Output for Debug-Messages - Depends on DEBUG-Constant.
*
* @param message The Debug-Message. Syntax: "methodname(): Message"
*/
public static void debugPrint(String 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;
if (DEBUG) {
System.out.println("CK-DEBUG: " + message);
}
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) {
/**
* Returns true, if the current Screen is a instance of GUI-Screen.
*
* @param screen The input Screen
* @return True, if GuiScreen; False if null
*/
public static boolean isCraftingGUI(GuiScreen screen) {
// 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;
}
}
if (screen != null) {
if (screen instanceof GuiCrafting) {
return true;
}
}
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 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;
/**
* 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;
}
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;
}
}
/**
* Reads the current Keyboard-Input and converts it to a Inventory-Slot.
*
* @return A Inventory-Slot (based on CraftingGUI), -1 for wrong input, -2
* for space
*/
public static int craftingKeyDownToSlotNumber() {
/**
* 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) {
// TODO: Make this dynamic! // Use Settings
// 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;
}
}
int returnValue = -1;
/**
* 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;
}
if (Keyboard.isKeyDown(Keyboard.KEY_Q)) {
returnValue = 1;
} else if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
returnValue = 2;
} else if (Keyboard.isKeyDown(Keyboard.KEY_E)) {
returnValue = 3;
} else if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
returnValue = 4;
} else if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
returnValue = 5;
} else if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
returnValue = 6;
} else if (Keyboard.isKeyDown(Keyboard.KEY_Y)) {
returnValue = 7;
} else if (Keyboard.isKeyDown(Keyboard.KEY_X)) {
returnValue = 8;
} else if (Keyboard.isKeyDown(Keyboard.KEY_C)) {
returnValue = 9;
} else if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
returnValue = -2;
}
/**
* 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;
}
return returnValue;
}
/**
* Reads the current Keyboard-Input and converts it to a Inventory-Slot.
*
* @return A Inventory-Slot (based on CraftingGUI), -1 for wrong input, -2
* for space
*/
public static int craftingKeyDownToSlotNumber() {
/**
* Saves the last Key that was Pressed
*/
private static int lastKeyDown = -1;
// TODO: Make this dynamic! // Use Settings
/**
* Returns, if the current KeyValue is the same
*
* @param currentKey
* the new input value, saved in the method
* @return True, if these are the same keys
*/
public static boolean isSameKey(int currentKey) {
int returnValue = -1;
boolean returnValue = false;
if (Keyboard.isKeyDown(Keyboard.KEY_Q)) {
returnValue = 1;
} else if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
returnValue = 2;
} else if (Keyboard.isKeyDown(Keyboard.KEY_E)) {
returnValue = 3;
} else if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
returnValue = 4;
} else if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
returnValue = 5;
} else if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
returnValue = 6;
} else if (Keyboard.isKeyDown(Keyboard.KEY_Y)) {
returnValue = 7;
} else if (Keyboard.isKeyDown(Keyboard.KEY_X)) {
returnValue = 8;
} else if (Keyboard.isKeyDown(Keyboard.KEY_C)) {
returnValue = 9;
} else if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
returnValue = -2;
}
if (lastKeyDown == currentKey && currentKey != -1) {
returnValue = true;
}
return returnValue;
}
lastKeyDown = currentKey;
return returnValue;
/**
* Returns, if the current KeyValue is the same
*
* @param currentKey the new input value, saved in the method
* @return True, if these are the same keys
*/
public static boolean isSameKey(int currentKey) {
}
boolean returnValue = false;
/**
* Saves the times strg was pressed before reseting
*/
private static int strgTimesDown = 0;
if (lastKeyDown == currentKey && currentKey != -1) {
returnValue = true;
}
/**
* Return the times Strg was pressed before reseting
*
* @param strgDown
* false, if reset
* @return A number of tick strg was down before
*/
public static int getStrgTimesDown(boolean strgDown) {
lastKeyDown = currentKey;
return returnValue;
if (!strgDown)
strgTimesDown = 0;
}
return ++strgTimesDown;
/**
* Return the times Strg was pressed before reseting
*
* @param strgDown false, if reset
* @return A number of tick strg was down before
*/
public static int getStrgTimesDown(boolean strgDown) {
}
if (!strgDown)
strgTimesDown = 0;
return ++strgTimesDown;
}
}
@@ -0,0 +1,45 @@
package de.skate702.craftingkeys;
import net.minecraft.client.Minecraft;
import net.minecraft.client.settings.KeyBinding;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import org.lwjgl.input.Keyboard;
/**
* Provides all Key Bindings and methods to register and check them.
*/
public class KeyBindings {
// WARNING: This does NOT work! Has to been removed. Own Option Dialog OR Settings file. Bindings are exclusive. Shit!
private static final String category = "key.categories.craftingkeys";
private static KeyBinding[] bindings = { // Inspired by CSS Position Properties
new KeyBinding("key.topleft", Keyboard.KEY_Q, category),
new KeyBinding("key.topcenter", Keyboard.KEY_W, category),
new KeyBinding("key.topright", Keyboard.KEY_E, category),
new KeyBinding("key.centerleft", Keyboard.KEY_A, category),
new KeyBinding("key.centercenter", Keyboard.KEY_S, category),
new KeyBinding("key.centerright", Keyboard.KEY_D, category),
new KeyBinding("key.lowerleft", Keyboard.KEY_Y, category), // GERMAN Keyboard layout
new KeyBinding("key.lowercenter", Keyboard.KEY_X, category),
new KeyBinding("key.lowerright", Keyboard.KEY_C, category),
new KeyBinding("key.stack", Keyboard.KEY_LSHIFT, category),
new KeyBinding("key.interact", Keyboard.KEY_LCONTROL, category),
};
/**
* Initializes all key bindings and registers them to the client-side.
*/
public static void init() {
for (KeyBinding binding : bindings) {
ClientRegistry.registerKeyBinding(binding);
}
ClientRegistry.registerKeyBinding(Minecraft.getMinecraft().gameSettings.keyBindRight);
//Minecraft.getMinecraft().gameSettings.keybind
}
}
@@ -1,9 +1,14 @@
package de.skate702.craftingkeys;
package de.skate702.craftingkeys.manager;
import de.skate702.craftingkeys.CraftingKeys;
import de.skate702.craftingkeys.Helper;
import net.minecraft.client.gui.inventory.GuiCrafting;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraft.item.Item;
import net.minecraft.block.Block;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.IChatComponent;
@@ -215,6 +220,9 @@ public class ContainerManager {
}
}
GameRegistry.addShapelessRecipe(new ItemStack(Item.getByNameOrId("Diamond")),
new ItemStack(Block.getBlockFromName("Dirt")));
for (int i = 10; i < container.inventorySlots.size(); i++) {
if (getItemStack(i) == null) {
@@ -0,0 +1,14 @@
?# Control Names
key.topleft=1. Top Left
key.topcenter=2. Top Center
key.topright=3. Top Right
key.centerleft=4. Center Left
key.centercenter=5. Center Center
key.centerright=6. Center Right
key.lowerleft=7. Lower Left
key.lowercenter=8. Lower Center
key.lowerright=9. Lower Right
key.stack=Stack Key
key.interact=Interaction Key
# Control Category Name
key.categories.craftingkeys=Crafting Keys (Mostly red Keys are NO problem!)
+1 -1
View File
@@ -1,6 +1,6 @@
[
{
"modid": "CraftingKeysID",
"modid": "craftingkeys",
"name": "Crafting Keys",
"description": "TODO",
"version": "${version}",