Jump to content

Recommended Posts

Posted

I'm trying to give every player a money variable, but I'm having some trouble. I have created a connection handler like so;

	@EventHandler
public void load(FMLInitializationEvent event) {
	proxy.init();

	NetworkRegistry.instance().registerConnectionHandler(new ConnectionHandler());
}

 

In the playerLoggedIn method I then do this:

	@Override
public void playerLoggedIn(Player player, NetHandler netHandler, INetworkManager manager) {
	EntityPlayer entityPlayer = (EntityPlayer) player;
	System.out.println("Player joined!");
	NBTTagCompound tag = entityPlayer.getEntityData();

	NBTBase modeTag = tag.getTag("credits");
	if(modeTag == null) {
		XXXX.credits.put(entityPlayer.username, 30);
		tag.setInteger("credits", 30);
	} else {
		XXXX.credits.put(entityPlayer.username, ((NBTTagInt)modeTag).data);
	}

	ByteArrayOutputStream bos = new ByteArrayOutputStream(4);
	DataOutputStream outputStream = new DataOutputStream(bos);

	int credits = 0;

	for (String key : XXXX.credits.keySet()) {
		if(key.equalsIgnoreCase(((EntityPlayer)player).username))credits = XXXX.credits.get(key);
	}

	try {
		outputStream.writeInt(credits);
	} catch (Exception ex) {
		ex.printStackTrace();
	}

	Packet250CustomPayload packet = new Packet250CustomPayload();
	packet.channel = "CreditValues";
	packet.data = bos.toByteArray();
	packet.length = bos.size();

	PacketDispatcher.sendPacketToPlayer(packet, player);
}

Where XXXX.credits is a HashMap like so:

public static HashMap<String, Integer> credits = new HashMap<String, Integer>();

 

The packet is then handled like so:

	@Override
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player playerEntity) {

	if (packet.channel.equals("CreditVaules")) {
		handleValues(packet);
	}

private void handleValues(Packet250CustomPayload packet) {
	Side side = FMLCommonHandler.instance().getEffectiveSide();
	if (side.isClient()) {
		DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));

		int credits = 0;

		try {
			credits = inputStream.readInt();
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}

		XXXX.myCredits = credits;
	}
}
}

Where myCredits is an integer to hold the individual client's credits.

 

I then print this number onto the screen, However, it is always 0. I don't know why this is the case. Can someone help?

 

NOTE: I'm drunk when writing this so if something doesn't make sense just ask.

Posted

yes, no, hi, im also playing with money in my mod

 

<optional> my mod requires that the player can receives money even while offline, so its store in a database isntead of the nbt</optional>

 

you should use IExtendedEntityProperties instead

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

Posted

How is this used properly for both client and server?

If you check for instanceof EntityPlayer in the EntityConstructing event a dummy of your data will also be present on the client. Use packets to sync the data between the client & server version.

 

I've basically done just that..

 

My main data class now looks like:

package mods.XXXX;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;

import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.minecraft.world.World;
import net.minecraftforge.common.IExtendedEntityProperties;

