Jump to content

PlayerTickEvent behaving erratically


chimera27

Recommended Posts

I was having trouble getting an item to do some things, namely change state via keypress and not slow down the player when they are charging it, so I (probably stupidly) moved alot of it's code to my PlayerTickHandler class and had it run if the player held the item and was right clicking/pressing the right button. I immediately ran into a problem. Much of the code I have set to run on the occasions specified will sometimes only run part of the way through only SOMETIMES.

 

Here's the first set of code, it is called when the player has the item in their hand and right click. I have some debug messages in it and it will either stop after initializing all the projectiles, or go all the way through:

 

 

if (player.getHeldItem() != null) {

			if (player.getHeldItem().getItem() instanceof ItemBeam) {
			ItemBeam Beam = (ItemBeam) player.getHeldItem().getItem();
				System.out.println("IsBeamInUse = " + isBeamInUse);
				System.out.println("Mousebuttondown = " + Mouse.isButtonDown(1));
					Beam.t = t;
					Beam.mode = mode;

				if(Mouse.isButtonDown(1)){
					t = t + 1;
					isBeamInUse = true;
				} 

				if(!Mouse.isButtonDown(1) && isBeamInUse){
					player.addChatMessage(new ChatComponentText("Calling onPlayerStoppedUsing!!"));
					if(world == null){
					player.addChatMessage(new ChatComponentText("World null!!"));
					}



					EntityPowerBolt entitypowerbolt = new EntityPowerBolt(world,
							player);
					player.addChatMessage(new ChatComponentText("Initalized powerbolt"));

					EntityChargedPowerBolt entitychargedpowerbolt = new EntityChargedPowerBolt(
							world, player);
					player.addChatMessage(new ChatComponentText("Initalized charged powerbolt"));

					EntityIceBolt entityicebolt = new EntityIceBolt(world,
							player);
					player.addChatMessage(new ChatComponentText("Initalized icebolt"));

					EntityChargedIceBolt entitychargedicebolt = new EntityChargedIceBolt(
							world, player);
					player.addChatMessage(new ChatComponentText("Initalized charged iceboltbolt"));

					EntityWaveBolt entitywavebolt = new EntityWaveBolt(world,
							player);
					player.addChatMessage(new ChatComponentText("Initalized wavebolt"));

					EntityChargedWaveBolt entitychargedwavebolt = new EntityChargedWaveBolt(
							world, player);
					player.addChatMessage(new ChatComponentText("Initalized charged wavebolt"));


					EntityPlasmaBolt entityplasmabolt = new EntityPlasmaBolt(world,
							player);
					player.addChatMessage(new ChatComponentText("Initalized plasmabolt"));

					EntityChargedPlasmaBolt entitychargedplasmabolt = new EntityChargedPlasmaBolt(
							world, player);
					player.addChatMessage(new ChatComponentText("Initalized charged plasmabolt"));

					EntityLightBolt entitylightbolt = new EntityLightBolt(world,
							player);
					EntityChargedLightBolt entitychargedlightbolt = new EntityChargedLightBolt(
							world, player);

					EntityDarkBolt entitydarkbolt = new EntityDarkBolt(world,
							player);
					EntityChargedDarkBolt entitychargeddarkbolt = new EntityChargedDarkBolt(
							world, player);

					EntityAnnihilatorBolt entityannihilatorbolt = new EntityAnnihilatorBolt(
							world, player);
					EntityChargedAnnihilatorBolt entitychargedannihilatorbolt = new EntityChargedAnnihilatorBolt(
							world, player);

					EntityUnchargedNovaBolt entityunchargednovabolt = new EntityUnchargedNovaBolt(
							world, player);

					EntityFlamethrower entityflamethrower = new EntityFlamethrower(
							world, player);
					EntityWaveBuster entitywavebuster = new EntityWaveBuster(world,
							player);
					EntityIceSpreader entityicespreader = new EntityIceSpreader(world,
							player);
					EntityNovaBolt entitynovabolt = new EntityNovaBolt(world,
							player);

					// Insert fireing sound here
					player.addChatMessage(new ChatComponentText("Initalized everything"));

					if (!world.isRemote) {
						player.addChatMessage(new ChatComponentText("World isn't remote!!"));




						if (t < 60) {
							player.addChatMessage(new ChatComponentText("Time less than 60"));

							if (mode == 1) {
								player.addChatMessage(new ChatComponentText("Spawning powerbolt"));

								world.spawnEntityInWorld(entitypowerbolt);
							}
							if (mode == 2) {
								world.spawnEntityInWorld(entityicebolt);
							}
							if (mode == 3) {
								world.spawnEntityInWorld(entitywavebolt);
							}
							if (mode == 4) {
								world.spawnEntityInWorld(entityplasmabolt);
							}
							if (mode == 5) {
								world.spawnEntityInWorld(entitydarkbolt);
							}
							if (mode == 6) {
								world.spawnEntityInWorld(entityannihilatorbolt);
							}
							if (mode == 7) {
								world.spawnEntityInWorld(entitylightbolt);
							}
							if (mode ==  {
								world.spawnEntityInWorld(entityunchargednovabolt);
							}
							t = 0;



						} else if (t >= 60) {
							player.addChatMessage(new ChatComponentText("Time more than 60"));

							if (mode == 1) {
								world.spawnEntityInWorld(entitychargedpowerbolt);
							}
							if (mode == 2) {
								world.spawnEntityInWorld(entitychargedicebolt);
							}
							if (mode == 3) {
								world.spawnEntityInWorld(entitychargedwavebolt);
							}
							if (mode == 4) {
								world.spawnEntityInWorld(entitychargedplasmabolt);
							}
							if (mode == 5) {
								world.spawnEntityInWorld(entitychargeddarkbolt);
							}
							if (mode == 6) {
								world.spawnEntityInWorld(entitychargedannihilatorbolt);
							}
							if (mode == 7) {
								world.spawnEntityInWorld(entitychargedlightbolt);
							}
							if (mode ==  {
								world.spawnEntityInWorld(entitynovabolt);
							}
							t = 0;

						}
					}




					isBeamInUse = false;
					t = 0;

				}

 

 

 

The second set of code (relating to changing a variable called mode depending on different keybinds) behaves the EXACT same way as the first, only working about half the time. Anyone know what the heck is causing this, or a better solution I could use instead of moving the code to my playertickhandler? (Keeping the code in my item class isn't an option as far as I know, as the only time onStoppedUsingItem is called is when the player stopped 'using' it, which means the player must start 'using' it, which slows down the player while they're 'using' it. I've tried to stop or counter this slowdown by boosting the players speed, and using player.setEating(false);, both to no avail)

Creator of Metroid Cubed! Power Suits, Beams, Hypermode and more!

width=174 height=100http://i.imgur.com/ghgWmA3.jpg[/img]

Link to comment
Share on other sites

Keep in mind that keys / mouse are only available CLIENT side, so you must send a packet to the server letting it know what the key states are or what to do, and then let your tick handler go from there.

 

One option to keep the code in your Item class is to use the onUpdate method that maps use, as that's called every tick while the item is in inventory whether or not it is in use or held. You will still need to send packets if you don't use the vanilla system for using items, and set a custom using item flag either in your player's IExtendedEntityProperties or the ItemStack's NBT. The former is probably best, especially if you plan on having other items with a similar mechanic.

 

@Override
public void onUpdate(ItemStack stack, World world, Entity entity, int slot, boolean isHeld) {
// check if the item is held, if entity is a player and "isCustomItemInUse" or whatever
// then do what you want with no move slowdown, since the item is not in use as far as vanilla is concerned
}

Link to comment
Share on other sites

Keep in mind that keys / mouse are only available CLIENT side, so you must send a packet to the server letting it know what the key states are or what to do, and then let your tick handler go from there.

 

One option to keep the code in your Item class is to use the onUpdate method that maps use, as that's called every tick while the item is in inventory whether or not it is in use or held. You will still need to send packets if you don't use the vanilla system for using items, and set a custom using item flag either in your player's IExtendedEntityProperties or the ItemStack's NBT. The former is probably best, especially if you plan on having other items with a similar mechanic.

 

@Override
public void onUpdate(ItemStack stack, World world, Entity entity, int slot, boolean isHeld) {
// check if the item is held, if entity is a player and "isCustomItemInUse" or whatever
// then do what you want with no move slowdown, since the item is not in use as far as vanilla is concerned
}

Thanks! This makes a hell of a lot more sense then putting my item code in a tickhandler :P

Creator of Metroid Cubed! Power Suits, Beams, Hypermode and more!

width=174 height=100http://i.imgur.com/ghgWmA3.jpg[/img]

Link to comment
Share on other sites

Keep in mind that keys / mouse are only available CLIENT side, so you must send a packet to the server letting it know what the key states are or what to do, and then let your tick handler go from there.

 

One option to keep the code in your Item class is to use the onUpdate method that maps use, as that's called every tick while the item is in inventory whether or not it is in use or held. You will still need to send packets if you don't use the vanilla system for using items, and set a custom using item flag either in your player's IExtendedEntityProperties or the ItemStack's NBT. The former is probably best, especially if you plan on having other items with a similar mechanic.

 

@Override
public void onUpdate(ItemStack stack, World world, Entity entity, int slot, boolean isHeld) {
// check if the item is held, if entity is a player and "isCustomItemInUse" or whatever
// then do what you want with no move slowdown, since the item is not in use as far as vanilla is concerned
}

I transferred it into my items onUpdate method, and it's still acting the same as it was, spawning the projectile about half the time and getting exactly as far as it did before when it doesn't... I'm probs REALLY stupid for having to ask this but what do I need to use packets for? I'm really not 100% sure what I would be telling the server that it doesn't already know, the bullets that DO spawn do damage to mobs, which do die and drop their items...

 

EDIT: Confirmed, i'm an idiot. The reason the entities are spawning half the time is because the other half the world is remote, meaning i'd have to send packets to the server those times, right? Time to set up a packet handler... Still not sure what differentiates the times where the world is and isn't remote, seems arbitrary to me...

 

Creator of Metroid Cubed! Power Suits, Beams, Hypermode and more!

width=174 height=100http://i.imgur.com/ghgWmA3.jpg[/img]

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

    • One of my players is suddenly unable to join a locally hosted MC Eternal server. We have been playing on this server for about 2-3 weeks now. I have tried erasing his player files and his reputation file, and now it just coughs up this and kicks him out: [User Authenticator #5/INFO] [minecraft/NetHandlerLoginServer]: UUID of player EthosTheGod is 7692d8db-02c3-424f-a4ab-0e4e259b106b [20:25:36] [User Authenticator #4/INFO] [minecraft/NetHandlerLoginServer]: UUID of player EthosTheGod is 7692d8db-02c3-424f-a4ab-0e4e259b106b [20:29:35] [Server thread/WARN] [minecraft/MinecraftServer]: Can't keep up! Did the system time change, or is the server overloaded? Running 575849ms behind, skipping 11516 tick(s) [20:29:35] [Server thread/INFO] [minecraft/NetHandlerLoginServer]: com.mojang.authlib.GameProfile@4a6c63f1[id=7692d8db-02c3-424f-a4ab-0e4e259b106b,name=EthosTheGod,properties={textures=[com.mojang.authlib.properties.Property@241ea89e]},legacy=false] (/IP.ADDRESS) lost connection: Disconnected [20:29:35] [Server thread/INFO] [minecraft/NetHandlerLoginServer]: com.mojang.authlib.GameProfile@6ab6c661[id=7692d8db-02c3-424f-a4ab-0e4e259b106b,name=EthosTheGod,properties={textures=[com.mojang.authlib.properties.Property@7f19aae3]},legacy=false] (/IP.ADDRESS) lost connection: Disconnected It just says "connection timed out" on his end. Any ideas?
    • I'm trying to migrate my mod from 1.20 to 1.21. Some packages in the forge api were changed so my mod did have some classes not working. I've changed everything i needed but still is getting me the following error error: cannot access Registry DeferredRegister.create(ForgeRegistries.BLOCKS, FarmMod.MOD_ID); ^ class file for net.minecraft.core.Registry not found The piece of code that is wrong is   public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, FarmMod.MOD_ID); And here are my imports   import com.lucas.farmmod.FarmMod; import com.lucas.farmmod.block.custom.BaseIrrigatorBlock; import com.lucas.farmmod.item.ModItems; import com.lucas.farmmod.item.custom.BaseIrrigatorBlockItem; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.Item; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.registries.DeferredRegister; import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.RegistryObject; The class DeferredRegister is throwing the error in the print below     I've tried running rebuilding my project in every way possible, tried refreshing my dependencies but nothing works. What can i do?
    • It sounds like there might be a synchronization issue with your PartEntity. Ensure that the part entity’s position is updated in your entity's tick method to continuously match the main entity’s location.
    • For keyboard and mouse inputs, Minecraft Forge utilizes the event system to manage interactions, making it easier to handle across different mods. If you’re looking to bypass this and read inputs directly, you’d typically look into the KeyboardListener and MouseListener classes in the game's code. These classes process input events directly from the user's hardware. If you're experimenting with inputs a lot, you might find a compact keypad handy for quick commands. Check out numeric keyboard . It can speed up your coding workflow! Good luck!
  • Topics

×
×
  • Create New...

Important Information

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