Add a custom currency API
This commit is contained in:
parent
c6ff1afbdc
commit
1842387ec8
23 changed files with 783 additions and 42 deletions
|
@ -6,6 +6,7 @@ import dev.zontreck.libzontreck.util.BinUtil;
|
|||
import dev.zontreck.libzontreck.util.ServerUtilities;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.NbtUtils;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.sounds.SoundEvent;
|
||||
|
@ -25,17 +26,15 @@ public class S2CPlaySoundPacket implements IPacket
|
|||
public S2CPlaySoundPacket(ResourceLocation loc) {
|
||||
sound=loc;
|
||||
}
|
||||
public S2CPlaySoundPacket(){}
|
||||
|
||||
@Override
|
||||
public void deserialize(CompoundTag data) {
|
||||
// Deserializes the play sound packet
|
||||
throw new UnsupportedOperationException("This operation is not supported in the play sound packet!");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(CompoundTag data) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'serialize'");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
package dev.zontreck.libzontreck.networking.packets;
|
||||
|
||||
import dev.zontreck.libzontreck.currency.Account;
|
||||
import dev.zontreck.libzontreck.currency.Bank;
|
||||
import dev.zontreck.libzontreck.currency.events.WalletSyncEvent;
|
||||
import dev.zontreck.libzontreck.util.ServerUtilities;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.eventbus.EventBus;
|
||||
import net.minecraftforge.network.NetworkDirection;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
import net.minecraftforge.network.simple.SimpleChannel;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class S2CWalletInitialSyncPacket implements IPacket
|
||||
{
|
||||
public Account act;
|
||||
public S2CWalletInitialSyncPacket(FriendlyByteBuf buf)
|
||||
{
|
||||
act = new Account(buf.readNbt());
|
||||
}
|
||||
public S2CWalletInitialSyncPacket(Account act)
|
||||
{
|
||||
this.act=act;
|
||||
}
|
||||
public S2CWalletInitialSyncPacket(UUID ID)
|
||||
{
|
||||
this.act= Bank.getAccount(ID);
|
||||
}
|
||||
public S2CWalletInitialSyncPacket(){}
|
||||
|
||||
@Override
|
||||
public void deserialize(CompoundTag data) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(CompoundTag data) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(FriendlyByteBuf buf) {
|
||||
buf.writeNbt(act.save());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handle(Supplier<NetworkEvent.Context> supplier) {
|
||||
return ServerUtilities.handlePacket(supplier, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
MinecraftForge.EVENT_BUS.post(new WalletSyncEvent(act));
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public NetworkDirection getDirection() {
|
||||
return NetworkDirection.PLAY_TO_CLIENT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(SimpleChannel chan) {
|
||||
ServerUtilities.registerPacket(chan, S2CWalletInitialSyncPacket.class, this, S2CWalletInitialSyncPacket::new);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
package dev.zontreck.libzontreck.networking.packets;
|
||||
|
||||
import dev.zontreck.ariaslib.events.EventBus;
|
||||
import dev.zontreck.libzontreck.currency.Transaction;
|
||||
import dev.zontreck.libzontreck.currency.events.WalletUpdatedEvent;
|
||||
import dev.zontreck.libzontreck.util.ServerUtilities;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraftforge.network.NetworkDirection;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
import net.minecraftforge.network.simple.SimpleChannel;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class S2CWalletUpdatedPacket implements IPacket
|
||||
{
|
||||
public UUID ID;
|
||||
public Transaction tx;
|
||||
public int balance;
|
||||
public int oldBal;
|
||||
|
||||
public S2CWalletUpdatedPacket(FriendlyByteBuf buf)
|
||||
{
|
||||
ID = buf.readUUID();
|
||||
tx = new Transaction(buf.readNbt());
|
||||
balance = buf.readInt();
|
||||
oldBal = buf.readInt();
|
||||
}
|
||||
|
||||
public S2CWalletUpdatedPacket(UUID ID, Transaction tx, int bal, int old)
|
||||
{
|
||||
this.ID= ID;
|
||||
this.tx=tx;
|
||||
this.balance=bal;
|
||||
oldBal=old;
|
||||
}
|
||||
|
||||
public S2CWalletUpdatedPacket(){}
|
||||
|
||||
|
||||
@Override
|
||||
public void deserialize(CompoundTag data) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(CompoundTag data) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(FriendlyByteBuf buf) {
|
||||
buf.writeUUID(ID);
|
||||
buf.writeNbt(tx.save());
|
||||
buf.writeInt(balance);
|
||||
buf.writeInt(oldBal);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handle(Supplier<NetworkEvent.Context> supplier) {
|
||||
return ServerUtilities.handlePacket(supplier, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
EventBus.BUS.post(new WalletUpdatedEvent(ID, oldBal, balance, tx));
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public NetworkDirection getDirection() {
|
||||
return NetworkDirection.PLAY_TO_CLIENT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(SimpleChannel chan) {
|
||||
ServerUtilities.registerPacket(chan, S2CWalletUpdatedPacket.class, this, S2CWalletUpdatedPacket::new);
|
||||
}
|
||||
}
|
Reference in a new issue