Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ssw.Guns.event;
- import java.io.ByteArrayOutputStream;
- import java.io.DataOutputStream;
- import java.util.ArrayList;
- import java.util.List;
- 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;
- import com.mrcreppo.artefact.src.inventory.InventoryCustomPlayer;
- import com.ssw.Guns.common.CommonProxy;
- import cpw.mods.fml.common.network.PacketDispatcher;
- import cpw.mods.fml.common.network.Player;
- public class ExtendedPlayer implements IExtendedEntityProperties
- {
- /*
- Here I create a constant EXT_PROP_NAME for this class of properties. You need a unique name for every instance of IExtendedEntityProperties you make, and doing it at the top of each class as a constant makes
- it very easy to organize and avoid typos. It's easiest to keep the same constant name in every class, as it will be distinguished by the class name: ExtendedPlayer.EXT_PROP_NAME vs. ExtendedEntity.EXT_PROP_NAME
- Note that a single entity can have multiple extended properties, so each property should have a unique name. Try to come up with something more unique than the tutorial example.
- */
- public final static String EXT_PROP_NAME = "ExtendedPlayerSSW";
- public final InventoryCustomPlayer inventory = new InventoryCustomPlayer();
- /*
- public static final int CLAN = 2;
- public static final int CLAN_LEVEL = 3;
- public static final int CLAN_POINTS = 4;
- public static final String CLAN_NAME = "";
- public static final int CLAN_LEADER = 11;
- */
- /*
- public static final int HAIR_WATCHER = 13;
- public static final int HAIR_COLOR_WATCHER = 11;
- public static final int SKIN_COLOR_WATCHER = 12;
- public static final int CHAR_END_WATCHER = 14;
- */
- // I always include the entity to which the properties belong for easy access
- // It's final because we won't be changing which player it is
- private final EntityPlayer player;
- // Declare other variables you want to add here
- // We're adding mana to the player, so we'll need current and max mana
- private float rad;
- private float weight;
- private float bWeight;
- private float maxWeight;
- private float stamina;
- private int pickup;
- private int radModifer;
- private int bioModifer;
- private int electroModifer;
- private int fireModifer;
- private int psyModifer;
- private int hitModifer;
- private int armorModifer;
- private int staminaModifer;
- private int weightModifer;
- private int deposit;
- private boolean exitTrade;
- private int energyTimeModifer;
- /*
- The default constructor takes no arguments, but I put in the Entity so I can initialize the above variable 'player'
- Also, it's best to initialize any other variables you may have added, just like in any constructor.
- */
- private ArrayList modiferList = new ArrayList();
- //private ArrayList timeModiferList = new ArrayList();
- public ExtendedPlayer(EntityPlayer player)
- {
- this.player = player;
- this.rad = 0;
- this.weight = 1;
- this.bWeight = 1;
- this.maxWeight = 60;
- this.stamina = 100;
- this.pickup = 0;
- this.radModifer = 0;
- this.bioModifer = 0;
- this.electroModifer = 0;
- this.fireModifer = 0;
- this.psyModifer = 0;
- this.hitModifer = 0;
- this.armorModifer = 0;
- this.staminaModifer = 0;
- this.weightModifer = 0;
- this.deposit = 3000;
- this.exitTrade = false;
- this.energyTimeModifer = 0;
- //this.player.getDataWatcher().addObject(DEPOSIT_WATCHER, this.deposit);
- }
- /**
- * Used to register these extended properties for the player during EntityConstructing event
- * This method is for convenience only; it will make your code look nicer
- */
- public static final void register(EntityPlayer player)
- {
- player.registerExtendedProperties(ExtendedPlayer.EXT_PROP_NAME, new ExtendedPlayer(player));
- }
- /**
- * Returns ExtendedPlayer properties for player
- * This method is for convenience only; it will make your code look nicer
- */
- public static final ExtendedPlayer get(EntityPlayer player)
- {
- return (ExtendedPlayer) player.getExtendedProperties(EXT_PROP_NAME);
- }
- // Save any custom data that needs saving here
- @Override
- public void saveNBTData(NBTTagCompound compound)
- {
- // We need to create a new tag compound that will save everything for our Extended Properties
- NBTTagCompound properties = new NBTTagCompound();
- // We only have 2 variables currently; save them both to the new tag
- this.inventory.writeToNBT(properties);
- properties.setFloat("RAD", this.rad);
- properties.setFloat("WEIGHT", this.weight);
- properties.setFloat("BWEIGHT", this.bWeight);
- properties.setFloat("MAXWEIGHT", this.maxWeight);
- properties.setFloat("STAMINA", this.stamina);
- properties.setInteger("PICKUP", this.pickup);
- properties.setInteger("RM", this.radModifer);
- properties.setInteger("BM", this.bioModifer);
- properties.setInteger("EM", this.electroModifer);
- properties.setInteger("FM", this.fireModifer);
- properties.setInteger("PM", this.psyModifer);
- properties.setInteger("HM", this.hitModifer);
- properties.setInteger("AM", this.armorModifer);
- properties.setInteger("SM", this.staminaModifer);
- properties.setInteger("WM", this.weightModifer);
- properties.setInteger("DEPOSIT", this.deposit);
- properties.setBoolean("EXIT", this.exitTrade);
- properties.setInteger("ETM", this.energyTimeModifer);
- /*
- properties.setInteger("Clan_Level", this.clan_level);
- properties.setInteger("Clan_Points", this.clan_points);
- properties.setInteger("Clan_Leader", this.clan_leader);
- properties.setInteger("Clan_Invited", this.clan_invited);
- /*
- properties.setInteger("Head", this.player.getDataWatcher().getWatchableObjectInt(HEAD_TYPE_WATCHER));
- properties.setInteger("Hair", this.player.getDataWatcher().getWatchableObjectInt(HAIR_WATCHER));
- properties.setInteger("HairColor", this.player.getDataWatcher().getWatchableObjectInt(HAIR_COLOR_WATCHER));
- properties.setInteger("SkinColor", this.player.getDataWatcher().getWatchableObjectInt(SKIN_COLOR_WATCHER));
- properties.setInteger("CharCreateEnd", this.player.getDataWatcher().getWatchableObjectInt(CHAR_END_WATCHER));
- */
- /*
- Now add our custom tag to the player's tag with a unique name (our property's name). This will allow you to save multiple types of properties and distinguish between them. If you only have one type, it isn't as important, but it will still avoid conflicts between your tag names and vanilla tag names. For instance, if you add some "Items" tag, that will conflict with vanilla. Not good. So just use a unique tag name.
- */
- compound.setTag(EXT_PROP_NAME, properties);
- }
- // Load whatever data you saved
- @Override
- public void loadNBTData(NBTTagCompound compound)
- {
- // Here we fetch the unique tag compound we set for this class of Extended Properties
- NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME);
- // Get our data from the custom tag compound
- /*
- this.player.getDataWatcher().updateObject(SEX_WATCHER, properties.getInteger("Sex"));
- this.player.getDataWatcher().updateObject(RACE_WATCHER, properties.getInteger("Race"));
- this.player.getDataWatcher().updateObject(HEAD_TYPE_WATCHER, properties.getInteger("Head"));
- this.player.getDataWatcher().updateObject(HAIR_WATCHER, properties.getInteger("Hair"));
- this.player.getDataWatcher().updateObject(HAIR_COLOR_WATCHER, properties.getInteger("HairColor"));
- this.player.getDataWatcher().updateObject(SKIN_COLOR_WATCHER, properties.getInteger("SkinColor"));
- this.player.getDataWatcher().updateObject(CHAR_END_WATCHER, properties.getInteger("CharCreateEnd"));
- */
- this.inventory.readFromNBT(properties);
- this.rad = properties.getFloat("RAD");
- this.weight = properties.getFloat("WEIGHT");
- this.bWeight = properties.getFloat("BWEIGHT");
- this.maxWeight = properties.getFloat("MAXWEIGHT");
- this.pickup = properties.getInteger("PICKUP");
- this.radModifer = properties.getInteger("RM");
- this.bioModifer = properties.getInteger("BM");
- this.electroModifer = properties.getInteger("EM");
- this.fireModifer = properties.getInteger("FM");
- this.psyModifer = properties.getInteger("PM");
- this.hitModifer = properties.getInteger("HM");
- this.armorModifer = properties.getInteger("AM");
- this.staminaModifer = properties.getInteger("SM");
- this.weightModifer = properties.getInteger("WM");
- this.deposit = properties.getInteger("DEPOSIT");
- this.exitTrade = properties.getBoolean("EXIT");
- this.energyTimeModifer = properties.getInteger("ETM");
- /*
- this.clan_level = properties.getInteger("Clan_Level");
- this.clan_points = properties.getInteger("Clan_Points");
- */
- System.out.println("Loading Player Data");
- // Just so you know it's working, add this line:
- //System.out.println("[TUT PROPS] Beard from NBT: " + this.beard);
- }
- /*
- I personally have yet to find a use for this method. If you know of any,
- please let me know and I'll add it in!
- */
- @Override
- public void init(Entity entity, World world)
- {
- }
- public final float getRad()
- {
- return this.rad;
- }
- public final float getWeight()
- {
- return this.weight;
- }
- public final float getbWeight()
- {
- return this.bWeight;
- }
- public final float getMaxWeight()
- {
- return this.maxWeight;
- }
- public final float getStamina()
- {
- return this.stamina;
- }
- public final int getPickup()
- {
- return this.pickup;
- }
- public final int getModifer(int number)
- {
- switch(number)
- {
- case 1: return this.radModifer;
- case 2: return this.bioModifer;
- case 3: return this.electroModifer;
- case 4: return this.fireModifer;
- case 5: return this.psyModifer;
- case 6: return this.hitModifer;
- case 7: return this.armorModifer;
- case 8: return this.staminaModifer;
- case 9: return this.weightModifer;
- default: System.out.println("[WARNING] Unknown modifer: " + number + ". Get Rad Modifer"); return this.radModifer;
- }
- }
- public final Modifer getModiferFromList(int index)
- {
- if(this.modiferList.size() < index)
- {
- return (Modifer) this.modiferList.get(0);
- }
- else
- {
- return (Modifer) this.modiferList.get(index);
- }
- }
- public final TimeModifer getTimeModiferFromList(int index)
- {
- if(this.modiferList.size() < index)
- {
- return (TimeModifer) this.modiferList.get(0);
- }
- else
- {
- return (TimeModifer) this.modiferList.get(index);
- }
- }
- public final ArrayList addModiferToList(String name, int type, int amount)
- {
- this.modiferList.add(this.modiferList.size(), new Modifer(name, type, amount));
- String[] types = {"rad", "bio", "electro", "fire", "psy", "hit", "armor", "stamina", "weight"};
- return modiferList;
- }
- public final ArrayList addTimeModiferToList(String name, int type, int amount, int time)
- {
- this.modiferList.add(this.modiferList.size(), new TimeModifer(name, type, amount, time));
- String[] types = {"rad", "bio", "electro", "fire", "psy", "hit", "armor", "stamina", "weight", "bleeding", "regeneration"};
- return modiferList;
- }
- public final ArrayList getModiferList()
- {
- return modiferList;
- }
- public final ArrayList removeModiferFromList(int index)
- {
- this.modiferList.remove(index);
- return modiferList;
- }
- public final int getDeposit()
- {
- return this.deposit;
- }
- public final boolean getExitTrade()
- {
- return this.exitTrade;
- }
- public final int getETM()
- {
- return this.energyTimeModifer;
- }
- /*
- public final int getClanLevel()
- {
- return this.clan_level;
- }
- public final int getClanPoints()
- {
- return this.clan_points;
- }
- */
- public final void setRad(float amount)
- {
- this.rad = amount;
- sync();
- }
- public final void setWeight(float amount)
- {
- this.weight = amount;
- sync();
- }
- public final void setbWeight(float amount)
- {
- this.bWeight = amount;
- sync();
- }
- public final void setMaxWeight(float amount)
- {
- this.maxWeight = amount;
- sync();
- }
- public final void setStamina(float amount)
- {
- this.stamina = amount;
- sync();
- }
- public final void setPickup(int amount)
- {
- this.pickup = amount;
- sync();
- }
- public final void setModifer(int number, int amount)
- {
- switch(number)
- {
- case 1: this.radModifer = amount; sync(); break;
- case 2: this.bioModifer = amount; sync(); break;
- case 3: this.electroModifer = amount; sync(); break;
- case 4: this.fireModifer = amount; sync(); break;
- case 5: this.psyModifer = amount; sync(); break;
- case 6: this.hitModifer = amount; sync(); break;
- case 7: this.armorModifer = amount; sync(); break;
- case 8: this.staminaModifer = amount; sync(); break;
- case 9: this.weightModifer = amount; sync(); break;
- default: System.out.println("[WARNING] Unknown modifer: " + number + ". Set Rad Modifer"); this.radModifer = amount; sync();
- }
- }
- public final void setDeposit(int amount)
- {
- this.deposit = amount;
- sync();
- }
- public final void setExitTrade(boolean amount)
- {
- this.exitTrade = amount;
- sync();
- }
- public final void setETM(int amount)
- {
- this.energyTimeModifer = amount;
- sync();
- }
- /**
- * Makes it look nicer in the methods save/loadProxyData
- */
- private static String getSaveKey(EntityPlayer player) {
- return player.username + ":" + EXT_PROP_NAME;
- }
- /**
- * Does everything I did in onLivingDeathEvent and it's static,
- * so you now only need to use the following in the above event:
- * ExtendedPlayer.saveProxyData((EntityPlayer) event.entity));
- */
- public static void saveProxyData(EntityPlayer player) {
- ExtendedPlayer playerData = ExtendedPlayer.get(player);
- NBTTagCompound savedData = new NBTTagCompound();
- playerData.saveNBTData(savedData);
- // Note that we made the CommonProxy method storeEntityData static,
- // so now we don't need an instance of CommonProxy to use it! Great!
- CommonProxy.storeEntityData(getSaveKey(player), savedData);
- System.out.println("Saving Player Data");
- }
- /**
- * This cleans up the onEntityJoinWorld event by replacing most of the code
- * with a single line: ExtendedPlayer.loadProxyData((EntityPlayer) event.entity));
- */
- public static void loadProxyData(EntityPlayer player) {
- ExtendedPlayer playerData = ExtendedPlayer.get(player);
- NBTTagCompound savedData = CommonProxy.getEntityData(getSaveKey(player));
- if(savedData != null) {
- playerData.loadNBTData(savedData);
- System.out.println("Loading Player Data");
- }
- // note we renamed 'syncExtendedProperties' to 'syncProperties' because yay, it's shorter
- playerData.sync();
- }
- public final void sync()
- {
- ByteArrayOutputStream bos = new ByteArrayOutputStream(8);
- DataOutputStream outputStream = new DataOutputStream(bos);
- // We'll write max mana first so when we set current mana client
- // side, it doesn't get set to 0 (see methods below)
- try {
- outputStream.writeFloat(this.rad);
- outputStream.writeFloat(this.weight);
- outputStream.writeFloat(this.bWeight);
- outputStream.writeFloat(this.maxWeight);
- outputStream.writeFloat(this.stamina);
- outputStream.writeInt(this.pickup);
- outputStream.writeInt(this.radModifer);
- outputStream.writeInt(this.bioModifer);
- outputStream.writeInt(this.electroModifer);
- outputStream.writeInt(this.fireModifer);
- outputStream.writeInt(this.psyModifer);
- outputStream.writeInt(this.hitModifer);
- outputStream.writeInt(this.armorModifer);
- outputStream.writeInt(this.staminaModifer);
- outputStream.writeInt(this.weightModifer);
- outputStream.writeInt(this.deposit);
- outputStream.writeBoolean(this.exitTrade);
- outputStream.writeInt(this.energyTimeModifer);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- Packet250CustomPayload packet = new Packet250CustomPayload("ep", bos.toByteArray());
- // We only want to send from the server to the client
- if (!player.worldObj.isRemote) {
- EntityPlayerMP player1 = (EntityPlayerMP) player;
- PacketDispatcher.sendPacketToPlayer(packet, (Player) player1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment