Guest User

IExtendedEntityProperties

a guest
May 13th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.01 KB | None | 0 0
  1. package com.ssw.Guns.event;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.DataOutputStream;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. import net.minecraft.entity.Entity;
  9. import net.minecraft.entity.player.EntityPlayer;
  10. import net.minecraft.entity.player.EntityPlayerMP;
  11. import net.minecraft.nbt.NBTTagCompound;
  12. import net.minecraft.network.packet.Packet250CustomPayload;
  13. import net.minecraft.world.World;
  14. import net.minecraftforge.common.IExtendedEntityProperties;
  15.  
  16. import com.mrcreppo.artefact.src.inventory.InventoryCustomPlayer;
  17. import com.ssw.Guns.common.CommonProxy;
  18.  
  19. import cpw.mods.fml.common.network.PacketDispatcher;
  20. import cpw.mods.fml.common.network.Player;
  21.  
  22. public class ExtendedPlayer implements IExtendedEntityProperties
  23. {
  24.     /*
  25. 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
  26. 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
  27.  
  28. 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.
  29.      */
  30.     public final static String EXT_PROP_NAME = "ExtendedPlayerSSW";
  31.  
  32.     public final InventoryCustomPlayer inventory = new InventoryCustomPlayer();
  33.     /*
  34.     public static final int CLAN = 2;
  35.     public static final int CLAN_LEVEL = 3;
  36.     public static final int CLAN_POINTS = 4;
  37.     public static final String CLAN_NAME = "";
  38.     public static final int CLAN_LEADER = 11;
  39.      */
  40.     /*
  41.     public static final int HAIR_WATCHER = 13;
  42.     public static final int HAIR_COLOR_WATCHER = 11;
  43.     public static final int SKIN_COLOR_WATCHER = 12;
  44.     public static final int CHAR_END_WATCHER = 14;
  45.      */
  46.     // I always include the entity to which the properties belong for easy access
  47.     // It's final because we won't be changing which player it is
  48.     private final EntityPlayer player;
  49.  
  50.     // Declare other variables you want to add here
  51.  
  52.     // We're adding mana to the player, so we'll need current and max mana
  53.     private float rad;
  54.     private float weight;
  55.     private float bWeight;
  56.     private float maxWeight;
  57.     private float stamina;
  58.     private int pickup;
  59.  
  60.     private int radModifer;
  61.     private int bioModifer;
  62.     private int electroModifer;
  63.     private int fireModifer;
  64.     private int psyModifer;
  65.     private int hitModifer;
  66.     private int armorModifer;
  67.     private int staminaModifer;
  68.     private int weightModifer;
  69.  
  70.     private int deposit;
  71.    
  72.     private boolean exitTrade;
  73.    
  74.     private int energyTimeModifer;
  75.  
  76.  
  77.  
  78.     /*
  79. The default constructor takes no arguments, but I put in the Entity so I can initialize the above variable 'player'
  80.  
  81. Also, it's best to initialize any other variables you may have added, just like in any constructor.
  82.      */
  83.  
  84.  
  85.     private ArrayList modiferList = new ArrayList();
  86.     //private ArrayList timeModiferList = new ArrayList();
  87.  
  88.     public ExtendedPlayer(EntityPlayer player)
  89.     {
  90.         this.player = player;
  91.  
  92.         this.rad = 0;
  93.         this.weight = 1;
  94.         this.bWeight = 1;
  95.         this.maxWeight = 60;
  96.         this.stamina = 100;
  97.         this.pickup = 0;
  98.  
  99.         this.radModifer = 0;
  100.         this.bioModifer = 0;
  101.         this.electroModifer = 0;
  102.         this.fireModifer = 0;
  103.         this.psyModifer = 0;
  104.         this.hitModifer = 0;
  105.         this.armorModifer = 0;
  106.         this.staminaModifer = 0;
  107.         this.weightModifer = 0;
  108.         this.deposit = 3000;
  109.         this.exitTrade = false;
  110.         this.energyTimeModifer = 0;
  111.        
  112.         //this.player.getDataWatcher().addObject(DEPOSIT_WATCHER, this.deposit);
  113.     }
  114.  
  115.     /**
  116.      * Used to register these extended properties for the player during EntityConstructing event
  117.      * This method is for convenience only; it will make your code look nicer
  118.      */
  119.     public static final void register(EntityPlayer player)
  120.     {
  121.         player.registerExtendedProperties(ExtendedPlayer.EXT_PROP_NAME, new ExtendedPlayer(player));
  122.     }
  123.  
  124.     /**
  125.      * Returns ExtendedPlayer properties for player
  126.      * This method is for convenience only; it will make your code look nicer
  127.      */
  128.     public static final ExtendedPlayer get(EntityPlayer player)
  129.     {
  130.         return (ExtendedPlayer) player.getExtendedProperties(EXT_PROP_NAME);
  131.     }
  132.  
  133.     // Save any custom data that needs saving here
  134.     @Override
  135.     public void saveNBTData(NBTTagCompound compound)
  136.     {
  137.         // We need to create a new tag compound that will save everything for our Extended Properties
  138.         NBTTagCompound properties = new NBTTagCompound();
  139.  
  140.         // We only have 2 variables currently; save them both to the new tag
  141.         this.inventory.writeToNBT(properties);
  142.         properties.setFloat("RAD", this.rad);
  143.         properties.setFloat("WEIGHT", this.weight);
  144.         properties.setFloat("BWEIGHT", this.bWeight);
  145.         properties.setFloat("MAXWEIGHT", this.maxWeight);
  146.         properties.setFloat("STAMINA", this.stamina);
  147.         properties.setInteger("PICKUP", this.pickup);
  148.  
  149.         properties.setInteger("RM", this.radModifer);
  150.         properties.setInteger("BM", this.bioModifer);
  151.         properties.setInteger("EM", this.electroModifer);
  152.         properties.setInteger("FM", this.fireModifer);
  153.         properties.setInteger("PM", this.psyModifer);
  154.         properties.setInteger("HM", this.hitModifer);
  155.         properties.setInteger("AM", this.armorModifer);
  156.         properties.setInteger("SM", this.staminaModifer);
  157.         properties.setInteger("WM", this.weightModifer);
  158.         properties.setInteger("DEPOSIT", this.deposit);
  159.         properties.setBoolean("EXIT", this.exitTrade);
  160.         properties.setInteger("ETM", this.energyTimeModifer);
  161.  
  162.         /*
  163.         properties.setInteger("Clan_Level", this.clan_level);
  164.         properties.setInteger("Clan_Points", this.clan_points);
  165.         properties.setInteger("Clan_Leader", this.clan_leader);
  166.         properties.setInteger("Clan_Invited", this.clan_invited);
  167.         /*
  168.         properties.setInteger("Head", this.player.getDataWatcher().getWatchableObjectInt(HEAD_TYPE_WATCHER));
  169.         properties.setInteger("Hair", this.player.getDataWatcher().getWatchableObjectInt(HAIR_WATCHER));
  170.         properties.setInteger("HairColor", this.player.getDataWatcher().getWatchableObjectInt(HAIR_COLOR_WATCHER));
  171.         properties.setInteger("SkinColor", this.player.getDataWatcher().getWatchableObjectInt(SKIN_COLOR_WATCHER));
  172.         properties.setInteger("CharCreateEnd", this.player.getDataWatcher().getWatchableObjectInt(CHAR_END_WATCHER));
  173.          */
  174.  
  175.         /*
  176. 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.
  177.          */
  178.         compound.setTag(EXT_PROP_NAME, properties);
  179.     }
  180.  
  181.     // Load whatever data you saved
  182.     @Override
  183.     public void loadNBTData(NBTTagCompound compound)
  184.     {
  185.         // Here we fetch the unique tag compound we set for this class of Extended Properties
  186.         NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME);
  187.         // Get our data from the custom tag compound
  188.         /*
  189.         this.player.getDataWatcher().updateObject(SEX_WATCHER, properties.getInteger("Sex"));
  190.         this.player.getDataWatcher().updateObject(RACE_WATCHER, properties.getInteger("Race"));
  191.         this.player.getDataWatcher().updateObject(HEAD_TYPE_WATCHER, properties.getInteger("Head"));
  192.         this.player.getDataWatcher().updateObject(HAIR_WATCHER, properties.getInteger("Hair"));
  193.         this.player.getDataWatcher().updateObject(HAIR_COLOR_WATCHER, properties.getInteger("HairColor"));
  194.         this.player.getDataWatcher().updateObject(SKIN_COLOR_WATCHER, properties.getInteger("SkinColor"));
  195.         this.player.getDataWatcher().updateObject(CHAR_END_WATCHER, properties.getInteger("CharCreateEnd"));
  196.          */
  197.         this.inventory.readFromNBT(properties);
  198.         this.rad = properties.getFloat("RAD");
  199.         this.weight = properties.getFloat("WEIGHT");
  200.         this.bWeight = properties.getFloat("BWEIGHT");
  201.         this.maxWeight = properties.getFloat("MAXWEIGHT");
  202.         this.pickup = properties.getInteger("PICKUP");
  203.  
  204.         this.radModifer = properties.getInteger("RM");
  205.         this.bioModifer = properties.getInteger("BM");
  206.         this.electroModifer = properties.getInteger("EM");
  207.         this.fireModifer = properties.getInteger("FM");
  208.         this.psyModifer = properties.getInteger("PM");
  209.         this.hitModifer = properties.getInteger("HM");
  210.         this.armorModifer = properties.getInteger("AM");
  211.         this.staminaModifer = properties.getInteger("SM");
  212.         this.weightModifer = properties.getInteger("WM");
  213.         this.deposit = properties.getInteger("DEPOSIT");
  214.         this.exitTrade = properties.getBoolean("EXIT");
  215.         this.energyTimeModifer = properties.getInteger("ETM");
  216.         /*
  217.         this.clan_level = properties.getInteger("Clan_Level");
  218.         this.clan_points = properties.getInteger("Clan_Points");
  219.          */
  220.  
  221.         System.out.println("Loading Player Data");
  222.  
  223.         // Just so you know it's working, add this line:
  224.         //System.out.println("[TUT PROPS] Beard from NBT: " + this.beard);
  225.     }
  226.  
  227.     /*
  228. I personally have yet to find a use for this method. If you know of any,
  229. please let me know and I'll add it in!
  230.      */
  231.     @Override
  232.     public void init(Entity entity, World world)
  233.     {
  234.     }
  235.  
  236.     public final float getRad()
  237.     {
  238.         return this.rad;
  239.     }
  240.     public final float getWeight()
  241.     {
  242.         return this.weight;
  243.     }
  244.     public final float getbWeight()
  245.     {
  246.         return this.bWeight;
  247.     }
  248.     public final float getMaxWeight()
  249.     {
  250.         return this.maxWeight;
  251.     }
  252.     public final float getStamina()
  253.     {
  254.         return this.stamina;
  255.     }
  256.     public final int getPickup()
  257.     {
  258.         return this.pickup;
  259.     }
  260.  
  261.     public final int getModifer(int number)
  262.     {
  263.         switch(number)
  264.         {
  265.         case 1: return this.radModifer;
  266.         case 2: return this.bioModifer;
  267.         case 3: return this.electroModifer;
  268.         case 4: return this.fireModifer;
  269.         case 5: return this.psyModifer;
  270.         case 6: return this.hitModifer;
  271.         case 7: return this.armorModifer;
  272.         case 8: return this.staminaModifer;
  273.         case 9: return this.weightModifer;
  274.         default: System.out.println("[WARNING] Unknown modifer: " + number + ". Get Rad Modifer"); return this.radModifer;
  275.         }
  276.     }
  277.  
  278.     public final Modifer getModiferFromList(int index)
  279.     {
  280.         if(this.modiferList.size() < index)
  281.         {
  282.             return (Modifer) this.modiferList.get(0);
  283.         }
  284.         else
  285.         {
  286.             return (Modifer) this.modiferList.get(index);
  287.         }
  288.     }
  289.    
  290.     public final TimeModifer getTimeModiferFromList(int index)
  291.     {
  292.         if(this.modiferList.size() < index)
  293.         {
  294.             return (TimeModifer) this.modiferList.get(0);
  295.         }
  296.         else
  297.         {
  298.             return (TimeModifer) this.modiferList.get(index);
  299.         }
  300.     }
  301.  
  302.     public final ArrayList addModiferToList(String name, int type, int amount)
  303.     {
  304.         this.modiferList.add(this.modiferList.size(), new Modifer(name, type, amount));
  305.         String[] types = {"rad", "bio", "electro", "fire", "psy", "hit", "armor", "stamina", "weight"};
  306.         return modiferList;
  307.     }
  308.    
  309.     public final ArrayList addTimeModiferToList(String name, int type, int amount, int time)
  310.     {
  311.         this.modiferList.add(this.modiferList.size(), new TimeModifer(name, type, amount, time));
  312.         String[] types = {"rad", "bio", "electro", "fire", "psy", "hit", "armor", "stamina", "weight", "bleeding", "regeneration"};
  313.         return modiferList;
  314.     }
  315.  
  316.     public final ArrayList getModiferList()
  317.     {
  318.         return modiferList;
  319.     }
  320.  
  321.     public final ArrayList removeModiferFromList(int index)
  322.     {
  323.         this.modiferList.remove(index);
  324.         return modiferList;
  325.     }
  326.  
  327.     public final int getDeposit()
  328.     {
  329.         return this.deposit;
  330.     }
  331.    
  332.     public final boolean getExitTrade()
  333.     {
  334.         return this.exitTrade;
  335.     }
  336.    
  337.     public final int getETM()
  338.     {
  339.         return this.energyTimeModifer;
  340.     }
  341.    
  342.     /*
  343.     public final int getClanLevel()
  344.     {
  345.         return this.clan_level;
  346.     }
  347.     public final int getClanPoints()
  348.     {
  349.         return this.clan_points;
  350.     }
  351.      */
  352.  
  353.     public final void setRad(float amount)
  354.     {
  355.         this.rad = amount;
  356.         sync();
  357.     }
  358.     public final void setWeight(float amount)
  359.     {
  360.         this.weight = amount;
  361.         sync();
  362.     }
  363.     public final void setbWeight(float amount)
  364.     {
  365.         this.bWeight = amount;
  366.         sync();
  367.     }
  368.     public final void setMaxWeight(float amount)
  369.     {
  370.         this.maxWeight = amount;
  371.         sync();
  372.     }
  373.     public final void setStamina(float amount)
  374.     {
  375.         this.stamina = amount;
  376.         sync();
  377.     }
  378.     public final void setPickup(int amount)
  379.     {
  380.         this.pickup = amount;
  381.         sync();
  382.     }
  383.  
  384.     public final void setModifer(int number, int amount)
  385.     {
  386.         switch(number)
  387.         {
  388.         case 1: this.radModifer = amount; sync(); break;
  389.         case 2: this.bioModifer = amount; sync(); break;
  390.         case 3: this.electroModifer = amount; sync(); break;
  391.         case 4: this.fireModifer = amount; sync(); break;
  392.         case 5: this.psyModifer = amount; sync(); break;
  393.         case 6: this.hitModifer = amount; sync(); break;
  394.         case 7: this.armorModifer = amount; sync(); break;
  395.         case 8: this.staminaModifer = amount;  sync(); break;
  396.         case 9: this.weightModifer = amount; sync(); break;
  397.         default: System.out.println("[WARNING] Unknown modifer: " + number + ". Set Rad Modifer"); this.radModifer = amount; sync();
  398.         }
  399.  
  400.     }
  401.  
  402.     public final void setDeposit(int amount)
  403.     {
  404.         this.deposit = amount;
  405.         sync();
  406.     }
  407.    
  408.     public final void setExitTrade(boolean amount)
  409.     {
  410.         this.exitTrade = amount;
  411.         sync();
  412.     }
  413.    
  414.     public final void setETM(int amount)
  415.     {
  416.         this.energyTimeModifer = amount;
  417.         sync();
  418.     }
  419.  
  420.     /**
  421.      * Makes it look nicer in the methods save/loadProxyData
  422.      */
  423.     private static String getSaveKey(EntityPlayer player) {
  424.         return player.username + ":" + EXT_PROP_NAME;
  425.     }
  426.  
  427.     /**
  428.      * Does everything I did in onLivingDeathEvent and it's static,
  429.      * so you now only need to use the following in the above event:
  430.      * ExtendedPlayer.saveProxyData((EntityPlayer) event.entity));
  431.      */
  432.     public static void saveProxyData(EntityPlayer player) {
  433.         ExtendedPlayer playerData = ExtendedPlayer.get(player);
  434.         NBTTagCompound savedData = new NBTTagCompound();
  435.  
  436.         playerData.saveNBTData(savedData);
  437.         // Note that we made the CommonProxy method storeEntityData static,
  438.         // so now we don't need an instance of CommonProxy to use it! Great!
  439.         CommonProxy.storeEntityData(getSaveKey(player), savedData);
  440.         System.out.println("Saving Player Data");
  441.     }
  442.  
  443.     /**
  444.      * This cleans up the onEntityJoinWorld event by replacing most of the code
  445.      * with a single line: ExtendedPlayer.loadProxyData((EntityPlayer) event.entity));
  446.      */
  447.     public static void loadProxyData(EntityPlayer player) {
  448.         ExtendedPlayer playerData = ExtendedPlayer.get(player);
  449.         NBTTagCompound savedData = CommonProxy.getEntityData(getSaveKey(player));
  450.  
  451.         if(savedData != null) {
  452.             playerData.loadNBTData(savedData);
  453.             System.out.println("Loading Player Data");
  454.         }
  455.         // note we renamed 'syncExtendedProperties' to 'syncProperties' because yay, it's shorter
  456.         playerData.sync();
  457.     }
  458.    
  459.     public final void sync()
  460.     {
  461.         ByteArrayOutputStream bos = new ByteArrayOutputStream(8);
  462.         DataOutputStream outputStream = new DataOutputStream(bos);
  463.  
  464.         // We'll write max mana first so when we set current mana client
  465.         // side, it doesn't get set to 0 (see methods below)
  466.         try {
  467.             outputStream.writeFloat(this.rad);
  468.             outputStream.writeFloat(this.weight);
  469.             outputStream.writeFloat(this.bWeight);
  470.             outputStream.writeFloat(this.maxWeight);
  471.             outputStream.writeFloat(this.stamina);
  472.             outputStream.writeInt(this.pickup);
  473.  
  474.             outputStream.writeInt(this.radModifer);
  475.             outputStream.writeInt(this.bioModifer);
  476.             outputStream.writeInt(this.electroModifer);
  477.             outputStream.writeInt(this.fireModifer);
  478.             outputStream.writeInt(this.psyModifer);
  479.             outputStream.writeInt(this.hitModifer);
  480.             outputStream.writeInt(this.armorModifer);
  481.             outputStream.writeInt(this.staminaModifer);
  482.             outputStream.writeInt(this.weightModifer);
  483.             outputStream.writeInt(this.deposit);
  484.             outputStream.writeBoolean(this.exitTrade);
  485.             outputStream.writeInt(this.energyTimeModifer);
  486.         } catch (Exception ex) {
  487.             ex.printStackTrace();
  488.         }
  489.  
  490.         Packet250CustomPayload packet = new Packet250CustomPayload("ep", bos.toByteArray());
  491.  
  492.         // We only want to send from the server to the client
  493.         if (!player.worldObj.isRemote) {
  494.             EntityPlayerMP player1 = (EntityPlayerMP) player;
  495.             PacketDispatcher.sendPacketToPlayer(packet, (Player) player1);
  496.         }
  497.     }
  498.  
  499.  
  500.  
  501.  
  502. }
Advertisement
Add Comment
Please, Sign In to add comment