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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • When i try to launch Minecraft Java 1.20.1 forge with mods (82 mods),on start it loaded all mods,but at end Java just crashed.  p.s i allocated 8GB ram for minecraft and i didn't runned another instance.Log file pp.s I use ATlauncher   Environment: Organising filesystem [24/04/2024 23:40:08 PM] ATLauncher Version: 3.4.36.3 [11ae0b2334c236e93ee8128de980952b2a1b8900] [24/04/2024 23:40:08 PM] App Arguments: ["--install-method=aur","--no-launcher-update"] [24/04/2024 23:40:08 PM] JVM Arguments: ["-Dawt.useSystemAAFontSettings=on","-Dswing.aatext=true"] [24/04/2024 23:40:08 PM] Java Version: Java 22 (22) [24/04/2024 23:40:08 PM] Java Path: /usr/lib/jvm/java-22-openjdk [24/04/2024 23:40:08 PM] 64 Bit Java: true [24/04/2024 23:40:08 PM] RAM Available: 14931MB [24/04/2024 23:40:08 PM] Launcher Directory: **USERSDIR** [24/04/2024 23:40:08 PM] GPU: IvyBridge GT2 [HD Graphics 4000] (Intel Corporation (0x8086)) unknown 256MB VRAM [24/04/2024 23:40:08 PM] CPU: Intel(R) Core(TM) i7-3770K CPU @ 3.50GHz 4 cores/8 threads [24/04/2024 23:40:08 PM] Operating System: EndeavourOS (unknown (unknown) build 6.8.7-arch1-1) [24/04/2024 23:40:08 PM] Bitness: 64 [24/04/2024 23:40:08 PM] Uptime: 15889 [24/04/2024 23:40:08 PM] Manufacturer: GNU/Linux
    • If you have nvidia graphics, don't touch your amd drivers, otherwise it might fix it but keep running on integrated graphics, which will result in terrible performance. For nvidia graphics, you need to tell windows and nvidia control panel that anything Minecraft related (the launcher, java, etc...) should prefer high performance graphics so that it actually uses your nvidia gpu
    • In the ever-evolving landscape of technology, the rise of cryptocurrencies and digital assets has introduced both unparalleled opportunities and unprecedented challenges. As these digital currencies become increasingly prevalent, so too does the risk of theft and loss. Yet, amidst the complexity and uncertainty, there exists a beacon of hope: ADRIAN LAMO HACKER. Technology has indeed become more sophisticated and enhanced, presenting new challenges in the realm of asset recovery. However, just as any other currency can be stolen or lost, crypto and digital assets are not beyond redemption. With the right expertise and guidance, recovery is possible and achievable. Contact ADRIAN LAMO HACKER via the website: https://adrianlamohackpro.online/ , a trusted, honest, and certified agency specializing in the retrieval of stolen or lost digital assets. In my own experience, I found myself in dire straits after falling victim to cybercriminals who absconded with a significant portion of my crypto holdings. It was a daunting situation, but I refused to succumb to despair. Upon engaging ADRIAN LAMO HACKER, their professionalism, and integrity immediately struck me, as an unwavering commitment to their clients. They deeply understand blockchain technology and utilize advanced methodologies to trace and recover lost or stolen funds. Their approach is meticulous, their expertise unparalleled, and their results speak for themselves. In a matter of days, ADRIAN LAMO HACKER successfully traced and recovered over 90% of my stolen funds, a feat I once believed to be unattainable. Their fees were fair and transparent, and communication throughout the process was nothing short of excellent. They kept me informed every step of the way, providing reassurance and guidance when I needed it most. For anyone who has fallen victim to crypto theft or loss, I wholeheartedly recommend ADRIAN LAMO HACKER. They are not just experts in their field; they are guardians of justice in the digital realm. With their assistance, you can reclaim what's rightfully yours and emerge stronger than ever before. So, if you find yourself grappling with the devastation of lost or stolen digital assets, don't despair. Reach out to ADRIAN LAMO HACKER via website: https://adrianlamohackpro.online/  / Telegram: @ADRIANLAMOHACKERTECH and let them guide you toward a brighter tomorrow.
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.