Tutorial 19. Tile Entity Logics

Before you read, make sure you have seen the previous tutorial

Code is on github

NOTE:

MY CODE PLUGIN IS NOT UPDATED YET. SOURCECODE WILL BE ADDED LATER

Ok, so a few tutorial ago, we made a tile entity with an inventory. Today, we will add some functiallity.
Once you put atleast 1 testItem in the top slots, it will take a while, and finally put a dirtblock in the output slot. Lets work on those logistics first

Remember: slot 0-5 are input, 6 is output


So, we need to check a bunch of things every tick. Therefor, we use the method updateEntity();

 

So, this code is hard to come up with yourself. But you just have to start somewhere, and just go.
I started with, is the grinder smashing already or not.
If yes, check if it is done. When it is done, do some stuff with the itemstacks. E.G. decrease the input slot by one, and increase the output slot by one, and set isSmashing to false.
If the grinder is not done, decrease a value, where we store how long it still has to go.

When it is not smashing, check if it can smash. If it can smash, start smashing, otherwise do very little

Just read the comments.

We also need a getter for the 2 private variables

 

Well, that was a lot of logics. We’re not done yet.
We want to make a custom progress bar. So, i have made a new GUI

mygui

Now, you learned how to draw the gui already. The arrow, is harder (a bit).

So, that black arrow is the progress bar. How do we partially show it? well, we just draw the bottom arrow on top of it.
We need to learn about U and V now. In the texture, my black arrow is located at (177;0), and my grey arrow at (177, 24). The arrows are 23 high/wide. I like to store these values in my GUI class. Again, define your picture’s location. Should be nearly the same as the gui.
Then draw the rectangle, with the values described.

 

Now, lets (partially) cover the texture up.
The height depends on how far the smasher is. The formula i use to get the value is
ArrowVDelta * percentage
So, depending on how far the smasher is, i partially draw it. How much? Well:
(TotalTimeNeeded – TimeLeft) / TotalTimeNeeded.
Now, you will have some problems with floats and ints. Therefor one of the operators has to be casted to flaot, and finally rounded back to ints.

 

Now, you will notice that the arrow doesn’t update. That is because we need to sync client and server. For this, we need packets, but for updating progress bars, minecraft has a little easy way to access the packets.
Firstly, we have to add our values that we want to sync.

Secondly, we want to detect when they change

 

And once they change, we want to forge an update

 

Everything should be done now.
NOTE: THIS IS A GUIDE ON HOW TO LEARN. THE CODE MIGHT NOT WORK IN YOUR CASE. IF THAT IS TRUE, CHANGE IT. THIS IS NOT TO COPY PASTE!


Back to List

Next tutorial


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">