Jump to content

Recommended Posts

Posted

I've been trying to debug this for awhile now, and I'm not sure whether the issue is my understanding of capabilities or if I just need a second pair of eyes to help with my code. The code is intended to work as follows:

1) The client sends a packet containing the player name and an integer to the server.

2) The server gets the PlayerEntity from the name

3) The player's capability is called, and the integer is added to the array.

 

The idea is to store which runes the player inputs from the keyboard, as certain rune combinations will make a spell. Unfortunately, the capability never gets more than one rune in the array, and the index is always at zero or one. My guess is that I am either always getting a new default implementation of the capability when calling the player's capability, or I need to do something extra to store capability data between calls.

 

[spoiler=adding the capability]

public class SpellEventHandler {

@SubscribeEvent
public void onEntityLoad(AttachCapabilitiesEvent.Entity event) {
	event.addCapability(new ResourceLocation(FantasyMod.MODID, "spellCasting"), new ProviderSpellCasting());
}

}

 

 

 

[spoiler=the capability interface]

public interface ISpellCasting {
public void addRune(EntityPlayer p, Runes r);

public int[] getArray();

public void setArray(int[] set);
}

 

 

 

[spoiler=The default implementation]

public class DefaultImplementation implements ISpellCasting {

private Runes[] spell = new Runes[spell.MAX_RUNES_IN_SPELL];
private long[] castTimes = new long[spell.MAX_RUNES_IN_SPELL];

private int index;

private static final float MAX_TIME_BETWEEN_CASTS = 3 * 1000;

public DefaultImplementation() {
	for (int i = 0; i < Spell.MAX_RUNES_IN_SPELL; i++) {
		spell[i] = Runes.EMPTY;
		castTimes[i] = 0;
	}
	index = 0;
}

@Override
public void addRune(EntityPlayer p, Runes r) {
	printArrays();
	long curTime = System.currentTimeMillis();
//		if (index < 0)
//			return;
//		if (index >= Spell.MAX_RUNES_IN_SPELL)
//			clearFirstIndex();
//		if (index > 0 && curTime - castTimes[index - 1] > MAX_TIME_BETWEEN_CASTS)
//			clearArrays();
	spell[index] = r;
	castTimes[index] = curTime;
	index++;
	isValidSpell(p);
}

private boolean isValidSpell(EntityPlayer p) {
	printArrays();
	Spell sp = SpellsCompendium.isValidSpell(spell, index);
	if (sp == null)
		return false;
	sp.cast(p);
	clearArrays();
	return true;
}

private void clearFirstIndex() {
	index = Spell.MAX_RUNES_IN_SPELL - 1;
	for (int i = 1; i < Spell.MAX_RUNES_IN_SPELL; i--) {
		spell[i - 1] = spell[i];
		castTimes[i - 1] = castTimes[i];
	}
}

private void clearArrays() {
	index = 0;
	for (int i = 0; i < Spell.MAX_RUNES_IN_SPELL; i++) {
		spell[i] = Runes.EMPTY;
		castTimes[i] = 0;
	}
}

private void printArrays() {
	for (int i = 0; i < Spell.MAX_RUNES_IN_SPELL; i++) {
		System.out.print(Runes.getInt(spell[i]) + " ");
	}
	System.out.println("\n" + index);
}

@Override
public int[] getArray() {
	int[] ret = new int[1 + index];
	ret[0] = index;
	for (int i = 1; i < ret.length; i++) {
		ret[i] = Runes.getInt(spell[i - 1]);
	}
	return null;
}

@Override
public void setArray(int[] set) {
	index = set[0];
	for (int i = 1; i < set.length; i++) {
		spell[i - 1] = Runes.getRune(set[i]);
	}
}

}
[/spoiler]

