Help with my simple mod

Started by Thevayl on Sat, 08/08/2020 - 16:40

Topic category: Advanced modding

Last seen on 11:21, 22. Oct 2020
Joined Jun 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Help with my simple mod
Sat, 08/08/2020 - 16:40

Hello, so first of all english is not my native language so excuse me if I making mistakes and also i am very new to coding.

 

 

I will explain the problem. I created a sword with 2 special attacks that can be used by right clicking or shift + right clicking. I used basic procedure then I complete the program by modifying the java code directly myself especially to add delay.

 

So what does it do :
Initially the program was supposed to remove to the player 2 hunger then play a sound, make it jump in the air and add a potion effect. Then depending on whether the player is pressing shift or not it will start one of the two attacks. No matter what attack is started the program will play a sound and make the player do a series of moves.

It worked perfectly but after testing I realized that we could spam right click to spam attacks. So I decided to create a "Gcooldown" variable located in another .java file because I didn't know how to do otherwise. Then I made sure that after right clicking the program will checks if the variable is set to true to be able to start. If it is then the variable will changes to false and will be reset to true at the end of the program.

This way we cannot trigger multiple attacks at the same time.

The problem :

But it's after doing this that very strange things happened. When I do an attack, sounds are never played and the potion effect only appears visually it is not really applied, the same happens for the hunger, 2 are remove but it is only visual. Even stranger when i m spamming or olding right click sounds, effect and hunger will eventualy work but the series of movement doesn't.

 

 

I think it's just an error in the order of my code but i don't know what appening and how to fix it. If someone can help me i joining my code and a video to show more explicitly what's appening in game.

 

Video :

https://youtu.be/M4qhPuekbW8

Code for the right click :

package net.mcreator.gael_sword.procedure;

import net.minecraft.util.EnumFacing;
import net.minecraft.potion.PotionEffect;
import net.minecraft.item.ItemStack;
import net.minecraft.init.MobEffects;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.Entity;
import net.minecraft.world.World;
import net.minecraft.util.SoundCategory;

import net.minecraft.util.ResourceLocation;

import net.mcreator.gael_sword.ElementsGaelsword;

import java.util.Timer;
import java.util.TimerTask;

@ElementsGaelsword.ModElement.Tag
public class ProcedureGaelswordRightClickedInAir extends ElementsGaelsword.ModElement {
	public ProcedureGaelswordRightClickedInAir(ElementsGaelsword instance) {
		super(instance, 2);
	}

