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

Hello,

I have created a capability for item to allow players to apply potion effects on swords. However it doesn't work. Everything seems to be OK, but I'm not able to apply my capability to any item. What's the problem?

 

Capability:

public class Poison implements IPoison{
	
	private PotionType type;
	private List<PotionEffect> effects = new ArrayList<PotionEffect>();
	private int remaininghits;
	
	@Override
	public void setHitsRemaining(int hits) {
		this.remaininghits = hits;
		
	}

	@Override
	public void removehits(int hits) {
		this.remaininghits -= hits; 
		if (this.remaininghits<=0)
		{
			this.type = null;
			this.setEffects(new ArrayList<PotionEffect>());
			System.out.println("Out of hits");
		}
	}
	
	@Override
	public int getHitsRemaining()
	{
		return this.remaininghits;
	}

	@Override
	public void setType(PotionType type)
	{
		this.type = type;	
	}

	@Override
	public void setEffects(List<PotionEffect> effects)
	{
		this.effects = effects;
	}

	@Override
	public List<PotionEffect> getEffects()
	{
		if (this.effects.isEmpty() && type != null)
		{
			return type.getEffects();
		}
		return effects;
	}

	@Override
	public PotionType getType()
	{
		return this.type;
	}

	@Override
	public List<PotionEffect> getEffectList()
	{
		return this.effects;
	}

}

 

Interface:

public interface IPoison {
	
	public void setType (PotionType type);
	
	public void setEffects (List<PotionEffect> effects);
	
	public void setHitsRemaining (int hits);
	
	public void removehits (int hits);
	
	public PotionType getType();
	
	public List<PotionEffect> getEffectList();
	
	public int getHitsRemaining();
	
	public List<PotionEffect> getEffects();

}

 

Provider:

public class PoisonProvider implements ICapabilitySerializable<NBTTagCompound>{
	
	@CapabilityInject(IPoison.class)

	public static final Capability<IPoison> POISON_CAP = null;

	 private IPoison instance = POISON_CAP.getDefaultInstance();

	@Override
	public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
		return capability == POISON_CAP;
	}

	@Override
	public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
		return capability == POISON_CAP ? POISON_CAP.<T> cast(this.instance) : null;
	}

	@Override
	public NBTTagCompound serializeNBT() {

		 return (NBTTagCompound) POISON_CAP.getStorage().writeNBT(POISON_CAP, this.instance, null);
	}

	@Override
	public void deserializeNBT(NBTTagCompound nbt) {
		POISON_CAP.getStorage().readNBT(POISON_CAP, instance, null, nbt);
		
	}
}

 

Storage:

public class PoisonStorage implements IStorage<IPoison>{

	@Override
	public NBTTagCompound writeNBT(Capability<IPoison> capability, IPoison instance, EnumFacing side) {
		NBTTagCompound compound = new NBTTagCompound();
		if (instance.getType() != null)
		{
			ResourceLocation resourcelocation = (ResourceLocation)PotionType.REGISTRY.getNameForObject(instance.getType());
			compound.setString("Type", resourcelocation.toString());
		}
		if (!instance.getEffectList().isEmpty()) 
		{
			NBTTagList nbttaglist = new NBTTagList();
			for (PotionEffect potioneffect : instance.getEffects())
			{
            nbttaglist.appendTag(potioneffect.writeCustomPotionEffectToNBT(new NBTTagCompound()));
			}
			compound.setTag("CustomPotionEffects", nbttaglist);
        }
		
		compound.setInteger("RemainingHits", instance.getHitsRemaining());
		return compound;
	}

	@Override
	public void readNBT(Capability<IPoison> capability, IPoison instance, EnumFacing side, NBTBase nbt) {
		NBTTagCompound compound = (NBTTagCompound) nbt;
		if (compound.hasKey("Potion"))
		{
			instance.setType(PotionUtils.getPotionTypeFromNBT(compound));
		}
		if (compound.hasKey("CustomPotionEffects"))
		{
			instance.setEffects(PotionUtils.getEffectsFromTag(compound));
		}
		instance.setHitsRemaining(((NBTTagCompound)nbt).getInteger("RemainingHits"));
	}

}

 

Event Handler:

public class ModWeaponHandler {
	
	public static final ResourceLocation POISON_CAP = new ResourceLocation(Reference.MOD_ID, "poison");
	