[spoiler=The capability registration]
public class CapabilitySpellCasting {
@CapabilityInject(ISpellCasting.class)
public static final Capability<ISpellCasting> SPELL_CASTING_CAPABILITY = null;

public static void register() {
	CapabilityManager.INSTANCE.register(ISpellCasting.class, new Storage(), DefaultImplementation.class);
}

public static class Storage implements IStorage<ISpellCasting> {

	@Override
	public NBTBase writeNBT(Capability<ISpellCasting> capability, ISpellCasting instance, EnumFacing side) {
		return new NBTTagIntArray(instance.getArray());
	}

	@Override
	public void readNBT(Capability<ISpellCasting> capability, ISpellCasting instance, EnumFacing side,
			NBTBase nbt) {
		instance.setArray(((NBTTagIntArray)nbt).getIntArray());
	}
}
}

 

 

 

[spoiler=The capability provider]

public class ProviderSpellCasting implements ICapabilityProvider {

@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
	return CapabilitySpellCasting.SPELL_CASTING_CAPABILITY != null
			&& capability == CapabilitySpellCasting.SPELL_CASTING_CAPABILITY;
}

@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
	if (hasCapability(capability, facing))
		return CapabilitySpellCasting.SPELL_CASTING_CAPABILITY
				.<T> cast(CapabilitySpellCasting.SPELL_CASTING_CAPABILITY.getDefaultInstance());
	return null;
}

}

 

 

 

[spoiler=Calling the capability]

public static class Handler implements IMessageHandler<SpellMessage, IMessage> {
	@Override
	public IMessage onMessage(SpellMessage message, MessageContext ctx) {
		System.out
				.println("Got Spell Packet from: " + message.player + " with rune: " + Runes.getInt(message.rune));
		World world = ctx.getServerHandler().playerEntity.worldObj;
		EntityPlayer player = world.getPlayerEntityByName(message.player);
		ISpellCasting cap = player.getCapability(CapabilitySpellCasting.SPELL_CASTING_CAPABILITY, null);
		cap.addRune(player, message.rune);
		return null;
	}
}

 

 

 

I don't need to store the capability between different world loadings, so the serializable interface isn't something that I spent much time looking into. Could that be the issue? Any help would be appreciated.

"Beware of bugs in the above code; I have only proved it correct, not tried it." - Donald Knuth

Posted

1. No, you don't need to store Capability.

 

2. In packets client->server that are "acting upon" player who issending them, you don't need to send player.

* Server knows who sent a packet to it.

* ctx.getServerHandler().playerEntity

* Only thing you need is rune pressed.

 

3. Your code is not thread safe:

http://greyminecraftcoder.blogspot.com.au/2015/01/thread-safety-with-network-messages.html

 

* Whole thing in your handler should be in runnable.

 

4. I didn't look for Java mistakes, I will if above will not fix the problem.

  Quote

1.7.10 is no longer supported by forge, you are on your own.

Posted

When you attach a capability using

AttachCapabilitiesEvent

or

Item#initCapabilities

, it will only be saved to NBT if the

ICapabilityProvider

also implements

INBTSerializable

.

 

Forge provides the

ICapabilitySerializable

interface, which simply extends

ICapabilityProvider

and

INBTSerializable

.

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

Alright, I have modified the message handler to be thread safe and removed the unnecessary player variable, thanks for those tips Ernio. I also changed my capability provider to implement ICapabilitySerializable. Unfortunately, I am still running into the same problem: when I send a rune to the server, the console prints out the rune array with only the first index filled. I know that the index is being incremented, so every time that I send a packet it seems to be interacting with a new DefaultImplementation object.

 

[spoiler=Default Implementation]

