Jump to content

Recommended Posts

Posted (edited)

Goal: Create a cool down system for firing a weapon, so getting and setting an integer within an itemstack

How: Using forge capability system

Problem: Can't set (but can retrieve capability)

 

IRepeaterData:

package com.kain.slippworld.data.repeater;

public interface IRepeaterData {
	public int getCoolDown();

	public void setCoolDown(int coolDown);
}

 

RepeaterData:

package com.kain.slippworld.data.repeater;

public class RepeaterData implements IRepeaterData {
	private int coolDown;
	
	@Override
	public int getCoolDown() {
		return coolDown;
	}

	@Override
	public void setCoolDown(int coolDown) {
		this.coolDown = coolDown;
	}

}

 

RepeaterCapability:

package com.kain.slippworld.data.repeater;

import com.kain.slippworld.*;
import com.kain.slippworld.data.*;

import net.minecraft.item.*;
import net.minecraft.nbt.*;
import net.minecraft.util.*;
import net.minecraftforge.common.capabilities.*;

public class RepeaterCapability {
	public static final ResourceLocation NAME = new ResourceLocation(Reference.MOD_ID, "_Repeater");

	@CapabilityInject(IRepeaterData.class)
	public static final Capability<IRepeaterData> REPEATER_DATA_CAPABILITY = null;

	public static final EnumFacing DEFAULT_FACING = null;

	public static void register() {
		CapabilityManager.INSTANCE.register(IRepeaterData.class, new Capability.IStorage<IRepeaterData>() {
			@Override
			public NBTBase writeNBT(Capability<IRepeaterData> capability, IRepeaterData instance, EnumFacing side) {
				return new NBTTagInt(instance.getCoolDown());
			}

			@Override
			public void readNBT(Capability<IRepeaterData> capability, IRepeaterData instance, EnumFacing side, NBTBase nbt) {
				instance.setCoolDown(((NBTTagInt) nbt).getInt());
			}
		}, () -> new RepeaterData());
	}

	public static IRepeaterData getCapability(ItemStack itemStack) {
		return itemStack != null && itemStack.hasCapability(REPEATER_DATA_CAPABILITY, DEFAULT_FACING) ? itemStack.getCapability(REPEATER_DATA_CAPABILITY, DEFAULT_FACING) : null;
	}

	public static ICapabilityProvider createProvider() {
		return new GenericCapabilityProvider<>(REPEATER_DATA_CAPABILITY, DEFAULT_FACING);
	}

	public static ICapabilityProvider createProvider(IRepeaterData repeaterData) {
		return new GenericCapabilityProvider(REPEATER_DATA_CAPABILITY, DEFAULT_FACING, repeaterData);
	}
}

 

GenericCapabilityProvider:

package com.kain.slippworld.data;

import net.minecraft.nbt.*;
import net.minecraft.util.*;
import net.minecraftforge.common.capabilities.*;

public class GenericCapabilityProvider<HANDLER> implements ICapabilitySerializable<NBTBase> {
	private final Capability<HANDLER> capability;

	private final EnumFacing facing;

	private final HANDLER instance;

	public GenericCapabilityProvider(Capability<HANDLER> capability, EnumFacing facing) {
		this(capability, facing, capability.getDefaultInstance());
	}

	public GenericCapabilityProvider(Capability<HANDLER> capability, EnumFacing facing, HANDLER instance) {
		this.capability = capability;
		this.facing = facing;
		this.instance = instance;
	}

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

	@Override
	public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
		if(capability == getCapability()) {
			return (T) instance;
		}
		return null;
	}

	@Override
	public NBTBase serializeNBT() {
		return getCapability().writeNBT(getInstance(), getFacing());
	}

	@Override
	public void deserializeNBT(NBTBase nbt) {
		getCapability().readNBT(getInstance(), getFacing(), nbt);
	}

	public Capability<HANDLER> getCapability() {
		return capability;
	}

	public EnumFacing getFacing() {
		return facing;
	}

	public HANDLER getInstance() {
		return instance;
	}
}

 

