Jump to content

[SOLVED] Problems With Tick Handlers (Casting World to Object[])


Recommended Posts

Posted

Being the insightful Google-er I am, I researched how to save NBT due to the various answers. After coming across this topic, I followed the final post... to find that there was an unresolved issue. He was casting World to Object[].

 

I was just wondering if there was a way to cast a class to Object[]. I researched why this error was occurring and only found people trying to cast Object[] to classes. BEFORE you start saying "LEARN JAVA!", I really don't want to take months of learning everything to learn the answer to this simple issue.

 

(I don't feel the need to post the stack trace because it is only talking about the lines that about World and tickData.)

 

(P.S. I hope I was clear enough for people to understand.)

-Mitchellbrine

 

Minecraft can do ANYTHING, it's coded in Java and you got the full power of Java behind you when you code. So nothing is impossible.

It may be freaking fucking hard though, but still possible ;)

 

If you create a topic on Modder Support, live by this motto:

I don't want your charity, I want your information
Posted

Thank you for the reply and I'll look into TickType.

 

Regarding the "Learn java!", when I started modding, I knew jack squat about Java. I've learned it over my time of modding and have learned QUITE A BIT of Java. Enough to mod, that is.

-Mitchellbrine

 

Minecraft can do ANYTHING, it's coded in Java and you got the full power of Java behind you when you code. So nothing is impossible.

It may be freaking fucking hard though, but still possible ;)

 

If you create a topic on Modder Support, live by this motto:

I don't want your charity, I want your information
Posted

How much other programming experience did you have prior, though?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

Well... um... to be perfectly honest with you...

 

None. After modding my first time (which was a failed mod using ModLoader), I started to take a look at code to see how things could be done (and of course used Forge, which I found way easier and efficient) and started to learn bits and pieces of Java from there.

-Mitchellbrine

 

Minecraft can do ANYTHING, it's coded in Java and you got the full power of Java behind you when you code. So nothing is impossible.

It may be freaking fucking hard though, but still possible ;)

 

If you create a topic on Modder Support, live by this motto:

I don't want your charity, I want your information
Posted

Ah ha.

 

See, I knew jack about Java when I started, but I was coming from a programming background.  So all of my questions have either been highly technical, dealing with a section of Forge/vanilla I'm not familiar with, or deep math (as I'm not so good at designing algorithms).

 

You're more in the dark, so when you run into posts like the one in your OP, you don't grasp the solution as readily.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

Not sure if this exactly helps, but if you want to add a cast for World (and EntityPlayer) within a server Tick Handler, I've found this method effective:

 

ArrayList list = (ArrayList) MinecraftServer.getServer().getConfigurationManager().playerEntityList;
Iterator iterator = list.iterator();

while (iterator.hasNext())
{
    EntityPlayerMP player = (EntityPlayerMP) iterator.next();
    WorldServer world = MinecraftServer.getServer().worldServerForDimension(player.dimension);
    // Add your stuff in here...
}

If this doesn't help you, maybe this will help someone else. This is just what I do for casting the World object in my ServerTickHandlers. Ciao!

Posted

@diesieben07

 

It's not bull if it works. I do not see anything wrong with it at all. Alternatively, you could just send it to the Client Side by creating a separate TickHandler. If I did not answer the original question, please explain to me why instead of just relating my methods to mere garbage.

Posted

Okay, I see what you're saying. However, there is no need to be snarky. I was just stating how I did it (and looking back at it, I would have used the worldObj, but this was some old code from my GitHub page that I used). I welcome your new information --I enjoy improving as a coder. I am certainly (by a great magnitude) not the best programmer, nor do I claim to be; I just saw a person here who needed an answer, and I gave the only one I knew. Thank you for all the work you do for the Modding Support. Have a great evening (at least, it is here). :)

Posted

When I started modding, I knew jack squat about Java. I've learned it over my time of modding and have learned QUITE A BIT of Java. Enough to mod, that is.

 

The thing is that you will be faster and better if you learn java first, then get into modding.

Now you get stuck on simple things, you use "forever" to code things which after spending some time studying java you would be able to pull of in no time. Not to mention the fact that you will be able to read the harder part of the minecraft src easier, you will be able to comprehend answers to more advance problems you see around and best of all you will be able to create unique and cool mods as you can create things on your very own ;)

 

Let me tell you why I know this, or skip down to the TL;DR lol ;)

 

I started programming a simple 2d rpg as my first programming project ever.

I managed to do it, despite everyone and their mother telling me it's IMPOSSIBLE without learning basic programming!

Of course it's possible, it just takes a "little" more time :)

Much later I took to studying some basic programming, did a lot of exercises, created Pong and Tetris clones etc.

You know doing the steps. Anyways after about a week of that I was noticing that I coded better and faster on my game projects :P

If I had coded the RPG game after first spending some time learning Java I would have finished it in 1/8th of the time I used.

 

It's the same with modding, either way it's entirely up to you what you do!

 

