Jump to content

[1.11.2] Custom capability not working


fcelon

Recommended Posts

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

    • The crash is no available Add crash-reports with sites like https://paste.ee/  
    • Hello, I have a problem, I have a modpack that worked for me for 5 days without a problem on my own server, I play with friends, but suddenly it started having a problem, but only with me, minecraft, when I turn it on, runs smoothly after a few seconds, it starts cutting and eats a lot of frames and crashes (it is really capable of a lot eat up 32gb of ram too) the problem is probably with the curious api, but I don't know how to solve it crash errors:  from client:   https://pastebin.com/28Z0JVBJ
    • Apparently skins aren't showing up when I open minecraft with Forge I removed all the mods and nothing changed I removed optifine and nothing changed When I removed Forge and went to Vanilla it worked
    • I'm trying to render an item in GUI, and upon hover, I want to display an image above it. However, this image is rendered beneath the item. Is this even possible?   final PoseStack poseStack2 = RenderSystem.getModelViewStack(); poseStack2.pushPose(); poseStack2.mulPoseMatrix(poseStack.last().pose()); poseStack2.translate(x, y, 0f); poseStack2.scale(scale, scale, scale); Minecraft.getInstance().getItemRenderer().renderAndDecorateFakeItem(itemStack, 0, 0); RenderSystem.disableBlend(); poseStack2.popPose(); RenderSystem.applyModelViewMatrix(); // Reset to the current pose stack if (isHovered) { poseStack.pushPose(); RenderSystem.enableBlend(); RenderSystem.setShaderColor(1f, 1f, 1f, 1f); RenderSystem.setShaderTexture(0, BG); Screen.blit(poseStack, x, y, 0, 0, SIZE, SIZE, SIZE, SIZE); RenderSystem.disableBlend(); poseStack.popPose(); }  
    • Hey! I want to add a glow texture for my custom armor. How can i do it?
×
×
  • Create New...

Important Information

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