ItemRepeater

package com.kain.slippworld.item.tool;

import javax.annotation.*;

import com.kain.slippworld.*;
import com.kain.slippworld.data.repeater.*;

import net.minecraft.creativetab.*;
import net.minecraft.entity.*;
import net.minecraft.entity.player.*;
import net.minecraft.item.*;
import net.minecraft.nbt.*;
import net.minecraft.util.*;
import net.minecraft.world.*;
import net.minecraftforge.common.capabilities.*;
import net.minecraftforge.fml.relauncher.*;

public class ItemRepeater extends Item {
	public final ToolMaterial material;
	protected final float damage;
	protected final int maxCoolDown;

	public ItemRepeater(ToolMaterial material, float distance, int maxCoolDown) {
		super();

		...
	}

	@Override
	public void onUpdate(ItemStack itemStack, World world, Entity e, int slot, boolean selected) {
		super.onUpdate(itemStack, world, e, slot, selected);

		if(!world.isRemote) {
			IRepeaterData data = RepeaterCapability.getCapability(itemStack);

			if(data != null) {
				int coolDown = data.getCoolDown();

				if(coolDown > 0) {
					System.out.println("COOL DOWN");
					data.setCoolDown(coolDown--);
				}
			}
		}
	}

	...

	@Override
	@Nullable
	public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable NBTTagCompound nbt) {
		return RepeaterCapability.createProvider();
	}

	public void attackEntity(ItemStack itemStack, EntityLivingBase entity, EntityPlayer player) {
		if(!player.world.isRemote) {
			IRepeaterData data = RepeaterCapability.getCapability(itemStack);

			if(data != null && data.getCoolDown() == 0) {
				entity.hurtResistantTime = 0;
				entity.attackEntityFrom(DamageSource.GENERIC, getDamage());

				data.setCoolDown(100);

				System.out.println("BANG");
			}
		}
	}

	public float getDamage() {
		return damage;
	}

	public int getMaxCoolDown() {
		return maxCoolDown;
	}
}

 

ItemRepeater#attackEntity is being called externally by a packet. Right now when I right click (trigger the attackEntity method), it detects the cool down value as being 0 and outputs "BANG," but it never sets the cool down value to 100. I'm sure that there's some markDirty method somewhere but I just can't find it.

 

I based my code around choonster's test mod.

Edited by TLHPoE
Forgot a class

Kain

Posted (edited)
11 minutes ago, TLHPoE said:

ItemRepeater#attackEntity is being called externally by a packet. Right now when I right click (trigger the attackEntity method), it detects the cool down value as being 0 and outputs "BANG," but it never sets the cool down value to 100.

 

What makes you think the cooldown isn't being set to 100? Are you looking at data from the client or from the server?

 

Capabilities aren't automatically synced between the server and client, you need to sync them yourself. I explain how to sync item capabilities here.

Edited by Choonster

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted
4 hours ago, Choonster said:

 

What makes you think the cooldown isn't being set to 100? Are you looking at data from the client or from the server?

 

Capabilities aren't automatically synced between the server and client, you need to sync them yourself. I explain how to sync item capabilities here.

 

Both times I access the capabilities are server side.

Kain

Posted
2 hours ago, Jay Avery said:

So what exactly tells you that the cool down is not being set to 100?

 

This check:

if(data != null && data.getCoolDown() == 0) {

 

This passes every time, and inside the clause it sets the cool down to 100.

  • Like 1

Kain

Posted

I can't see any obvious issues with your code, could you create a Git repository for it (if you don't already have one) and link it here? I'll try debugging it myself.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

In future, please create the Git repository in the root directory of your mod (where build.gradle is) and include the Gradle buildscript (build.gradle and gradle.properties), the Gradle wrapper (gradlew, gradlew.bat and the gradle directory), the source code and assets (the src directory) and your mod's license and readme files. See my TestMod3 for an example of the proper structure.

 

I'll start debugging it soon.

  • Like 1

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted (edited)

