Jump to content

Jetpack-like script/mod


DeltaTimo

Recommended Posts

Hey there!

I'm pretty new to modding in minecraft ( I have some experience with lua ):

I'm trying to make a Jetpack like mod/script which makes my player fly up (addVelocity 0,0.075,0) which worked pretty good using a keybind.

Problem is when I fall down, or jump of a tree and thrust up so I land on the ground with almost no speed I still get tons of damage.

I tried setting fallDistance to 0 while I hold the key and when I release the key which had (I think) no effect. I've set fallDistance clientside, which is in my guess the problem. But since I just started modding and have no idea (and searched a long while to find out how to do binds) I don't know how to set it serverside.

 

I'm just gonna give you the code I wrote in the proxy:

 

Base File: chatbinds.common.Chatbinds.java

package chatbinds.common;

import net.minecraft.src.BaseMod;
import net.minecraft.src.ModLoader;
import cpw.mods.fml.client.registry.KeyBindingRegistry;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;


@Mod(modid = "dt855_chatbinds", name = "Chatbinds", version = "0.1")
@NetworkMod(clientSideRequired = true, serverSideRequired = false)

public class Chatbinds
{
@SidedProxy(clientSide="chatbinds.client.ClientProxy", serverSide="chatbinds.common.CommonProxy")
public static CommonProxy proxy;

@Init
public void load(FMLInitializationEvent event)
{
	proxy.registerKeybinds();
}
}

 

chatbinds.client.ClientProxy.java

package chatbinds.client;

import cpw.mods.fml.client.registry.KeyBindingRegistry;
import net.minecraft.client.settings.KeyBinding;
import chatbinds.common.CommonProxy;

public class ClientProxy extends CommonProxy
{
@Override
public void registerKeybinds()
{
	KeyBindingRegistry.registerKeyBinding(new MyKeyHandlerFly());
}
}

 

chatbinds.client.MyKeyHandlerFly.java

package chatbinds.client;

import java.util.EnumSet;

import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.src.ModLoader;

import org.lwjgl.input.Keyboard;

import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.client.registry.KeyBindingRegistry.KeyHandler;
import cpw.mods.fml.common.TickType;

public class MyKeyHandlerFly extends KeyHandler {
static KeyBinding myBinding = new KeyBinding("Jetpack", Keyboard.KEY_T);

    public MyKeyHandlerFly() {
            //the first value is an array of KeyBindings, the second is whether or not the call 
            //keyDown should repeat as long as the key is down
            super(new KeyBinding[]{myBinding}, new boolean[]{true});
    }

    @Override
    public String getLabel() {
            return "mykeybindings";
    }

    @Override
    public void keyDown(EnumSet<TickType> types, KeyBinding kb,
                    boolean tickEnd, boolean isRepeat) {
    		if (FMLClientHandler.instance().getClient().currentScreen == null) {
    			EntityClientPlayerMP localPlayer = ModLoader.getMinecraftInstance().thePlayer;
        		localPlayer.addVelocity(0,0.075,0);
        		localPlayer.fallDistance = 0;
    		}
    }

    @Override
    public void keyUp(EnumSet<TickType> types, KeyBinding kb, boolean tickEnd) {
            //do whatever
    		EntityClientPlayerMP localPlayer = ModLoader.getMinecraftInstance().thePlayer;
    		localPlayer.fallDistance = 0;
    }

    @Override
    public EnumSet<TickType> ticks() {
            return EnumSet.of(TickType.CLIENT);
            //I am unsure if any different TickTypes have any different effects.
    }
}

 

Serverside Proxy: chatbinds.common.CommonProxy.java

package chatbinds.common;

import chatbinds.client.MyKeyHandler;
import cpw.mods.fml.client.registry.KeyBindingRegistry;

public class CommonProxy {
public void registerKeybinds()
{

}
}

 

P.S.: I have no idea what I'm doing, I followed some tutorials and searched around the web to find parts and put everything together. So if you have a solution for my problem I'd be very appreciated if you could describe what the code means.

