Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

So, I have coded a sword which has an ability to teleport behind an entity when right-clicking, and that will cause damage to the sword. However, whenever I hit with the sword or right-click after pausing for a while, the durability will go back up to what it was before I vigorously used it (for testing).

 

Here is the code:

package io.nozzomi.improvisedarmory.items.swords;

import java.util.List;

import javax.annotation.Nullable;

import io.nozzomi.improvisedarmory.Main;
import io.nozzomi.improvisedarmory.init.ModItems;
import io.nozzomi.improvisedarmory.util.IHasModel;
import io.nozzomi.improvisedarmory.util.Utilities;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.IItemPropertyGetter;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class SwordTrickery extends ItemSword implements IHasModel	
{
	public int cooldown = 0;
	public int overCooldown = 0;
	public int stage = 0;
	
	public SwordTrickery() {
		super(ModItems.MATERIAL_TRICKERY);
		this.setUnlocalizedName("sword_trickery");
		this.setRegistryName("sword_trickery");
		this.setCreativeTab(Main.tabImprovised);
		
		this.addPropertyOverride(new ResourceLocation("onCooldown"), new IItemPropertyGetter() {
			@SideOnly(Side.CLIENT)
			@Override
			public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
				return stage;
			}
		});
		
		ModItems.ITEMS.add(this);
	}

	@Override
	public void registerModels()
	{
		Main.proxy.registerItemRenderer(this, 0, "inventory");
	}
	
	@Override
	public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
		ItemStack item = player.getHeldItem(hand);
		Entity e = Utilities.getMouseOver(5);
		if(e != null && e instanceof EntityLiving)
		{
			Vec3d aim = e.getLookVec();
			if(world.isAirBlock(new BlockPos(e.posX - aim.x * 2, e.posY, e.posZ - aim.z * 2)) ||
					!world.isBlockFullCube(new BlockPos(e.posX - aim.x * 2, e.posY, e.posZ - aim.z * 2)))
			{
				if(cooldown <= 0)
				{
					if(!player.capabilities.isCreativeMode)
					{
						item.damageItem(20*stage, player);
					}
					player.setPositionAndRotation(e.posX - aim.x * 2, e.posY, e.posZ - aim.z * 2, e.rotationYaw, player.rotationPitch);
					cooldown = 20;
					overCooldown = 1200;
					stage += stage < 4 ? 1 : 0;
				}
			}
		}
		return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, item);
	}
	
	@Override
	public void addInformation(ItemStack stack, World worldIn, List<String> tooltip, ITooltipFlag flagIn) 
	{
		tooltip.add("Current Stage: 0");
	}
	
	@Override
	public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker)
    {
        switch(stage)
        {
        case 0:
        	stack.damageItem(1, attacker);
        	break;
        case 1:
        	stack.damageItem(5, attacker);
        	break;
        case 2:
        	stack.damageItem(15, attacker);
        	break;
        case 3:
        	stack.damageItem(30, attacker);
        	break;
        case 4:
        	stack.damageItem(50, attacker);
        	break;
        default:
        	stack.damageItem(1, attacker);
        	break;
        }
        return true;
    }
	
	@Override
	public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected)
	{
		if(cooldown != 0)
			cooldown--;
		
		if(overCooldown != 0)
			overCooldown--;
		
		if(overCooldown == 0 && stage != 0)
			stage = 0;
	}
}

 

Edited by Nozzomi
Removed Spoiler

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.