This forum is for support related directly to modding minecraft, it's as it states on the front page "not a Java school. You are expected to have basic knowledge of Java before posting here.". The focus is in understanding the minecraft src and the forge API and how to properly use it when modding, this means you will be asked to learn how to walk before we coach you on marathon running ;)

 

TL;DR:

You don't need to know Java to mod, it just makes it a lot easier and faster! (by weeks and months, not just a few minutes!).

This forum expects java knowledge and a will to learn more, that's how things work around here :)

If you don't want to get told to learn java, then you are in the wrong place. Learning java reduces errors, simplifies bug finding and opens up the way to creating unique and awesome mods!

 

 

PS: As this isn't the place for Java questions I'd like to recommend DreamInCode, it's very beginner friendly and their java forum is quite active. Check it out if you'd like at: http://www.dreamincode.net/forums/forum/32-java/

 

If you guys dont get it.. then well ya.. try harder...

Posted

I fixed the problem, guys! It is possible to cast a class to Object[]!

 

Turns out, I needed to use

 

if (tickData[0] instanceof World)
{
                 world = (World)tickData[0];
}

 

instead of

 

if (tickData instanceof World)
{
                 world = (World)tickData;
}

 

Technically, tickData all alone is an Object. To cast to it, I needed it to be an Object[], so I made it tickData[0] and it works like a charm!

 

-Mitchellbrine

 

Minecraft can do ANYTHING, it's coded in Java and you got the full power of Java behind you when you code. So nothing is impossible.

It may be freaking fucking hard though, but still possible ;)

 

If you create a topic on Modder Support, live by this motto:

I don't want your charity, I want your information
Posted

Uhm, okay let me try to say this one more time.

You CAN'T cast an class like here World to an Object[] it's impossible.

And re-read your code, you did NOT make world into an Object[] mate...

 

Also..

Technically, tickData all alone is an Object.

No, tickData is an Object[]...

If you guys dont get it.. then well ya.. try harder...

Posted

Uhm, okay let me try to say this one more time.

You CAN'T cast an class like here World to an Object[] it's impossible.

And re-read your code, you did NOT make world into an Object[] mate...

 

Oh really...

 

My TickHandler:

 

 

package mc.Mitchellbrine.HardcoreNether;

 

import java.util.EnumSet;

 

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.world.World;

import cpw.mods.fml.common.ITickHandler;

import cpw.mods.fml.common.TickType;

 

public class TickHandler implements ITickHandler {

 

 

  public static StaminaHandler worldData;

 

 

  @Override

  public void tickStart(EnumSet<TickType> type, Object... tickData) {

      if(type.equals(EnumSet.of(TickType.WORLDLOAD))){

        World world = null;

        for(int i = 0; i < tickData.length; i++)

        {

            if(tickData[0] instanceof World)

                world = (World)tickData[0];

        }

     

            if(world == null){

            return;

        }

        worldData = StaminaHandler.get(world);

        worldData.markDirty();

      }

  }

 

  @Override

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

  }

 

  @Override

  public EnumSet<TickType> ticks() {

      // TODO Auto-generated method stub

      return EnumSet.of(TickType.WORLDLOAD);

  }

 

  @Override

  public String getLabel() {

      // TODO Auto-generated method stub

      return "Stamina_Ticker";

  }

 

}

 

 

 

Now, tell me... what does this line do right here?

 

world = (World)tickData[0];

 

It casts World to tickData[0]! It's completely possible! No errors and works like a charm!

 

Technically, tickData all alone is an Object.

No, tickData is an Object[]...

 

Okay, I may be off on my logic there, but it WORKS!

-Mitchellbrine

 

Minecraft can do ANYTHING, it's coded in Java and you got the full power of Java behind you when you code. So nothing is impossible.

It may be freaking fucking hard though, but still possible ;)

 

If you create a topic on Modder Support, live by this motto:

I don't want your charity, I want your information
Posted

Now, tell me... what does this line do right here?

 

world = (World)tickData[0];

It casts World to tickData[0]! It's completely possible! No errors and works like a charm!

 

No... it does not. Quite the opposite in truth.

You are casting the contents of the first slot in tickData[] to World...

 

Okay, I may be off on my logic there, but it WORKS!

Finally something we can agree on, now your logic is not just a bit of it's straight out the window wrong.

It's impossible to cast a single object into an array, just consider it logically in your head for a second and it should be quite reasonable to assume you would agree.

 

Protip to make your life simpler:

When someone in a programming community with a high standing (reputation, rank etc.) tells you that you are wrong then you usually are just that. Especially when you state you haven't really spent time on learning the basics of the subject ;)

If you guys dont get it.. then well ya.. try harder...

Posted

I think I've graduated Java basics. I want to learn advanced physics (and the mathematical computations involved) and OpenGL. I'm also working on a 2D RPG. xD I haven't taken a formal class, but I've read books and read plenty of online tutorials for Java basics. Unfortunately, they do not offer Java (or any coding class for that matter) at my school, so everything I have learned thus far is from self-teaching.

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.