Guest User

Untitled

a guest
Feb 18th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. // simplemoney.capability.Money
  2. package xaser.simplemoney.capability;
  3.  
  4. import xaser.simplemoney.network.NetworkHandler;
  5. import xaser.simplemoney.network.UpdateMoneyMessage;
  6. import net.minecraftforge.common.capabilities.CapabilityManager;
  7.  
  8. /**
  9.  * Default implementation of Money capability.
  10.  */
  11. public class Money implements IMoney {
  12.  
  13.     private int money = 0;
  14.  
  15.     public static void register() {
  16.         CapabilityManager.INSTANCE.register(IMoney.class, new MoneyStorage(), Money.class);
  17.     }
  18.  
  19.     private void onMoneyUpdate() {
  20.         NetworkHandler.NETWORK.sendTo(new UpdateMoneyMessage(money), this.)
  21.     }
  22.  
  23.     /**
  24.      * Getter for the money field.
  25.      * @return  The amount of money.
  26.      */
  27.     @Override
  28.     public int getMoney() {
  29.         return money;
  30.     }
  31.  
  32.     /**
  33.      * Adds the specified amount to the money balance.
  34.      * @param amount    Positive or negative value to addMoney to the current amount of money.
  35.      * @throws IllegalArgumentException
  36.      */
  37.     @Override
  38.     public void addMoney(int amount) throws IllegalArgumentException {
  39.         // TODO: make sure that Java 8 feature addExact causes no trouble.
  40.         int new_value = Math.addExact(money, amount);   // Use addExact to automatically throw exception on overflow.
  41.  
  42.         if (new_value < 0) {
  43.             throw new IllegalArgumentException("Addition resulted in negative value. Cannot have negative balance.");
  44.         }
  45.         else {
  46.             money = new_value;
  47.             onMoneyUpdate();
  48.         }
  49.     }
  50.  
  51.     /**
  52.      * Sets the money balance to the specified amount.
  53.      * @param amount Positive value to be assigned to the money field.
  54.      * @throws IllegalArgumentException
  55.      */
  56.     @Override
  57.     public void setMoney(int amount) throws IllegalArgumentException {
  58.         if(amount < 0) {
  59.             throw new IllegalArgumentException("Amount is negative. Cannot have negative balance.");
  60.         }
  61.         else {
  62.             money = amount;
  63.             onMoneyUpdate();
  64.         }
  65.     }
  66. }
  67.  
  68.  
  69. // simplemoney.network.UpdateMoneyMessage
  70. package xaser.simplemoney.network;
  71.  
  72. import io.netty.buffer.ByteBuf;
  73. import xaser.simplemoney.capability.IMoney;
  74. import xaser.simplemoney.capability.MoneyProvider;
  75. import net.minecraft.client.Minecraft;
  76. import net.minecraft.util.IThreadListener;
  77. import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
  78. import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
  79. import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
  80.  
  81. /** Notification message that informs the player about an update of his money value.
  82.  */
  83. public class UpdateMoneyMessage implements IMessage {
  84.     private int updated_value = 0;
  85.  
  86.     // Default constructor REQUIRED!
  87.     public UpdateMoneyMessage() {
  88.     }
  89.  
  90.     public UpdateMoneyMessage(int updated_value) {
  91.         this.updated_value = updated_value;
  92.     }
  93.  
  94.     @Override
  95.     public void toBytes(ByteBuf buf) {
  96.         buf.writeInt(updated_value);
  97.     }
  98.  
  99.     @Override
  100.     public void fromBytes(ByteBuf buf) {
  101.         this.updated_value = buf.readInt();
  102.     }
  103.  
  104.     public static class Handler implements IMessageHandler<UpdateMoneyMessage, IMessage> {
  105.  
  106.         @Override
  107.         public IMessage onMessage(final UpdateMoneyMessage message, MessageContext ctx) {
  108.  
  109.             // Schedule the update of the money value in the main thread.
  110.             // Packet is Client bound, so we do not need to check sides!
  111.             IThreadListener mainThread = Minecraft.getMinecraft();
  112.             mainThread.addScheduledTask(new Runnable() {
  113.                 @Override
  114.                 public void run() {
  115.                     IMoney playerMoney = Minecraft.getMinecraft().thePlayer.getCapability(MoneyProvider.MONEY_CAPABILITY, null);
  116.                     playerMoney.setMoney(message.updated_value);
  117.                 }
  118.             });
  119.  
  120.             return null;    // Do not send a response, we have TCP guaranteed delivery anyway.
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment