Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // simplemoney.capability.Money
- package xaser.simplemoney.capability;
- import xaser.simplemoney.network.NetworkHandler;
- import xaser.simplemoney.network.UpdateMoneyMessage;
- import net.minecraftforge.common.capabilities.CapabilityManager;
- /**
- * Default implementation of Money capability.
- */
- public class Money implements IMoney {
- private int money = 0;
- public static void register() {
- CapabilityManager.INSTANCE.register(IMoney.class, new MoneyStorage(), Money.class);
- }
- private void onMoneyUpdate() {
- NetworkHandler.NETWORK.sendTo(new UpdateMoneyMessage(money), this.)
- }
- /**
- * Getter for the money field.
- * @return The amount of money.
- */
- @Override
- public int getMoney() {
- return money;
- }
- /**
- * Adds the specified amount to the money balance.
- * @param amount Positive or negative value to addMoney to the current amount of money.
- * @throws IllegalArgumentException
- */
- @Override
- public void addMoney(int amount) throws IllegalArgumentException {
- // TODO: make sure that Java 8 feature addExact causes no trouble.
- int new_value = Math.addExact(money, amount); // Use addExact to automatically throw exception on overflow.
- if (new_value < 0) {
- throw new IllegalArgumentException("Addition resulted in negative value. Cannot have negative balance.");
- }
- else {
- money = new_value;
- onMoneyUpdate();
- }
- }
- /**
- * Sets the money balance to the specified amount.
- * @param amount Positive value to be assigned to the money field.
- * @throws IllegalArgumentException
- */
- @Override
- public void setMoney(int amount) throws IllegalArgumentException {
- if(amount < 0) {
- throw new IllegalArgumentException("Amount is negative. Cannot have negative balance.");
- }
- else {
- money = amount;
- onMoneyUpdate();
- }
- }
- }
- // simplemoney.network.UpdateMoneyMessage
- package xaser.simplemoney.network;
- import io.netty.buffer.ByteBuf;
- import xaser.simplemoney.capability.IMoney;
- import xaser.simplemoney.capability.MoneyProvider;
- import net.minecraft.client.Minecraft;
- import net.minecraft.util.IThreadListener;
- import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
- import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
- import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
- /** Notification message that informs the player about an update of his money value.
- */
- public class UpdateMoneyMessage implements IMessage {
- private int updated_value = 0;
- // Default constructor REQUIRED!
- public UpdateMoneyMessage() {
- }
- public UpdateMoneyMessage(int updated_value) {
- this.updated_value = updated_value;
- }
- @Override
- public void toBytes(ByteBuf buf) {
- buf.writeInt(updated_value);
- }
- @Override
- public void fromBytes(ByteBuf buf) {
- this.updated_value = buf.readInt();
- }
- public static class Handler implements IMessageHandler<UpdateMoneyMessage, IMessage> {
- @Override
- public IMessage onMessage(final UpdateMoneyMessage message, MessageContext ctx) {
- // Schedule the update of the money value in the main thread.
- // Packet is Client bound, so we do not need to check sides!
- IThreadListener mainThread = Minecraft.getMinecraft();
- mainThread.addScheduledTask(new Runnable() {
- @Override
- public void run() {
- IMoney playerMoney = Minecraft.getMinecraft().thePlayer.getCapability(MoneyProvider.MONEY_CAPABILITY, null);
- playerMoney.setMoney(message.updated_value);
- }
- });
- return null; // Do not send a response, we have TCP guaranteed delivery anyway.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment