Jump to content

[1.7.10] Trying to debug my Gravity boots


JaselUmena

Recommended Posts

Hi all,

 

One friend of mine and me are developing a simple mod (which is starting to grow quite rapidly) for us to play with friends and random people on a small private server with cauldron (plugins and all that stuff). We use a quite big modpack featuring 92 mods and our very own mod. However, this in not our problem:

 

Until now, our mod has featured one ore (around which revolves the rest of the mod), a couple of armor sets and weapons (with the names of our friends on the server) which give potion effects and such. However, we recently took a step forward and tried to implement two things:

 

- A "ninja" armor set which gives invisibility and hunger when sneaking and enables jumping on air. This jump works with all the other mods in single player.

 

 

package com.colorescraftmod.principal.items;

 

import net.minecraft.client.Minecraft;

import net.minecraft.entity.Entity;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.EntityLivingBase;

import net.minecraft.item.ItemArmor;

import net.minecraft.item.ItemStack;

import net.minecraft.item.ItemArmor.ArmorMaterial;

import net.minecraft.potion.Potion;

import net.minecraft.potion.PotionEffect;

import net.minecraft.util.MathHelper;

import net.minecraft.world.World;

import net.minecraftforge.common.util.EnumHelper;

 

import com.colorescraftmod.principal.colorescraftmod;

import com.colorescraftmod.principal.help.Reference;

 

public class ninjaarmor extends ItemArmor {

 

public String textureName;

public static ArmorMaterial ninjaarmor = EnumHelper.addArmorMaterial("ninjaarmor", 7, new int[] {2, 6, 5, 2}, 9);

 

public ninjaarmor(String unlocalizedName, ArmorMaterial material, String textureName, int type) {

super(material, 0, type);

this.textureName = textureName;

this.setUnlocalizedName(unlocalizedName);

this.setTextureName(Reference.MODID + ":" + unlocalizedName);

this.setCreativeTab(colorescraftmod.colorescraftmod);//en que pestanya va

}

 

@Override

public String getArmorTexture(ItemStack stack, Entity entity, int slot, String type)

  {

      if(((EntityLivingBase) entity).getActivePotionEffect(Potion.invisibility) == null)

      {

          return Reference.MODID + ":textures/armor/" + this.textureName + "_" + (this.armorType == 2 ? "2" : "1") + ".png";

      }

      else

      {

      return Reference.MODID + ":textures/armor/" + this.textureName + "inv_" + (this.armorType == 2 ? "2" : "1") + ".png";

       

      }

  }

 

@Override

public void onArmorTick(World world, EntityPlayer player, ItemStack itemStack) {

 

if (player.inventory.armorItemInSlot(3) != null && player.inventory.armorItemInSlot(3).getItem() == ModItems.ninjaHelmet

            && player.inventory.armorItemInSlot(2) != null && player.inventory.armorItemInSlot(2).getItem() == ModItems.ninjaplate

            && player.inventory.armorItemInSlot(1) != null && player.inventory.armorItemInSlot(1).getItem() == ModItems.ninjaLeggings

            && player.inventory.armorItemInSlot(0) != null && player.inventory.armorItemInSlot(0).getItem() == ModItems.ninjaBoots) {

 

player.fallDistance = 0.0F;

  Minecraft mc=Minecraft.getMinecraft();

        if(mc.gameSettings.keyBindJump.isPressed()){

        player.motionY += 0.5; 

       

        }

       

        if(player.isSneaking()){

        effectPlayer(player, Potion.hunger, 1, 40);

    effectPlayer(player, Potion.invisibility, 1, 40);

        }

       

    }

}

 

 

 

 

 

private void effectPlayer(EntityPlayer player, Potion potion, int amplifier,  int time) {

    if (player.getActivePotionEffect(potion) == null || player.getActivePotionEffect(potion).getDuration() <= 1)

        player.addPotionEffect(new PotionEffect(potion.id, time, amplifier, true));

}

 

 

}

 

 

- "Gravity boots" which imitate the funcionality of a simple jetpack (you press space and it gives you motion upwards).

 

 

package com.colorescraftmod.principal.items;

 

import net.minecraft.entity.Entity;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.EntityLivingBase;

import net.minecraft.item.ItemArmor;

import net.minecraft.item.ItemStack;

import net.minecraft.item.ItemArmor.ArmorMaterial;

import net.minecraft.potion.Potion;

