Jump to content

[Solved] I can't figure this out - Boolean on Client and Server side


American2050

Recommended Posts

So I have an item that needs to check if the player is actually looking at some block, so I use RayTraceResult for this, that can be called only on Client Side.

 

My Problem is that then, if that returns true, I need to do something on the Server side, but as I'm setting a boolean to true on the Client side, that boolean is true only on the Client, and not on the Server.

 

How would I go around this so the server knows that the boolean is returning true on the Client and it needs to do stuff?

 

This is the code:

 

	@Override
    public void onUpdate(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected)
    {

	//CHECK IF IS SELECTED
	if(isSelected){

		if(updateTick>=4)
    	{
    		updateTick = 0;
    		
    		//CLIENT SIDE
    		if(world.isRemote){
    			
    			//IS LOCKED?
    			if(this.getNBTTagBoolean(stack, TAG_WATERING_DONGLE_LOCKED)){
    				
    				//GET BLOCK PLAYER IS LOOKING AT
        			RayTraceResult rtr = Minecraft.getMinecraft().objectMouseOver;
        	    	
        			if(rtr!=null){
        	
        				RayTraceResult.Type rtrHit = rtr.typeOfHit;
        	
        				//IS PLAYER LOOKING AT A BLOCK?
        				if(rtrHit == RayTraceResult.Type.BLOCK) {
        					
        					posBlockWatering = rtr.getBlockPos();
        					playerWatering = (EntityPlayer) entity;
        					doWatering = true;
        				}//END LOOKING AT BLOCK
        				
        				//NO LOOKING AT BLOCK
        				else{
        					posBlockWatering = null;
        					playerWatering = null;
        					doWatering = false;
        				}//END NO LOOKING AT BLOCK
        			}
        			
    				
    			}//END IS LOCKED
    			
    			//NO LOCKED
    			else{
    				doWatering = false;
    			}//END NO LOCKED
    			
    		}//END CLIENT SIDE
    		
    		if(doWatering){
    			doWater(stack, world, playerWatering, posBlockWatering);
    		}
    		
    	}

		//UPDATE TICK NOT 4 OR HIGHER - INCREASE IT
		else{
			updateTick++;
		}

	}

	//NO SELECTED
	else{

		this.updateNBTData(stack, TAG_WATERING_DONGLE_LOCKED, false);
		doWatering = false;
	}
    	
	    	    		
    }//END ON UPDATE

 

Complete code here: http://pastebin.com/6gXh7inP

 

Thanks for the help.

Link to comment
Share on other sites

You can ray trace on the server just fine.

So... Maybe the error was another?

 

This was the crash report.

 