public class DefaultImplementation implements ISpellCasting {

private Runes[] spell = new Runes[spell.MAX_RUNES_IN_SPELL];
private long[] castTimes = new long[spell.MAX_RUNES_IN_SPELL];

private int index;

private static final float MAX_TIME_BETWEEN_CASTS = 3 * 1000;

public DefaultImplementation() {
	for (int i = 0; i < Spell.MAX_RUNES_IN_SPELL; i++) {
		spell[i] = Runes.EMPTY;
		castTimes[i] = 0;
	}
	index = 0;
}

@Override
public void addRune(EntityPlayer p, Runes r) {
	printArrays();
	long curTime = System.currentTimeMillis();
//		if (index < 0)
//			return;
//		if (index >= Spell.MAX_RUNES_IN_SPELL)
//			clearFirstIndex();
//		if (index > 0 && curTime - castTimes[index - 1] > MAX_TIME_BETWEEN_CASTS)
//			clearArrays();
	spell[index] = r;
	castTimes[index] = curTime;
	index++;
	isValidSpell(p);
}

private boolean isValidSpell(EntityPlayer p) {
	printArrays();
	Spell sp = SpellsCompendium.isValidSpell(spell, index);
	if (sp == null)
		return false;
	sp.cast(p);
	clearArrays();
	return true;
}

private void clearFirstIndex() {
	index = Spell.MAX_RUNES_IN_SPELL - 1;
	for (int i = 1; i < Spell.MAX_RUNES_IN_SPELL; i--) {
		spell[i - 1] = spell[i];
		castTimes[i - 1] = castTimes[i];
	}
}

private void clearArrays() {
	index = 0;
	for (int i = 0; i < Spell.MAX_RUNES_IN_SPELL; i++) {
		spell[i] = Runes.EMPTY;
		castTimes[i] = 0;
	}
}

private void printArrays() {
	for (int i = 0; i < Spell.MAX_RUNES_IN_SPELL; i++) {
		System.out.print(Runes.getInt(spell[i]) + " ");
	}
	System.out.println("\n" + index);
}

private int[] runesToIntArray() {
	int[] ret = new int[spell.MAX_RUNES_IN_SPELL];
	for (int i = 0; i < spell.length; i ++) {
		ret[i] = Runes.getInt(spell[i]);
	}
	return ret;
}

private Runes[] intToRunesArray(int[] ary) {
	Runes[] ret = new Runes[spell.MAX_RUNES_IN_SPELL];
	for (int i = 0; i < ary.length; i ++) {
		ret[i] = Runes.getRune(ary[i]);
	}
	return ret;
}

@Override
public NBTTagCompound getNBT() {
	NBTTagCompound tag = new NBTTagCompound();
	tag.setInteger("index", index);
	tag.setIntArray("spell", runesToIntArray());
	return tag;
}

@Override
public void setNBT(NBTTagCompound comp) {
	index = comp.getInteger("index");
	spell = intToRunesArray(comp.getIntArray("spell"));
}

}

 

 

 

[spoiler=Registration and storage for the capability]

public class CapabilitySpellCasting {
@CapabilityInject(ISpellCasting.class)
public static final Capability<ISpellCasting> SPELL_CASTING_CAPABILITY = null;

public static void register() {
	CapabilityManager.INSTANCE.register(ISpellCasting.class, new Storage(), DefaultImplementation.class);
}

public static class Storage implements IStorage<ISpellCasting> {

	@Override
	public NBTTagCompound writeNBT(Capability<ISpellCasting> capability, ISpellCasting instance, EnumFacing side) {
		return instance.getNBT();
	}

	@Override
	public void readNBT(Capability<ISpellCasting> capability, ISpellCasting instance, EnumFacing side,
			NBTBase nbt) {
		instance.setNBT((NBTTagCompound)nbt);
	}
}
}

 

 

 

[spoiler=The new provider code]

public class ProviderSpellCasting implements ICapabilitySerializable<NBTTagCompound> {

ISpellCasting instance = CapabilitySpellCasting.SPELL_CASTING_CAPABILITY.getDefaultInstance();

@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
	return CapabilitySpellCasting.SPELL_CASTING_CAPABILITY != null
			&& capability == CapabilitySpellCasting.SPELL_CASTING_CAPABILITY;
}