import net.minecraft.potion.PotionEffect;

import net.minecraft.util.MathHelper;

import net.minecraft.world.World;

import net.minecraftforge.common.util.EnumHelper;

 

import org.lwjgl.input.Keyboard;

 

import com.colorescraftmod.principal.colorescraftmod;

import com.colorescraftmod.principal.help.Reference;

 

public class agravedad extends ItemArmor {

 

public String textureName;

public static ArmorMaterial agravedad = EnumHelper.addArmorMaterial("agravedad", -1, new int[] {1, 3, 2, 1}, 15);

 

public agravedad(String unlocalizedName, ArmorMaterial material, String textureName, int type) {

super(material, 0, type);

this.textureName = textureName;

this.setUnlocalizedName(unlocalizedName);

this.setTextureName(Reference.MODID + ":" + unlocalizedName);

this.setCreativeTab(colorescraftmod.colorescraftmod);//en que pestanya va

}

 

@Override

public String getArmorTexture(ItemStack stack, Entity entity, int slot, String type)

  {

          return Reference.MODID + ":textures/armor/" + this.textureName + "_" + (this.armorType == 2 ? "2" : "1") + ".png";

  }

 

@Override

public void onArmorTick(World world, EntityPlayer player, ItemStack itemStack) {

 

 

    if (player.inventory.armorItemInSlot(0) != null && player.inventory.armorItemInSlot(0).getItem() == ModItems.agravedadBoots)

    {

     

    if(Keyboard.isKeyDown(57)){

        player.motionY += 0.1;

        player.fallDistance = 0.0F;

        }

   

    }

}

 

 

 

 

 

private void effectPlayer(EntityPlayer player, Potion potion, int amplifier,  int time) {

    if (player.getActivePotionEffect(potion) == null || player.getActivePotionEffect(potion).getDuration() <= 1)

        player.addPotionEffect(new PotionEffect(potion.id, time, amplifier, true));

}

 

 

}

 

 

However, for unknown reasons, whenever someone weats these sets on the server, they get kicked until we delete their inventories. We have banned part of the ninja set and the gravity boots until we get to fix it. I included the code of both items in the spoilers. Please help us ;w; they work in single player but for some reason we get a error when using it on the cauldron server.

Link to comment
Share on other sites

This line right here is your problem :