public class PlayerVariables implements IExtendedEntityProperties {

public static final String IDENTIFIER = "xxxxvalues";

public static PlayerVariables fromPlayer(EntityPlayerMP player) {
	return (PlayerVariables)player.getExtendedProperties(IDENTIFIER);
}

int xxx = 0;
int yyy = 68;

EntityPlayerMP player;

@Override
public void saveNBTData(NBTTagCompound compound) {
	compound.setInteger("xxx", xxx);
	compound.setInteger("yyy", yyy);
}

@Override
public void loadNBTData(NBTTagCompound compound) {
	if (compound.hasKey("xxx")) {
		this.addXxx(compound.getInteger("xxx"));
	} else {
		this.addXxx(30);
	}

	if (compound.hasKey("yyy")) {
		this.useYyy(68 - (68 - compound.getInteger("yyy")));
	} else {
		this.useYyy(0);
	}
}

@Override
public void init(Entity entity, World world) {
	player = (EntityPlayerMP) entity;
}

public void addXxx(int value) {
	xxx += value;
	sendPacket();
}

public boolean removeXxx(int value) {
	if (xxx > value) {
		xxx -= value;
		sendPacket();
		return true;
	} else {
		return false;
	}
}

public boolean useYyy(int value) {
	if (yyy > value) {

		yyy -= value;

		sendPacket();
		return true;
	} else {
		return false;
	}
}

private void sendPacket() {
	ByteArrayOutputStream bos = new ByteArrayOutputStream(;
	DataOutputStream outputStream = new DataOutputStream(bos);
	try {
		outputStream.writeInt(yyy);
		outputStream.writeInt(xxx);
	} catch (Exception ex) {
		ex.printStackTrace();
	}

	Packet250CustomPayload packet = new Packet250CustomPayload();
	packet.channel = "XxxxValues";
	packet.data = bos.toByteArray();
	packet.length = bos.size();

	PacketDispatcher.sendPacketToPlayer(packet, (Player)player);
}
}

 

The entity constructor code is..

package mods.XXXX;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;

import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player;

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.minecraftforge.event.EventPriority;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;

public class EntityConstructListener {

@ForgeSubscribe(priority = EventPriority.NORMAL)
public void onEntityConstuct(EntityConstructing event) {
	if (event.entity instanceof EntityPlayerMP) {
		EntityPlayerMP ep = (EntityPlayerMP) event.entity;
		ep.registerExtendedProperties(PlayerVariables.IDENTIFIER, new PlayerVariables());

	}
}
}

 

My packet handler is as so:

package mods.XXXX;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

import mods.XXXX.client.ClientProxy;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet250CustomPayload;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.Player;
import cpw.mods.fml.relauncher.Side;

public class PacketHandler implements IPacketHandler {

@Override
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player playerEntity) {

	Side side = FMLCommonHandler.instance().getEffectiveSide();

	if (packet.channel.equals("Some other packet") && side == Side.SERVER) {
		handleXxxx(packet, playerEntity); // method is hidden <- this one already works
	}

	if (packet.channel.equals("XxxVaules") && side == Side.CLIENT) {
		handleValues(packet);
	}
}

private void handleValues(Packet250CustomPayload packet) {
	DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));

	int yyy = 0;
	int xxx = 0;

	try {
		yyy = inputStream.readInt();
		xxx = inputStream.readInt();
	} catch (IOException e) {
		e.printStackTrace();
		return;
	}

	ClientProxy.xxx = xxx;
	ClientProxy.yyy = yyy;
}
}

I then use ClientProxy.xxx and ClientProxy.yyy as variables in calculations for a graphical overlay. However, the variables do not set to default when testing on singleplayer, but both are at 0. Why are they 0, they should be send from the data.

Posted

1: can you NOT use xxx yyy as variable name, it makes the code 1000 000 times harder to read

-if your variables are actually called xxx yyy it is VERY shitty name btw

 

2: can you println in your packet handler (client side) to check if you are actually receiving the packet ?

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

Posted

you always ahve one or the other

 

if not tell us from where and well point you to the right direction :)

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

Posted

This is my packet handler, the System.out.println isn't called when on a singleplayer game

package mods.SaberMod;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

import mods.SaberMod.client.ClientProxy;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet250CustomPayload;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.Player;
import cpw.mods.fml.relauncher.Side;

public class PacketHandler implements IPacketHandler {

@Override
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player playerEntity) {

	Side side = FMLCommonHandler.instance().getEffectiveSide();

	if (packet.channel.equals("SaberExtend") && side == Side.SERVER) {
		handleExtend(packet, playerEntity);
	}

	if (packet.channel.equals("SaberVaules") && side == Side.CLIENT) {
		handleValues(packet);
	}
}

private void handleExtend(Packet250CustomPayload packet, Player playerEntity) {

	DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));

	int stage = 0;
	int damage = 0;

	try {
		stage = inputStream.readInt();
		damage = inputStream.readInt();
	} catch (IOException e) {
		e.printStackTrace();
		return;
	}

	stage = (stage == 0) ? 1 : 0;

	EntityPlayerMP player = (EntityPlayerMP) playerEntity;
	player.inventory.setInventorySlotContents(player.inventory.currentItem, new ItemStack((256 + SaberMod.BASE_ITEM_ID + stage), 1, damage));
	player.updateHeldItem();
}

private void handleValues(Packet250CustomPayload packet) {
	DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));

	Minecraft mc;
	mc = Minecraft.getMinecraft();
	mc.getLogAgent().logFine("Saber Values packet recieved.");

	int force = 0;
	int credits = 0;

	try {
		force = inputStream.readInt();
		credits = inputStream.readInt();
	} catch (IOException e) {
		e.printStackTrace();
		return;
	}

	ClientProxy.credits = credits;
	ClientProxy.force = force;
}
}

Posted

more importantly

 

@Override

public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player playerEntity) {

System.out.println("i was called");

}

would this be called ? because if this isnt called then you are not registering your packet handlers correctly :)

 

 

 

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

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

    • UPDATE: this seems to be an Arch-specific issue. Switching to Ubuntu server fixed EVERYTHING.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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