@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
	if (hasCapability(capability, facing))
		return CapabilitySpellCasting.SPELL_CASTING_CAPABILITY
				.<T> cast(CapabilitySpellCasting.SPELL_CASTING_CAPABILITY.getDefaultInstance());
	return null;
}

@Override
public NBTTagCompound serializeNBT() {
	return (NBTTagCompound) CapabilitySpellCasting.SPELL_CASTING_CAPABILITY.getStorage().writeNBT(CapabilitySpellCasting.SPELL_CASTING_CAPABILITY, instance, null);
}

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

}

 

 

 

[spoiler=The new message handler code]

public static class Handler implements IMessageHandler<SpellMessage, IMessage> {
	@Override
	public IMessage onMessage(final SpellMessage message, final MessageContext ctx) {
		System.out
				.println("Got Spell Packet from: " + message.player + " with rune: " + Runes.getInt(message.rune));
		ctx.getServerHandler().playerEntity.getServer().addScheduledTask(new Runnable() {
			public void run() {
				addMessageRune(ctx.getServerHandler().playerEntity, message.rune);
			}
		});			
		return null;
	}

	public void addMessageRune(EntityPlayer player, Runes rune) {
		ISpellCasting cap = player.getCapability(CapabilitySpellCasting.SPELL_CASTING_CAPABILITY, null);
		cap.addRune(player, rune);
	}
}

 

 

 

Could the problem be in my provider's getCapability? That seems correct to me, but I wouldn't be shocked if I was wrong.

"Beware of bugs in the above code; I have only proved it correct, not tried it." - Donald Knuth

Posted
  On 6/2/2016 at 5:44 PM, Snoopy_WWI_Ace said:

Could the problem be in my provider's getCapability? That seems correct to me, but I wouldn't be shocked if I was wrong.

 

That is exactly the issue.

Capability#getDefaultInstance

creates a new instance of the default implementation. Your

getCapability

method is creating and returning a new instance every time it's called instead of using the instance you stored in the

instance