	public static void executeProcedure(java.util.HashMap<String, Object> dependencies){
		if (dependencies.get("entity")==null) {
			System.err.println("Failed to load dependency entity for procedure GaelswordRightClickedInAir!");
			return;
		}
		//if(dependencies.get("itemstack")==null){
		//	System.err.println("Failed to load dependency itemstack for procedure GaelswordRightClickedInAir!");
		//	return;
		//}

		if (dependencies.get("world") == null) {
			System.err.println("Failed to load dependency world for procedure GaelswordRightClickedInAir!");
			return;
		}

            Entity entity =(Entity)dependencies.get("entity");
            ItemStack itemstack =(ItemStack)dependencies.get("itemstack");
			World world = (World) dependencies.get("world");
			if (((ProcedureGaelcooldown.Gcooldown) == (true))) {
				ProcedureGaelcooldown.Gcooldown = (false);
				if (entity instanceof EntityPlayer)
				((EntityPlayer) entity).getFoodStats()
						.setFoodLevel((int) (((entity instanceof EntityPlayer) ? ((EntityPlayer) entity).getFoodStats().getFoodLevel() : 0) - 4));
				world.playSound((EntityPlayer) null, (entity.posX), (entity.posY), (entity.posZ),
						(net.minecraft.util.SoundEvent) net.minecraft.util.SoundEvent.REGISTRY.getObject(new ResourceLocation("gael_sword:gael_sword_start")),
						SoundCategory.NEUTRAL, (float) 1, (float) 1);
				entity.motionX = (entity.motionX);
				entity.motionY = ((entity.motionY)+0.7);
				entity.motionZ = (entity.motionZ);
				if(entity instanceof EntityLivingBase)
				((EntityLivingBase)entity).addPotionEffect(new PotionEffect(MobEffects.RESISTANCE,(int) 2,(int) 1));
	Timer chrono = new Timer();
	chrono.schedule(new TimerTask() {
		@Override
		public void run() {
		if ((entity.isSneaking())) {
			world.playSound((EntityPlayer) null, (entity.posX), (entity.posY), (entity.posZ),
							(net.minecraft.util.SoundEvent) net.minecraft.util.SoundEvent.REGISTRY
									.getObject(new ResourceLocation("gael_sword:gael_sword_2")),
							SoundCategory.NEUTRAL, (float) 1, (float) 1);
			if (((entity.getHorizontalFacing()) == EnumFacing.NORTH)) {
			entity.motionX = (entity.motionX);
			entity.motionY = (entity.motionY);
			entity.motionZ = ((entity.motionZ)+2);
			Timer chrono2 = new Timer();
			chrono2.schedule(new TimerTask() {
				@Override
				public void run() {
				entity.motionX = (entity.motionX);
				entity.motionY = (entity.motionY)+0.7;
				entity.motionZ = ((entity.motionZ)-3);
				}
			}, 900);
			}
			else{
				if (((entity.getHorizontalFacing()) == EnumFacing.SOUTH)) {
					entity.motionX = (entity.motionX);
					entity.motionY = (entity.motionY);
					entity.motionZ = ((entity.motionZ)-2);
					Timer chrono3 = new Timer();
					chrono3.schedule(new TimerTask() {
						@Override
						public void run() {
							entity.motionX = (entity.motionX);
							entity.motionY = (entity.motionY)+0.7;
							entity.motionZ = ((entity.motionZ)+3);
						}
					}, 900);
				}	
				else{
					if (((entity.getHorizontalFacing()) == EnumFacing.WEST)) {
						entity.motionX = ((entity.motionX)+2);
						entity.motionY = (entity.motionY);
						entity.motionZ = (entity.motionZ);
						Timer chrono4 = new Timer();
						chrono4.schedule(new TimerTask() {
							@Override
							public void run() {
								entity.motionX = ((entity.motionX)-3);
								entity.motionY = (entity.motionY)+0.7;
								entity.motionZ = (entity.motionZ);
							}
						}, 900);
					}
					else{
						if (((entity.getHorizontalFacing()) == EnumFacing.EAST)) {
							entity.motionX = ((entity.motionX)-2);
							entity.motionY = (entity.motionY);
							entity.motionZ = (entity.motionZ);
							Timer chrono5 = new Timer();
							chrono5.schedule(new TimerTask() {
								@Override
								public void run() {
									entity.motionX = ((entity.motionX)+3);
									entity.motionY = (entity.motionY)+0.7;
									entity.motionZ = (entity.motionZ);
								}
							}, 900);
						}
					}
				}
			}
		Timer chrono18 = new Timer();
			chrono18.schedule(new TimerTask() {
				@Override
				public void run() {
					ProcedureGaelcooldown.Gcooldown = (true);
				}
			}, 2500);
		}
		else{
			world.playSound((EntityPlayer) null, (entity.posX), (entity.posY), (entity.posZ),
							(net.minecraft.util.SoundEvent) net.minecraft.util.SoundEvent.REGISTRY
									.getObject(new ResourceLocation("gael_sword:gael_sword_1")),
							SoundCategory.NEUTRAL, (float) 1, (float) 1);
			if (((entity.getHorizontalFacing()) == EnumFacing.NORTH)) {
				entity.motionX = (entity.motionX);
				entity.motionY = (entity.motionY);
				entity.motionZ = ((entity.motionZ)+0.5);
				Timer chrono6 = new Timer();
				chrono6.schedule(new TimerTask() {
					@Override
					public void run() {
						entity.motionX = ((entity.motionX)-1);
						entity.motionY = (entity.motionY);
						entity.motionZ = (entity.motionZ);
						Timer chrono7 = new Timer();
						chrono7.schedule(new TimerTask() {
							@Override
							public void run() {
								entity.motionX = (entity.motionX);
								entity.motionY = (entity.motionY);
								entity.motionZ = ((entity.motionZ)-1);
								Timer chrono8 = new Timer();
								chrono8.schedule(new TimerTask() {
									@Override
									public void run() {
										entity.motionX = ((entity.motionX)+3);
										entity.motionY = (entity.motionY);
										entity.motionZ = (entity.motionZ);
									}
								}, 50);
							}
						}, 50);
					}
				}, 200);
			}
			else{
				if (((entity.getHorizontalFacing()) == EnumFacing.SOUTH)) {
					entity.motionX = (entity.motionX);
					entity.motionY = (entity.motionY);
					entity.motionZ = ((entity.motionZ)-0.5);
					Timer chrono9 = new Timer();
					chrono9.schedule(new TimerTask() {
						@Override
						public void run() {
							entity.motionX = ((entity.motionX)+1);
							entity.motionY = (entity.motionY);
							entity.motionZ = (entity.motionZ);
							Timer chrono10 = new Timer();
							chrono10.schedule(new TimerTask() {
								@Override
								public void run() {
									entity.motionX = (entity.motionX);
									entity.motionY = (entity.motionY);
									entity.motionZ = ((entity.motionZ)+1);
									Timer chrono11 = new Timer();
									chrono11.schedule(new TimerTask() {
										@Override
										public void run() {
											entity.motionX = ((entity.motionX)-3);
											entity.motionY = (entity.motionY);
											entity.motionZ = (entity.motionZ);
										}
									}, 50);
								}
							}, 50);
						}
					}, 200);
				}
				else{
					if (((entity.getHorizontalFacing()) == EnumFacing.WEST)) {
						entity.motionX = ((entity.motionX)+0.5);
						entity.motionY = (entity.motionY);
						entity.motionZ = (entity.motionZ);
						Timer chrono12 = new Timer();
						chrono12.schedule(new TimerTask() {
							@Override
							public void run() {
								entity.motionX = (entity.motionX);
								entity.motionY = (entity.motionY);
								entity.motionZ = ((entity.motionZ)+1);
								Timer chrono13 = new Timer();
								chrono13.schedule(new TimerTask() {
									@Override
									public void run() {
										entity.motionX = ((entity.motionX)-1);
										entity.motionY = (entity.motionY);
										entity.motionZ = (entity.motionZ);
										Timer chrono14 = new Timer();
										chrono14.schedule(new TimerTask() {
											@Override
											public void run() {
												entity.motionX = (entity.motionX);
												entity.motionY = (entity.motionY);
												entity.motionZ = ((entity.motionZ)-3);
											}
										}, 50);
									}
								}, 50);
							}
						}, 200);
					}
					else{
						if (((entity.getHorizontalFacing()) == EnumFacing.EAST)) {
							entity.motionX = ((entity.motionX)-0.5);
							entity.motionY = (entity.motionY);
							entity.motionZ = (entity.motionZ);
							Timer chrono15 = new Timer();
							chrono15.schedule(new TimerTask() {
								@Override
								public void run() {
									entity.motionX = (entity.motionX);
									entity.motionY = (entity.motionY);
									entity.motionZ = ((entity.motionZ)-1);
									Timer chrono16 = new Timer();
									chrono16.schedule(new TimerTask() {
										@Override
										public void run() {
											entity.motionX = ((entity.motionX)+1);
											entity.motionY = (entity.motionY);
											entity.motionZ = (entity.motionZ);
											Timer chrono17 = new Timer();
											chrono17.schedule(new TimerTask() {
												@Override
												public void run() {
													entity.motionX = (entity.motionX);
													entity.motionY = (entity.motionY);
													entity.motionZ = ((entity.motionZ)+3);
												}
											}, 50);
										}
									}, 50);
								}
							}, 200);
						}
					}
				}
			}
		Timer chrono18 = new Timer();
			chrono18.schedule(new TimerTask() {
				@Override
				public void run() {
					ProcedureGaelcooldown.Gcooldown = (true);
				}
			}, 2000);
		}
		}
	}, 1000);
	
}

	//	if(entity instanceof EntityPlayer)
	//((EntityPlayer)entity).getCooldownTracker().setCooldown(itemstack.getItem(), (int) 100);
}
}

Code for the variable :

package net.mcreator.gael_sword.procedure;

import net.mcreator.gael_sword.ElementsGaelsword;

@ElementsGaelsword.ModElement.Tag
public class ProcedureGaelcooldown extends ElementsGaelsword.ModElement {
	public ProcedureGaelcooldown(ElementsGaelsword instance) {
		super(instance, 4);
	}

	public static void executeProcedure(java.util.HashMap<String, Object> dependencies) {
	}
	public static boolean Gcooldown = true;
}