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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • One of my players is suddenly unable to join a locally hosted MC Eternal server. We have been playing on this server for about 2-3 weeks now. I have tried erasing his player files and his reputation file, and now it just coughs up this and kicks him out: [User Authenticator #5/INFO] [minecraft/NetHandlerLoginServer]: UUID of player EthosTheGod is 7692d8db-02c3-424f-a4ab-0e4e259b106b [20:25:36] [User Authenticator #4/INFO] [minecraft/NetHandlerLoginServer]: UUID of player EthosTheGod is 7692d8db-02c3-424f-a4ab-0e4e259b106b [20:29:35] [Server thread/WARN] [minecraft/MinecraftServer]: Can't keep up! Did the system time change, or is the server overloaded? Running 575849ms behind, skipping 11516 tick(s) [20:29:35] [Server thread/INFO] [minecraft/NetHandlerLoginServer]: com.mojang.authlib.GameProfile@4a6c63f1[id=7692d8db-02c3-424f-a4ab-0e4e259b106b,name=EthosTheGod,properties={textures=[com.mojang.authlib.properties.Property@241ea89e]},legacy=false] (/IP.ADDRESS) lost connection: Disconnected [20:29:35] [Server thread/INFO] [minecraft/NetHandlerLoginServer]: com.mojang.authlib.GameProfile@6ab6c661[id=7692d8db-02c3-424f-a4ab-0e4e259b106b,name=EthosTheGod,properties={textures=[com.mojang.authlib.properties.Property@7f19aae3]},legacy=false] (/IP.ADDRESS) lost connection: Disconnected It just says "connection timed out" on his end. Any ideas?
    • I'm trying to migrate my mod from 1.20 to 1.21. Some packages in the forge api were changed so my mod did have some classes not working. I've changed everything i needed but still is getting me the following error error: cannot access Registry DeferredRegister.create(ForgeRegistries.BLOCKS, FarmMod.MOD_ID); ^ class file for net.minecraft.core.Registry not found The piece of code that is wrong is   public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, FarmMod.MOD_ID); And here are my imports   import com.lucas.farmmod.FarmMod; import com.lucas.farmmod.block.custom.BaseIrrigatorBlock; import com.lucas.farmmod.item.ModItems; import com.lucas.farmmod.item.custom.BaseIrrigatorBlockItem; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.Item; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.registries.DeferredRegister; import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.RegistryObject; The class DeferredRegister is throwing the error in the print below     I've tried running rebuilding my project in every way possible, tried refreshing my dependencies but nothing works. What can i do?
    • It sounds like there might be a synchronization issue with your PartEntity. Ensure that the part entity’s position is updated in your entity's tick method to continuously match the main entity’s location.
    • For keyboard and mouse inputs, Minecraft Forge utilizes the event system to manage interactions, making it easier to handle across different mods. If you’re looking to bypass this and read inputs directly, you’d typically look into the KeyboardListener and MouseListener classes in the game's code. These classes process input events directly from the user's hardware. If you're experimenting with inputs a lot, you might find a compact keypad handy for quick commands. Check out numeric keyboard . It can speed up your coding workflow! Good luck!
  • Topics

×
×
  • Create New...

Important Information

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