Everything posted by Angel_Valis
-
Placing blocks from inventory with a key press.
Hello all, I'm trying my hand at something pretty simple as an introduction to modding Minecraft, and I'm pretty well stuck. I want to be able to place torches from my inventory without needing them to be in my hotbar by pressing a key. I have the key binding class set up just fine and it runs any code just fine. My problem is that I can't figure out how to actually place anything. I've found a few methods that looks like they would work, but they all seem to require coordinates and such that I have no clue how to obtain. Here's my code so far including something I thought might work, but literally does nothing. Main mod class: package angel_valis.easyTorch; import java.util.EnumSet; import org.lwjgl.input.Keyboard; import net.minecraft.client.settings.KeyBinding; import cpw.mods.fml.client.registry.KeyBindingRegistry; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.Mod.Init; import cpw.mods.fml.common.Mod.Instance; import cpw.mods.fml.common.Mod.PostInit; import cpw.mods.fml.common.Mod.PreInit; import cpw.mods.fml.common.SidedProxy; import cpw.mods.fml.common.TickType; 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.network.NetworkMod; import cpw.mods.fml.common.registry.TickRegistry; import cpw.mods.fml.relauncher.Side; @Mod(modid="EasyT", name="Easy Torch", version="1.0.0") @NetworkMod(clientSideRequired=true, serverSideRequired=false) public class EasyTorch { // The instance of your mod that Forge uses. @Instance("EasyTorch") public static EasyTorch instance; // Says where the client and server 'proxy' code is loaded. @SidedProxy(clientSide="angel_valis.easyTorch.client.ClientProxy", serverSide="angel_valis.easyTorch.CommonProxy") public static CommonProxy proxy; @EventHandler public void preInit(FMLPreInitializationEvent event) { // Stub Method } @EventHandler public void load(FMLInitializationEvent event) { proxy.registerRenderers(); TickRegistry.registerTickHandler(new EasyTPlayerTickHandler(EnumSet.of(TickType.PLAYER)), Side.SERVER); KeyBinding[] key = {new KeyBinding("Torch", Keyboard.KEY_R)}; boolean[] repeat = {false}; KeyBindingRegistry.registerKeyBinding(new EasyTKeyHandler(key, repeat)); } @EventHandler public void postInit(FMLPostInitializationEvent event) { // Stub Method } } Key handler class: package angel_valis.easyTorch; import java.util.EnumSet; import net.minecraft.client.settings.KeyBinding; import cpw.mods.fml.client.registry.KeyBindingRegistry.KeyHandler; import cpw.mods.fml.common.TickType; public class EasyTKeyHandler extends KeyHandler { private EnumSet tickTypes = EnumSet.of(TickType.PLAYER); private static boolean keyPressed = false; public EasyTKeyHandler(KeyBinding[] keyBindings, boolean[] repeatings) { super(keyBindings, repeatings); // TODO Auto-generated constructor stub } @Override public String getLabel() { // TODO Auto-generated method stub return "EasyT Keybindings"; } public static boolean getPressed(){ return keyPressed; } @Override public void keyDown(EnumSet<TickType> types, KeyBinding kb, boolean tickEnd, boolean isRepeat) { // TODO Auto-generated method stub keyPressed = true; } @Override public void keyUp(EnumSet<TickType> types, KeyBinding kb, boolean tickEnd) { // TODO Auto-generated method stub keyPressed = false; } @Override public EnumSet<TickType> ticks() { // TODO Auto-generated method stub return tickTypes; } } A handler class that just serves to wrap up some functions: package angel_valis.easyTorch; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; public class TorchHandler { private static int slotNum = -1; public static boolean hasTorch(EntityPlayer player){ for (int x = 0; x < player.inventory.mainInventory.length; ++x) { if (player.inventory.mainInventory[x] != null && player.inventory.mainInventory[x].itemID == Block.torchWood.blockID) { slotNum = x; return true; } } slotNum = -1; return false; } public int getSlot(){ return slotNum; } } And finally the tick handler and the code that doesn't work at all: package angel_valis.easyTorch; import java.util.EnumSet; import angel_valis.easyTorch.TorchHandler; import angel_valis.easyTorch.EasyTKeyHandler; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.MovingObjectPosition; import net.minecraft.world.World; import cpw.mods.fml.common.ITickHandler; import cpw.mods.fml.common.TickType; public class EasyTPlayerTickHandler implements ITickHandler { private final EnumSet<TickType> ticksToGet; private static TorchHandler tH = new TorchHandler(); /* * This Tick Handler will fire for whatever TickType's you construct and register it with. */ public EasyTPlayerTickHandler(EnumSet<TickType> ticksToGet) { this.ticksToGet = ticksToGet; } /* * I suggest putting all your tick Logic in EITHER of these, but staying in one */ @Override public void tickStart(EnumSet<TickType> type, Object... tickData) { playerTick((EntityPlayer)tickData[0]); } @Override public void tickEnd(EnumSet<TickType> type, Object... tickData) { } @Override public EnumSet<TickType> ticks() { return ticksToGet; } @Override public String getLabel() { return "EasyTTickHandler"; } public void playerTick(EntityPlayer player) { if(EasyTKeyHandler.getPressed()){ if (tH.hasTorch(player) && tH.getSlot() != -1){ player.worldObj.setBlock((int)player.getLookVec().xCoord, (int)player.getLookVec().yCoord, (int)player.getLookVec().zCoord, player.inventory.mainInventory[tH.getSlot()].itemID); } } } } Any help would be appreciated.
IPS spam blocked by CleanTalk.