[12:11:40] [server thread/WARN] [net.minecraft.network.NetworkSystem]: Failed to handle packet for /127.0.0.1:51001
net.minecraft.util.ReportedException: Ticking player
at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:418) ~[EntityPlayerMP.class:?]
at net.minecraft.network.NetHandlerPlayServer.update(NetHandlerPlayServer.java:174) ~[NetHandlerPlayServer.class:?]
at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher$1.update(NetworkDispatcher.java:215) ~[NetworkDispatcher$1.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:308) ~[NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:195) [NetworkSystem.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:806) [MinecraftServer.class:?]
at net.minecraft.server.dedicated.DedicatedServer.updateTimeLightAndEntities(DedicatedServer.java:408) [DedicatedServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:687) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:536) [MinecraftServer.class:?]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
Caused by: java.lang.NoClassDefFoundError: net/minecraft/client/Minecraft
at com.mramericanmike.mikedongles.items.WateringDongle.onUpdate(WateringDongle.java:214) ~[WateringDongle.class:?]
at net.minecraft.item.ItemStack.updateAnimation(ItemStack.java:526) ~[itemStack.class:?]
at net.minecraft.entity.player.InventoryPlayer.decrementAnimations(InventoryPlayer.java:389) ~[inventoryPlayer.class:?]
at net.minecraft.entity.player.EntityPlayer.onLivingUpdate(EntityPlayer.java:559) ~[EntityPlayer.class:?]
at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2218) ~[EntityLivingBase.class:?]
at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:259) ~[EntityPlayer.class:?]
at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:341) ~[EntityPlayerMP.class:?]
... 9 more
Caused by: java.lang.ClassNotFoundException: net.minecraft.client.Minecraft
at net.minecraft.launchwrapper.LaunchClassLoader.findClass(LaunchClassLoader.java:191) ~[launchwrapper-1.12.jar:?]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_91]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_91]
at com.mramericanmike.mikedongles.items.WateringDongle.onUpdate(WateringDongle.java:214) ~[WateringDongle.class:?]
at net.minecraft.item.ItemStack.updateAnimation(ItemStack.java:526) ~[itemStack.class:?]
at net.minecraft.entity.player.InventoryPlayer.decrementAnimations(InventoryPlayer.java:389) ~[inventoryPlayer.class:?]
at net.minecraft.entity.player.EntityPlayer.onLivingUpdate(EntityPlayer.java:559) ~[EntityPlayer.class:?]
at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2218) ~[EntityLivingBase.class:?]
at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:259) ~[EntityPlayer.class:?]
at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:341) ~[EntityPlayerMP.class:?]
... 9 more
Caused by: net.minecraftforge.fml.common.asm.ASMTransformerWrapper$TransformerException: Exception in class transformer net.minecraftforge.fml.common.asm.transformers.SideTransformer@770b3be0 from coremod FMLCorePlugin
at net.minecraftforge.fml.common.asm.ASMTransformerWrapper$TransformerWrapper.transform(ASMTransformerWrapper.java:253) ~[forgeSrc-1.10.2-12.18.1.2018.jar:?]
at net.minecraft.launchwrapper.LaunchClassLoader.runTransformers(LaunchClassLoader.java:279) ~[launchwrapper-1.12.jar:?]
at net.minecraft.launchwrapper.LaunchClassLoader.findClass(LaunchClassLoader.java:176) ~[launchwrapper-1.12.jar:?]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_91]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_91]
at com.mramericanmike.mikedongles.items.WateringDongle.onUpdate(WateringDongle.java:214) ~[WateringDongle.class:?]
at net.minecraft.item.ItemStack.updateAnimation(ItemStack.java:526) ~[itemStack.class:?]
at net.minecraft.entity.player.InventoryPlayer.decrementAnimations(InventoryPlayer.java:389) ~[inventoryPlayer.class:?]
at net.minecraft.entity.player.EntityPlayer.onLivingUpdate(EntityPlayer.java:559) ~[EntityPlayer.class:?]
at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2218) ~[EntityLivingBase.class:?]
at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:259) ~[EntityPlayer.class:?]
at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:341) ~[EntityPlayerMP.class:?]
... 9 more
Caused by: java.lang.RuntimeException: Attempted to load class net/minecraft/client/Minecraft for invalid side SERVER
at net.minecraftforge.fml.common.asm.transformers.SideTransformer.transform(SideTransformer.java:56) ~[forgeSrc-1.10.2-12.18.1.2018.jar:?]
at net.minecraftforge.fml.common.asm.ASMTransformerWrapper$TransformerWrapper.transform(ASMTransformerWrapper.java:249) ~[forgeSrc-1.10.2-12.18.1.2018.jar:?]
at net.minecraft.launchwrapper.LaunchClassLoader.runTransformers(LaunchClassLoader.java:279) ~[launchwrapper-1.12.jar:?]
at net.minecraft.launchwrapper.LaunchClassLoader.findClass(LaunchClassLoader.java:176) ~[launchwrapper-1.12.jar:?]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_91]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_91]
at com.mramericanmike.mikedongles.items.WateringDongle.onUpdate(WateringDongle.java:214) ~[WateringDongle.class:?]
at net.minecraft.item.ItemStack.updateAnimation(ItemStack.java:526) ~[itemStack.class:?]
at net.minecraft.entity.player.InventoryPlayer.decrementAnimations(InventoryPlayer.java:389) ~[inventoryPlayer.class:?]
at net.minecraft.entity.player.EntityPlayer.onLivingUpdate(EntityPlayer.java:559) ~[EntityPlayer.class:?]
at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2218) ~[EntityLivingBase.class:?]
at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:259) ~[EntityPlayer.class:?]
at net.minecraft.entity.player.EntityPlayerMP.onUpdateEntity(EntityPlayerMP.java:341) ~[EntityPlayerMP.class:?]
... 9 more

 