MessageTargetEntity is sending the client-side Repeater ItemStack to the server and MessageTargetEntityHandler is passing the copy of this it read from the byte buffer as an argument to ItemRepeater#attackEntity. This copy's IRepeaterData has its cooldown modified, but this cooldown is never used again and the object is garbage collected. The copy in the server-side player's inventory remains unmodified.

 

Instead of sending an ItemStack in the packet, use the ItemStack already in the player's inventory.

 

Another issue I encountered was this code in ItemRepeater#onUpdate:

int coolDown = data.getCoolDown();

if(coolDown > 0) {
	System.out.println("COOL DOWN");
	data.setCoolDown(coolDown--);
}

 

Because you're using the post-decrement operator, this calls IRepeaterData#setCoolDown with the existing value of the coolDown local variable (setting RepeaterData#coolDown to the same value it already had) and then decrements the coolDown local variable (which is never used again). If you use the pre-decrement operator instead, the cooldown will be decremented as intended.

 

There's no reason to call Object#equals on an ItemStack, ItemStack doesn't override it so it's the same as using the == operator. If you want to compare two ItemStacks by value instead of reference, use the static equality methods in the ItemStack class.

 

Object#finalize shouldn't be called explicitly, it should only be called by the JVM. MessageTargetEntityHandler calls it, even though it doesn't override it and as such will do absolutely nothing.

 

MessageTargetEntityHandler calls ItemRepeater#attackEntity without actually checking if the targeted entity is in range of the player. A malicious client could currently send this packet to attack any entity in the same dimension as the player, regardless of the distance between them. The server should never trust the client.

 

The way the Repeater sets itself as the active item while it's held is a bit hacky and doesn't take into account the off hand. I'd recommend removing this and sending the EnumHand holding the Repeater in MessageTargetEntityHandler instead of using EntityLivingBase#getActiveItemStack.

 

I've fixed the first four issues and pushed the changes to a fork of your repository. You can view or merge the changes here.