if(Keyboard.isKeyDown(57)){
              player.motionY += 0.1;
              player.fallDistance = 0.0F;

 

Also this line in the ninja suit:

Minecraft mc=Minecraft.getMinecraft();
              if(mc.gameSettings.keyBindJump.isPressed()){
                 player.motionY += 0.5;  

 

Any and all things to do with the keyboard have to be checked client side, the result of which then has to be sent to the sever using a packet handling system. If client side code is called on the server ie checking if a keyboard key is being pressed it will constantly crash you. Not only that but Minecraft.getMinecraft() only returns the client minecraft instance and not the server one and should never be used in anything that is not rendering gui's or things of the like. I recommend checking out these tutorials and implementing them in your mod.

http://www.minecraftforge.net/forum/index.php/topic,20135.0.html

http://www.minecraftforum.net/forums/mapping-and-modding/mapping-and-modding-tutorials/2137055-1-7-x-1-8-customizing-packet-handling-with

 

Hope this helps

Link to comment
Share on other sites

We are on it right now... we are not the best coders in the world (specially me, I'm in the project just because of the item skins, models and such), but we are trying to implement the Simple Network thing. The main concern my friend has is to change from a chat message to the armor and all the complexity it implies. Thank you for your offer, though, that's very kind of you and I will no doubt message you in case we don't succeed.

Link to comment
Share on other sites

Okay, so it seems we are on a dead end. The one of us who's doing the code does not know where to place what parts of the code and he went to bed  (it's 6am here) and left me these 3 codes. He told me he doesn't know how to continue and that he would really appreciate an example of a mod that adds an item that does something when the user presses a key (idk like summon a fireball with motion 0 or something when the item is in the player's hand) so he can reverse engineer the code.

 

For those who are wondering why am I writing and not him is because he does not understand English very well and he thought I would be able to seek help more easily. Thank you in advance for any tips, I leave here the same three files he sent me:

 

ninjaarmor.java

 

package com.colorescraftmod.principal.items;

 

import net.minecraft.client.Minecraft;

import net.minecraft.entity.Entity;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.EntityLivingBase;

import net.minecraft.item.ItemArmor;

import net.minecraft.item.ItemStack;

import net.minecraft.item.ItemArmor.ArmorMaterial;

import net.minecraft.potion.Potion;

import net.minecraft.potion.PotionEffect;

import net.minecraft.util.MathHelper;

import net.minecraft.world.World;

import net.minecraftforge.common.util.EnumHelper;

 

import com.colorescraftmod.principal.colorescraftmod;

import com.colorescraftmod.principal.help.ModNetwork;

import com.colorescraftmod.principal.help.PlayerActionPacket;

import com.colorescraftmod.principal.help.Reference;

 

public class ninjaarmor extends ItemArmor {

 

public String textureName;

public static ArmorMaterial ninjaarmor = EnumHelper.addArmorMaterial("ninjaarmor", 7, new int[] {2, 6, 5, 2}, 9);

 

public ninjaarmor(String unlocalizedName, ArmorMaterial material, String textureName, int type) {

super(material, 0, type);

this.textureName = textureName;

this.setUnlocalizedName(unlocalizedName);

this.setTextureName(Reference.MODID + ":" + unlocalizedName);

this.setCreativeTab(colorescraftmod.colorescraftmod);

}

 

@Override

public String getArmorTexture(ItemStack stack, Entity entity, int slot, String type)

  {

      if(((EntityLivingBase) entity).getActivePotionEffect(Potion.invisibility) == null)

      {

          return Reference.MODID + ":textures/armor/" + this.textureName + "_" + (this.armorType == 2 ? "2" : "1") + ".png";

      }

      else

      {

      return Reference.MODID + ":textures/armor/" + this.textureName + "inv_" + (this.armorType == 2 ? "2" : "1") + ".png";

       

      }

  }

 

@Override

public void onArmorTick(World world, EntityPlayer player, ItemStack itemStack) {

 

if (player.inventory.armorItemInSlot(3) != null && player.inventory.armorItemInSlot(3).getItem() == ModItems.ninjaHelmet

            && player.inventory.armorItemInSlot(2) != null && player.inventory.armorItemInSlot(2).getItem() == ModItems.ninjaplate

            && player.inventory.armorItemInSlot(1) != null && player.inventory.armorItemInSlot(1).getItem() == ModItems.ninjaLeggings

            && player.inventory.armorItemInSlot(0) != null && player.inventory.armorItemInSlot(0).getItem() == ModItems.ninjaBoots) {

 

player.fallDistance = 0.0F;

 

        if (world.isRemote)

            {

                if (Minecraft.getMinecraft().gameSettings.keyBindJump.getIsKeyPressed())

                {

                ModNetwork.net.sendToServer(new PlayerActionPacket.ActionMessage(PlayerActionPacket.ninja));

                }

            }

        if(player.isSneaking()){

        effectPlayer(player, Potion.hunger, 1, 40);

    effectPlayer(player, Potion.invisibility, 1, 40);

        }

       

    }

}

 

 

 

 

 

private void effectPlayer(EntityPlayer player, Potion potion, int amplifier,  int time) {

 

    if (player.getActivePotionEffect(potion) == null || player.getActivePotionEffect(potion).getDuration() <= 1)

        player.addPotionEffect(new PotionEffect(potion.id, time, amplifier, true));

}

 

 

}

 

 

ModNetwork.java

 

package com.colorescraftmod.principal.help;

 

import cpw.mods.fml.common.network.NetworkRegistry;

import cpw.mods.fml.common.network.simpleimpl.IMessage;

import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;

import cpw.mods.fml.relauncher.Side;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.world.WorldServer;

 

 

public class ModNetwork

{

    public static SimpleNetworkWrapper net;

    public static int messages = 0;

 

    public static void init()

    {

        net = NetworkRegistry.INSTANCE.newSimpleChannel(Reference.MOD_CHANNEL);

 

        registerMessage(PlayerActionPacket.class, PlayerActionPacket.ActionMessage.class);

 

    }

 

    @SuppressWarnings("unchecked")

    public static void registerClientSide(Class handler, Class message)

    {

        net.registerMessage(handler, message, messages, Side.CLIENT);

        messages++;

    }

 

    @SuppressWarnings("unchecked")

    private static void registerMessage(Class handler, Class message)

    {

        net.registerMessage(handler, message, messages, Side.CLIENT);

        net.registerMessage(handler, message, messages, Side.SERVER);

        messages++;

    }

 

    public static void sendToNearby(IMessage message, EntityPlayer player)

    {

        if(player!=null && player.worldObj instanceof WorldServer)

        {

            try

            {

                ((WorldServer) player.worldObj).getEntityTracker().func_151248_b(player, ModNetwork.net.getPacketFrom(message));

            } catch (Exception ex)

            {

                ex.printStackTrace();

            }

        }

    }

 

    public static void sendToDimension(IMessage message, EntityPlayer player)

    {

        net.sendToDimension(message, player.dimension);

    }

}

 

 

PlayerActionPacket.java

 

package com.colorescraftmod.principal.help;

 

 

import cpw.mods.fml.common.network.simpleimpl.IMessage;

import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;

import cpw.mods.fml.common.network.simpleimpl.MessageContext;

import io.netty.buffer.ByteBuf;

 

 

public class PlayerActionPacket implements IMessageHandler<PlayerActionPacket.ActionMessage, IMessage>

{

    public static final byte ninja = 0;

    public static final byte gravedad = 1;

 

    @Override

    public IMessage onMessage(ActionMessage message, MessageContext ctx)

    {

        if (ctx.side.isServer())

        {

           

 

            if(message.type == gravedad)

            {

 

            }

 

        }

        return null;

    }

 

    public static class ActionMessage implements IMessage

    {

 

        private byte type;

 

        public ActionMessage()

        {

        }

 

        public ActionMessage(byte type)

        {

            this.type = type;

 

        }

 

        @Override

        public void fromBytes(ByteBuf buf)

        {

            this.type = buf.readByte();

        }

 

        @Override

        public void toBytes(ByteBuf buf)

        {

            buf.writeByte(type);

        }

    }

}

 

 

I don't quite understand code, but this seems quite empty and I suspect that he was following a tutorial or something. We are trying and once we learn how to do the handler properly, I think our mod will progress a lot so any help is more than welcome (maybe a small reward too if it's a lot of trouble and a thank you and eternal gratitude  is not worth it ;w;).