field.

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

    • Looks like an issue with biomeswevegone
    • In the server.properties, set max-tick-time to -1   if there are further issues, make a test without distanthorizons
    • Verified user can get a $300 off TℰℳU Coupon code using the code ((“{{ '[''com31844'']}}”)). This TℰℳU $100Off code is specifically for new and existing customers both and can be redeemed to receive a $100discount on your purchase. Our exclusive TℰℳU Coupon code offers a flat $100off your purchase, plus an additional 100% discount on top of that. You can slash prices by up to $100as a new TℰℳU customer using code ((“{{ '[''com31844'']}}”)). Existing users can enjoy $100off their next haul with this code. But that’s not all! With our TℰℳU Coupon codes for 2025, you can get up to 90% discount on select items and clearance sales. Whether you’re a new customer or an existing shopper, our TℰℳU codes provide extra discounts tailored just for you. Save up to 100% with these current TℰℳU Coupons ["^"{{ '[''com31844'']}} "^"] for April 2025. The latest TℰℳU coupon codes at here. New users at TℰℳU receive a $100discount on orders over $100Use the code ((“{{ '[''com31844'']}}”)) during checkout to get TℰℳU Coupon $100Off For New Users. You can save $100Off your first order with the coupon code available for a limited time only. TℰℳU 90% Off promo code ((“{{ '[''com31844'']}}”)) will save you $100on your order. To get a discount, click on the item to purchase and enter the coe. Yes, offers $100Off coupon code “{{ '[''com31844'']}}” for first time users. You can get a $100bonus plus $100Off any purchase at TℰℳU with the $100Coupon Bundle at TℰℳU if you sign up with the referral code ((“{{ '[''com31844'']}}”)) and make a first purchase of $100or more. Free TℰℳU codes $100off — ((“{{ '[''com31844'']}}”)) Get a $100discount on your TℰℳU order with the promo code "{{ '[''com31844'']}}". You can get a discount by clicking on the item to purchase and entering this TℰℳU Coupon code $100off ((“{{ '[''com31844'']}}”)). ŢℰNew User Coupon ((“{{ '[''com31844'']}})): Up To $100OFF For First-Time Users Our TℰℳU first-time user coupon codes are designed just for new customers, offering the biggest discounts and the best deals currently available on TℰℳU To maximize your savings, download the TℰℳU app and apply our TℰℳU new user coupon during checkout. TℰℳU Coupon Codes For Existing Users ((“{{ '[''com31844'']}}”)): $100Price Slash Have you been shopping on TℰℳU or a while? Our TℰℳU Coupon for existing customers is here to reward you for your continued support, offering incredible discounts on your favorite products. TℰℳU Coupon For $100Off ((“{{ '[''com31844'']}}”)): Get A Flat $100Discount On Order Value Get ready to save big with our incredible TℰℳU Coupon for $100off! Our amazing ŢℰM$100off coupon code will give you a flat $100discount on your order value, making your shopping experience even more rewarding. TℰℳU Coupon Code For $100Off ((“{{ '[''com31844'']}}”)): For Both New And Existing Customers Our incredible TℰℳU Coupon code for $100off is here to help you save big on your purchases. Whether you’re a new user or an existing customer, our $100off code for TℰℳU will give you an additional discount! TℰℳU Coupon Bundle ((“{{ '[''com31844'']}}”)): Flat $100Off + Up To $100Discount Get ready for an unbelievable deal with our TℰℳU Coupon bundle for 2025! Our TℰℳU Coupon bundles will give you a flat $100discount and an additional $100off on top of it. Free TℰℳU Coupons ((“{{ '[''com31844'']}}”)): Unlock Unlimited Savings! Get ready to unlock a world of savings with our free TℰℳU Coupons! We’ve got you covered with a wide range of TℰℳU Coupon code options that will help you maximize your shopping experience. 100% Off TℰℳU Coupons, Promo Codes + 25% Cash Back ((“{{ '[''com31844'']}}”)) Redeem TℰℳU Coupon Code ((“{{ '[''com31844'']}}”)) TℰℳU Coupon $100OFF ((“{{ '[''com31844'']}}”)) TℰℳU Coupon $100OFF FOR EXISTING CUSTOMERS ((“{{ '[''com31844'']}}”)) TℰℳU Coupon $100OFF FIRST ORDER ((“{{ '[''com31844'']}}”)) TℰℳU Coupon $100OFF REDDIT ((“{{ '[''com31844'']}}”)) TℰℳU Coupon $100OFF FOR EXISTING CUSTOMERS REDDIT ((“{{ '[''com31844'']}}”)) TℰℳU $100OFF CODE ((“{{ '[''com31844'']}}”)) TℰℳU 70 OFF COUPON 2025 ((“{{ '[''com31844'']}}”)) DOMINOS 70 RS OFF COUPON CODE ((“{{ '[''com31844'']}}”)) WHAT IS A COUPON RATE ((“{{ '[''com31844'']}}”)) TℰℳU $100OFF FOR EXISTING CUSTOMERS ((“{{ '[''com31844'']}}”)) TℰℳU $100OFF FIRST ORDER ((“{{ '[''com31844'']}}”)) TℰℳU $100OFF FREE SHIPPING ((“{{ '[''com31844'']}}”)) You can get an exclusive $100off discount on your TℰℳU purchase with the code [{{ '[''com31844'']}}] Or [{{ '[''com31844'']}}].This code is specially designed for new customers and offers a significant price cut on your shopping. Make your first purchase on TℰℳU more rewarding by using this code to get $100off instantly.   TℰℳU Coupon Code For $100Off [{{ '[''com31844'']}}] Or [{{ '[''com31844'']}}]: Get A Flat $100Discount On Order Value Get ready to save big with our incredible TℰℳU coupon for $100off! Our coupon code will give you a flat $100discount on your order value, making your shopping experience even more rewarding.   Exclusive TℰℳU Discount Code [{{ '[''com31844'']}}] Or [{{ '[''com31844'']}}]: Flat $300 OFF for New and Existing Customers Using our TℰℳU promo code you can get A$ 200 off your order and 100% off using our TℰℳU promo code [{{ '[''com31844'']}}] Or [{{ '[''com31844'']}}]. As a new TℰℳU customer, you can save up to $100using this promo code. For returning users, our TℰℳU promo code offers a $100price slash on your next shopping spree. This is our way of saying thank you for shopping with us! Get ready to save big with our incredible TℰℳU Coupon code for $300 off! Our amazing TℰℳU $300 off coupon code will give you a flat $300 discount on your order value, making your shopping experience even more rewarding.   TℰℳU Coupon Code For 40% Off ["{com31844}"] For Both New And Existing Customers   Our incredible TℰℳU Coupon code for 40% off is here to help you save big on your purchases. Whether you’re a new user or an existing customer, our 40% off code for TℰℳU will give you an additional discount!   TℰℳU Coupon Bundle ["{com31844}"]: Flat $300 Off + Up To 90% Discount   Get ready for an unbelievable deal with our TℰℳU Coupon bundle for 2025! Our TℰℳU Coupon bundles will give you a flat $300 discount and an additional 40% off on top of it.   Free TℰℳU Coupons ["{com31844}"]: Unlock Unlimited Savings!   Get ready to unlock a world of savings with our free TℰℳU Coupons! We’ve got you covered with a wide range of TℰℳU Coupon code options that will help you maximize your shopping experience.   50% Off TℰℳU Coupons, Promo Codes + 25% Cash Back ["{com31844}"]   Redeem TℰℳU Coupon Code ["{com31844}"]   TℰℳU Coupon $300 OFF ["{com31844}"]   TℰℳU Coupon $300 OFF ["{com31844}"]   TℰℳU Coupon $300 OFF FOR EXISTING CUSTOMERS ["{com31844}"]   TℰℳU Coupon $300 OFF FIRST ORDER ["{com31844}"]   TℰℳU Coupon $300 OFF FIRST ORDER ["{com31844}"]   TℰℳU Coupon $300 OFF FOR EXISTING CUSTOMERS FREE SHIPPING USA ["{com31844}"]   TℰℳU Coupon $300 OFF HOW DOES IT WORK ["{com31844}"]   TℰℳU Coupon $300 OFF FOR EXISTING CUSTOMERS CANADA ["{com31844}"]   TℰℳU Coupon $300 OFF 2025 ["{com31844}"]   TℰℳU Coupon $300 OFF FOR NEW CUSTOMERS ["{com31844}"]   TℰℳU Coupon $300 OFF CANADA ["{com31844}"]   TℰℳU Coupon $300 OFF FOR EXISTING CUSTOMERS FIRST ORDER ["{com31844}"]   TℰℳU 300 OFF COUPON BUNDLE ["{com31844}"]   This TℰℳU coupon $300 off is designed to provide substantial savings on your purchases, making shopping for your favorite items easier than ever. With our $300 off TℰℳU coupon, you'll be amazed at how much you can save on your next order. Here are the key benefits you can expect when using our coupon code {com31844} "OR" {com31844}: {com31844} "OR" {com31844}: Flat $300 off on your purchase {com31844} "OR" {com31844}: $300 coupon pack for multiple uses {com31844} "OR" {com31844}: $300 flat discount for new customers {com31844} "OR" {com31844}: Extra $300 promo code for existing customers {com31844} "OR" {com31844}: $300 coupon exclusively for USA/Canada users H2: TℰℳU Coupon Code $300 Off For New Users In 2024 New users can reap the highest benefits by using our coupon code on the TℰℳU app. This TℰℳU coupon $300 off is specifically designed to welcome new customers with incredible savings  
    • Now the serve runs for a bit, then crashes after 20 minutes of running, what's the issue here? apologies if I need to start a new post and didn't https://pastebin.com/uBpp5bCz  
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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