Edited by Choonster

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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

    • But your Launcher does not find it   Java path is: /run/user/1000/doc/3f910b8/java Checking Java version... Java checker returned some invalid data we don't understand: Check the Azul Zulu site and select your OS and download the latest Java 8 build for Linux https://www.azul.com/downloads/?version=java-8-lts&package=jre#zulu After installation, check the path and put this path into your Launcher Java settings (Java Executable)
    • Try other builds of pehkui and origins++ until you find a working combination
    • Some Create addons are only compatible with Create 6 or Create 5 - so not both versions at the same time Try older builds of Create Stuff and Additions This is the last build before the Create 6 update: https://www.curseforge.com/minecraft/mc-mods/create-stuff-additions/files/6168370
    • ✅ Crobo Coupon Code: 51k3b0je — Get Your \$25 Amazon Gift Card Bonus! If you’re new to Crobo and want to make the most out of your first transaction, you’ve come to the right place. The **Crobo coupon code: 51k3b0je** is a fantastic way to get started. By using this code, you can unlock an exclusive **\$25 Amazon gift card** after completing your first eligible transfer. Let’s dive deep into how the **Crobo coupon code: 51k3b0je** works, why you should use it, and how to claim your reward. --- 🌟 What is Crobo? Crobo is a trusted, modern platform designed for **international money transfers**. It offers fast, secure, and low-cost transactions, making it a favorite choice for individuals and businesses alike. Crobo is committed to transparency, low fees, and competitive exchange rates. And with promo deals like the **Crobo coupon code: 51k3b0je**, it becomes even more attractive. Crobo focuses on providing customers with: * Quick transfer speeds * Minimal fees * Safe, encrypted transactions * Great referral and promo code rewards When you choose Crobo, you’re choosing a platform that values your time, money, and loyalty. And now with the **Crobo coupon code: 51k3b0je**, you can start your Crobo journey with a **bonus reward**! ---# 💥 What is the Crobo Coupon Code: 51k3b0je? The **Crobo coupon code: 51k3b0je** is a **special promotional code** designed for new users. By entering this code during signup, you’ll be eligible for: ✅ A **\$25 Amazon gift card** after your first qualifying transfer. ✅ Access to Crobo’s referral system to earn more rewards. ✅ The ability to combine with future seasonal Crobo discounts. Unlike generic promo codes that just offer small fee reductions, the **Crobo coupon code: 51k3b0je** directly gives you a tangible, valuable reward — perfect for online shopping or gifting. --- ### 🎯 Why Use Crobo Coupon Code: 51k3b0je? There are many reasons why users choose to apply the **Crobo coupon code: 51k3b0je**: 🌟 **Free bonus reward** — Your first transfer can instantly earn you a \$25 Amazon gift card. 🌟 **Trusted platform** — Crobo is known for secure, fast, and affordable transfers. 🌟 **Easy to apply** — Simply enter **Crobo coupon code: 51k3b0je** at signup — no complicated steps. 🌟 **Referral opportunities** — Once you’ve used **Crobo coupon code: 51k3b0je**, you can invite friends and earn more rewards. 🌟 **Stackable savings** — Pair **Crobo coupon code: 51k3b0je** with Crobo’s ongoing offers or holiday deals for even more benefits. --- ### 📝 How to Use Crobo Coupon Code: 51k3b0je Getting started with **Crobo coupon code: 51k3b0je** is quick and easy. Just follow these steps: 1️⃣ **Download the Crobo app** (available on Google Play Store and Apple App Store) or visit the official Crobo website. 2️⃣ **Start the sign-up process** by entering your basic details (name, email, phone number, etc.). 3️⃣ When prompted, enter **Crobo coupon code: 51k3b0je** in the promo code or coupon code field. 4️⃣ Complete your first transaction — be sure to meet the minimum amount required to qualify for the reward (usually specified in Crobo’s promo terms). 5️⃣ After the transaction is verified, receive your **\$25 Amazon gift card** directly via email or within your Crobo account. --- ### 💡 Tips to Maximize Your Crobo Coupon Code: 51k3b0je Bonus 👉 **Transfer the minimum qualifying amount or more** — this ensures you meet the conditions for the gift card. 👉 **Refer friends after your signup** — Crobo allows users who’ve signed up with codes like **Crobo coupon code: 51k3b0je** to share their own code for extra bonuses. 👉 **Check for additional Crobo promotions** — sometimes Crobo offers seasonal or regional deals that stack with the coupon code. 👉 **Complete your transaction soon after signup** — many bonuses have time limits, so act quickly! --- ### 🚀 Frequently Asked Questions about Crobo Coupon Code: 51k3b0je **Q: Can I use Crobo coupon code: 51k3b0je if I already have a Crobo account?** A: No — the **Crobo coupon code: 51k3b0je** is intended for **new users only**. It must be applied during the initial registration process. --- **Q: How long does it take to get the \$25 Amazon gift card after using Crobo coupon code: 51k3b0je?** A: Typically, the gift card is sent **within a few business days** after your first qualifying transfer is completed and verified. --- **Q: Are there hidden fees when using Crobo coupon code: 51k3b0je?** A: No — Crobo is transparent about its fees. The **Crobo coupon code: 51k3b0je** simply adds a bonus reward without increasing your costs. --- **Q: Can I combine Crobo coupon code: 51k3b0je with other promo codes?** A: The **Crobo coupon code: 51k3b0je** is generally applied as a standalone signup bonus. However, Crobo often offers **ongoing promotions** that may apply to future transactions. ---  📌 Reference Crobo promo code: {51k3b0je} Crobo discount code: {51k3b0je} --- # 🌍 Final Thoughts If you want to enjoy safe, fast, and affordable money transfers with an added bonus, **Crobo coupon code: 51k3b0je** is your best option. Not only will you experience excellent service, but you’ll also earn a **\$25 Amazon gift card** — a reward that you can use immediately for shopping or gifts. 👉 **Don’t wait — sign up today using Crobo coupon code: 51k3b0je and claim your bonus!**
  • Topics

×
×
  • Create New...

Important Information

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