Link to comment
Share on other sites

Here is my (probably poor) implementation of a jetpack like item.

 

First in the preInit of your main mod file you need to register all packets so that the forge network code will recognize them.

 

https://github.com/Sudwood/AdvancedUtilities/blob/master/java/com/sudwood/advancedutilities/AdvancedUtilities.java#L158

 

In order to check if a key is pressed I use a ForgeModLoader Event handler which is registered in the main mod file

 

https://github.com/Sudwood/AdvancedUtilities/blob/master/java/com/sudwood/advancedutilities/AdvancedUtilities.java#L168

https://github.com/Sudwood/AdvancedUtilities/blob/master/java/com/sudwood/advancedutilities/AUFMLEventHandler.java

 

The event handler checks if a key is pressed and if it is sends a packet to the server with information from the extended player as I save whether the jetpack should be on in the player data itself

https://github.com/Sudwood/AdvancedUtilities/blob/master/java/com/sudwood/advancedutilities/ExtendedPlayer.java

https://github.com/coolAlias/Forge_Tutorials/blob/master/IExtendedEntityPropertiesTutorial.java

 

The packet is then sent to the sever where it saves the extended player data about the jetpack

https://github.com/Sudwood/AdvancedUtilities/blob/master/java/com/sudwood/advancedutilities/packets/PacketJetpack.java

 

Then in a player tick event in the ForgeModLoader Event Handler I check if the jetpack is equiped and the player property props.isJetpack is true

Then if the jetpack has power and all that and is okay for use I make sure to set allow flight on the server to be on as it will kick players that use it if it is false

Finally I apply the added velocity to the player and consume energy from the jetpack

https://github.com/Sudwood/AdvancedUtilities/blob/master/java/com/sudwood/advancedutilities/AUFMLEventHandler.java#L107

 

Finally in a client tick event in the ForgeModLoader Event Handler I check if the jump key is not pressed and if so send another packetJetpack to the server to tell it to stop boosting the player

https://github.com/Sudwood/AdvancedUtilities/blob/master/java/com/sudwood/advancedutilities/AUFMLEventHandler.java#L74

 

Hope this helps feel free to look through any of my mod's code if you need help on anything else.

If more explanation is needed (I am bad at explaining) then feel free to ask for more help. :)

 

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.