	@SubscribeEvent(priority=EventPriority.NORMAL, receiveCanceled=true)
	public void onEvent(AnvilUpdateEvent event)
	{
		if(event.getLeft().getItem() instanceof ItemSword && event.getRight().getItem() instanceof ItemPotion && event.getOutput() == ItemStack.EMPTY)
		{
			ItemStack output = event.getLeft().copy();
			IPoison poison = output.getCapability(PoisonProvider.POISON_CAP, null);
				
			poison.setType(PotionUtils.getPotionFromItem(event.getRight()));
			if (event.getRight().getTagCompound().hasKey("CustomPotionEffects"))
			{
				poison.setEffects(PotionUtils.getEffectsFromStack(event.getRight()));
			}
			poison.setHitsRemaining(64);
			event.setCost(1);
			event.setOutput(output);
		}
	}

	@SideOnly(Side.CLIENT)
	@SubscribeEvent(priority=EventPriority.NORMAL, receiveCanceled=true)
	public void onEvent(ItemTooltipEvent event)
	{
		if (event.getItemStack().getItem() instanceof ItemSword)
		{
			IPoison poison = event.getItemStack().getCapability(PoisonProvider.POISON_CAP, null);
			if (!poison.getEffects().isEmpty())
			{
				for (PotionEffect effect : poison.getEffects())
				{
					String name = effect.getPotion().getName();
					if (effect.getDuration()!=0)
					{
						String s1 = I18n.translateToLocal(name).trim();
						if (effect.getAmplifier() > 0)
						{
							s1 = s1 + " " + I18n.translateToLocal("potion.potency." + effect.getAmplifier()).trim();
						}

						if (effect.getDuration() > 20)
						{
							s1 = s1 + " (" + Potion.getPotionDurationString(effect, 1) + ")";
						}
						event.getToolTip().add(TextFormatting.RED + s1);
		            }
					event.getToolTip().add(poison.getHitsRemaining() + " hits remaining");
				}
			}
		}
	}

	@SubscribeEvent(priority=EventPriority.HIGHEST, receiveCanceled=true)
	public void onEvent(LivingAttackEvent event)
	{
		if (event.getSource().getSourceOfDamage() !=null)
		{
			if (event.getSource().getSourceOfDamage() instanceof EntityLivingBase)
			{
				EntityLivingBase attacker = (EntityLivingBase) event.getSource().getSourceOfDamage();
				if (attacker.getHeldItemMainhand().getItem() instanceof ItemSword && !attacker.world.isRemote)
				{
					IPoison poison = attacker.getHeldItemMainhand().getCapability(PoisonProvider.POISON_CAP, null);
					System.out.println(poison.getEffects());
					System.out.println(poison.getHitsRemaining());
					if (!poison.getEffects().isEmpty() && event.getEntityLiving().hurtResistantTime<10)
					{
						for (PotionEffect effect : poison.getEffects())
						{
							if (effect.getPotion().isInstant())
							{
								effect.getPotion().affectEntity(null, event.getSource().getSourceOfDamage(), event.getEntityLiving(), effect.getAmplifier(), 1/6D);
								event.getEntityLiving().hurtResistantTime = 0;
							}
							else
							{
								event.getEntityLiving().addPotionEffect(new PotionEffect(effect));
							}
						}
						poison.removehits(1);
					}
				}
			}
        }
    }

	@SubscribeEvent(priority=EventPriority.NORMAL, receiveCanceled=true)
	public void onEvent(AttachCapabilitiesEvent.Item event)
	{
		if (event.getItemStack().getItem() instanceof ItemSword)
		{
			if (!event.getItemStack().hasCapability(PoisonProvider.POISON_CAP, null))
			{
				event.addCapability(POISON_CAP, new PoisonProvider());
			}
		}
	}
}

 

I can combine sword and potion in the anvil, but the output doesn't have any capability attached. Capability is registered in preInit by CapabilityManager.INSTANCE.register(IPoison.class, new PoisonStorage(), Poison.class); Event handler is registered in Init by MinecraftForge.EVENT_BUS.register(new ModWeaponHandler()); I have already created a very similar cap in the past and it worked perfectly, I don't know what is the problem now.

Thanks for any help.

Edited by fcelon

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.