Maybe I misread it and the problem was a different one?

 

Line 214 I believe it was were this was on the original code before I redid it.

 

RayTraceResult rtr = Minecraft.getMinecraft().objectMouseOver;

 

Link to comment
Share on other sites

:o

I think I noticed now where I went wrong, using:

 

RayTraceResult rtr = Minecraft.getMinecraft().objectMouseOver;

 

When apparently I should be using:

 

RayTraceResult rtr = entity.rayTrace(6, 1);

 

Gonna do some tests with that now and see how it goes. Hopefully this is the fix.

Link to comment
Share on other sites

Entity#rayTrace

is a client-only method, you can't use it on the server.

 

Use

Item#rayTrace

to raytrace along a player's look vector for their reach distance.

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

Thanks, I'm using something like this now. Problems solved apparently ;)

 

EntityPlayer player = (EntityPlayer) entity;
RayTraceResult rtr = this.rayTrace(world, player, false);

 

And I'm also running the code only when:

 

if(entity instanceof EntityPlayer){

 

I think I have to make that check right? Because otherway the code will also run if some mob is holding the item?

Link to comment
Share on other sites

Thanks, I'm using something like this now. Problems solved apparently ;)

 

EntityPlayer player = (EntityPlayer) entity;
RayTraceResult rtr = this.rayTrace(world, player, false);

 

And I'm also running the code only when:

 

if(entity instanceof EntityPlayer){

 

I think I have to make that check right? Because otherway the code will also run if some mob is holding the item?

 

Yes. You can only cast a value to a type if you know that it's a value of that type.

 

I just noticed that you appear to be using a field of your

Item

class as a timer, this won't work.

Item

s are singletons, only one instance exists per item type. This means that you can't store per-stack data in fields, you need to store it in the

ItemStack

(using metadata, NBT or capabilities).

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

Thanks, I'm using something like this now. Problems solved apparently ;)

 

EntityPlayer player = (EntityPlayer) entity;
RayTraceResult rtr = this.rayTrace(world, player, false);

 

And I'm also running the code only when:

 

if(entity instanceof EntityPlayer){

 

I think I have to make that check right? Because otherway the code will also run if some mob is holding the item?

 

Yes. You can only cast a value to a type if you know that it's a value of that type.

 

I just noticed that you appear to be using a field of your

Item

class as a timer, this won't work.

Item

s are singletons, only one instance exists per item type. This means that you can't store per-stack data in fields, you need to store it in the

ItemStack

(using metadata, NBT or capabilities).

 

You mean the "updateTick"?

 

It's only running if the item is selected and the item is only max stack at 1, so it shouldn't be a problem, right?

Link to comment
Share on other sites

What he said was that your timer variable is the same for every item in the game, so if one Player is holding this item and another player is holding it or not holding it the variable will be equal among the items.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

So guys, I went onr of the way you suggested and I'm using the NBT to store the timer, but the problem I see now is that whenever I'm holding the item, it never comes to position. Like you know when you hold a tool and it shows from down and it comes up, its happening the same, but it stays always down.

 

The item works and everything, but I'm not sure what's causing this. I believe it is the reason that I'm constantly updating the NBT (I mean, it has to be that right?) but not sure what I can do to prevent this.

 

Here is the code:

 

http://pastebin.com/46S1v18u

 

Thanks once again for the patience, as always :D

Link to comment
Share on other sites

Basically:

if the equipped slot changes

if the item (ItemStack#getItem) in the slot changes

or if the item metadata changes (most likely you still want this)

 

The base implementation checks all of those and if any NBT changes were made (you're wanting to ignore this).

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.

Link to comment
Share on other sites

Basically:

if the equipped slot changes

if the item (ItemStack#getItem) in the slot changes

or if the item metadata changes (most likely you still want this)

 

The base implementation checks all of those and if any NBT changes were made (you're wanting to ignore this).

 

Yes, I went with checking if the LOCKED on the NBT changed from the oldStack to the newStack and performed the animation just in that case, that way the item get "re-equipped" and the render effect of been enchanted get recognized. Any other just returns false.

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.