Link to comment
Share on other sites

not how i'd do it, personally i'd use

onArmortTickUpdate(parameters which i cant remember)

in the item class and have

if(Keyboard.isKeyDown(Keyboard.KEY_SPACE))
{
	//flight code
}

or do a tick handler of type player, which is called in the common and client proxys using

TickRegistry.registerTickHandler(handler, side)

side being Side.CLIENT when called in the client proxy and Side.SERVER in the common proxy. and having the flight code in the on tick start method

(wrighting this from memory, so please excuse any errors,

any trouble and i'll be willing to help further

Link to comment
Share on other sites

you can also use servertick, eg

package modname.common;

import java.util.EnumSet;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import cpw.mods.fml.common.ITickHandler;
import cpw.mods.fml.common.TickType;

public class ServerTickHandler implements ITickHandler {

private void onPlayerTick(EntityPlayer player) {
	if (player.getCurrentItemOrArmor(4) != null ) {
		ItemStack helmet = player.getCurrentItemOrArmor(4);
		ItemStack chest = player.getCurrentItemOrArmor(3);
		ItemStack legs = player.getCurrentItemOrArmor(2);

		if (plate.getItem() == modname.platename ) {
			player.fallDistance = 0;

		}
	}	
}

@Override
public void tickStart(EnumSet<TickType> type, Object... tickData)
{
  if (type.equals(EnumSet.of(TickType.PLAYER)))
  {
    onPlayerTick((EntityPlayer)tickData[0]);
  }
}

@Override
public EnumSet<TickType> ticks() 
{
  return EnumSet.of(TickType.PLAYER, TickType.SERVER);
}

@Override
public void tickEnd(EnumSet<TickType> type, Object... tickData) {
}


@Override
public String getLabel() {
	return null;
}

}

Use examples, i have aspergers.

Examples make sense to me.

Link to comment
Share on other sites

you can also use servertick

 

from my recollection of doing something like this in 1.4.7 it needs to be called on client side as well, so create a tick handler like ashtonr12 has shown, but

@Override
public EnumSet<TickType> ticks() 
{
  return EnumSet.of(TickType.PLAYER, TickType.SERVER);
}

 

should be

@Override
public EnumSet<TickType> ticks() 
{
  return EnumSet.of(TickType.PLAYER);
}

 

and then call it in both the client and common proxys under the respective sides for each. if you don't the client and server desynchronise because that the server thinks the player is flying but the client doesn't meaning the desired effect is not achieved.

 

not tried this since 1.4.7 though so things may have changed but i think it's unlikely give the nature of it

Link to comment
Share on other sites

do the following

 

in your common proxy

    public void registerHandlers()
    {
    	TickRegistry.registerTickHandler(new PlayerTickHandler(), Side.SERVER);
    }

 

in your client proxy

    @Override
    public void registerHandlers()
    {
    	TickRegistry.registerTickHandler(new PlayerTickHandler(), Side.CLIENT);
    }

 

PlayerTickHandler.java

public class PlayerTickHandler implements ITickHandler
{

@Override
public void tickStart(EnumSet<TickType> type, Object... tickData)
{
                // can't remember but you may need a null check here for the tickData[0]
	onPlayerTick((EntityPlayer)tickData[0]);
}

@Override
public void tickEnd(EnumSet<TickType> type, Object... tickData)
{

}

@Override
public EnumSet<TickType> ticks()
{
	return EnumSet.of(TickType.PLAYER);
}

@Override
public String getLabel()
{
	return "PlayerTickHandler";
}

public static void onPlayerTick(EntityPlayer player)
{
	//code for player to be called each tick such as "if(Keyboard.isKeyPressed(Keyboard.KEY_SPACE)) player.motionY += 0.09"
                //FYI 0.09 seems to be a fairly good value for this from my experience
}
}

 

finaly in your init method in your mod file do

proxy.registerHandlers();

 

[EDIT] i'd also suggest learning java properly

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.