Jump to content

[1.11] Chunk Values Returning Null


Lambda

Recommended Posts

Hey there!

 

So I was able to create (with some help) a 'energy' based system, each chunk has a value assigned.. However, trying to access this on the client side it seems that the chunks values are returning null, with a server check, they return the value.. So I'm guessing I need a packet, however I don't really know how to store the information and send it to the server side.. Here is the code I'm trying to display:

(Within the RenderGameOverlayEvent.Post)

                if (player.getHeldItemMainhand().getItem() == InitItems.itemJAWrench || player.getHeldItemMainhand().getItem() == Items.CLOCK || player.getHeldItemOffhand().getItem() == InitItems.itemJAWrench || player.getHeldItemOffhand().getItem() == Items.CLOCK) {
                    World world = player.world;
                    final Chunk chunk = world.getChunkFromBlockCoords(new BlockPos(player.getPosition()));
                    final ChunkPos chunkPos = chunk.getPos();
                    IVoidEnergy voidStorage = CapabilityVoidEnergy.getVoidEnergyFromChunk(world, chunkPos);
                    if(voidStorage != null) {
                        int percentValue = (voidStorage.getEnergyStored() / voidStorage.getMaxEnergyStored()) * 100;
                        minecraft.fontRendererObj.drawStringWithShadow("Void Flux: " + percentValue + "%", event.getResolution().getScaledWidth()/2+5, event.getResolution().getScaledHeight()/2+15, StringUtil.DECIMAL_COLOR_WHITE);
                    }else{
                        minecraft.fontRendererObj.drawStringWithShadow("No Void Flux In This Chunk!", event.getResolution().getScaledWidth()/2+5, event.getResolution().getScaledHeight()/2+15, StringUtil.DECIMAL_COLOR_WHITE);
                    }
                }

 

Everything seems to work, however when I hold the correct Item, the 'no void flux in this chunk' seems to display, meaning that the chunk value is null..

 

So this left me to believe that I need to send a packet, however I'm having trouble on what values to send... the ChuckPos? the value? the chunk itself.. etc etc...

 

So I have this so far, however I came here to see if anybody can help me finish the rest:

    public static final IDataHandler VOID_NETWORK_HANDLER = new IDataHandler() {
        @Override
        @SideOnly(Side.CLIENT)
        public void handleData(NBTTagCompound compound, MessageContext context) {
            World world = Minecraft.getMinecraft().world;
            EntityPlayer player = Minecraft.getMinecraft().player;

            if(world != null) {
                //Some how access the value > write it to the compound > (rest I know how to do)
            }
        }
    };

 

 

The rest of the code can be accessed though my github below..

here are some references so you can find the classes easily;

 

event>ClientSideEvents - The overlay event

network>PacketHandler - Where I'm handling all of my packets

voidenergy - where all the capability stuff is

api > capability - where all of the interfacing is.

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Link to comment
Share on other sites

ChunkDataEvent.Load

is only fired on the server side, so you need to subscribe to

ChunkEvent.Load

to create a default

IVoidEnergy

for any chunk that doesn't have one (i.e. client-side chunks).

 

To render the current energy value of a chunk on screen, you'll need to sync it from the server to the client using a packet. Do this when a player starts watching a chunk (

ChunkWatchEvent.Watch

) and when the energy value changes. Use

WorldServer#getPlayerChunkMap

,

PlayerChunkMap#getEntry

and

PlayerChunkMapEntry#sendPacket

to send a packet to all players watching a chunk and

SimpleNetworkWrapper#getPacketFrom

to get a

Packet

from an

IMessage

.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Okay I believe I was able to get this work.. I ended up opting for a GUI instead of just referencing the fontRendererObj, however trying to divide the energyStored / capacity seems to always get me 0%... So I system.out both the ints, and they seem to be storing correctly, when I try to divide them.. I get 0. Maybe I'm just bad at math? I'm almost 100% sure its not due to my math skill :).

 

However, when I'm in a chunk that has been uneffected, it seems to return 100%, like expected. Anything less than the capacity.. returns 0;

        final String textToPrint = StringUtil.localizeFormatted("hud." + ModUtil.MOD_ID + ".voidenergy", ((voidEnergy.getEnergyStored() / voidEnergy.getMaxEnergyStored()) * 100));
        drawString(minecraft.fontRendererObj, textToPrint + "%", 0,0, StringUtil.DECIMAL_COLOR_WHITE);

 

with my localization being:

hud.plentifulutilities.voidenergy=Void Flux: %d

 

Also, would anybody know how to make a GUI screen that allows a person to edit the GUI values?

 

Thanks!

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Link to comment
Share on other sites

double percentedValue = (voidEnergy.getEnergyStored() / voidEnergy.getMaxEnergyStored()) * 100;

 

I also tried:

        double percentedValue = (voidEnergy.getEnergyStored() / voidEnergy.getMaxEnergyStored());
        double finalValue = percentedValue * 100;

 

just in case multiplication was messing with it.

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Link to comment
Share on other sites

You're dividing two integers, so Java is using integer division and then converting the result to a

double

. You need to cast one of the numbers to a

double

so Java converts the other one to a

double

and uses floating-point division.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.