Add a custom currency API
This commit is contained in:
parent
c6ff1afbdc
commit
1842387ec8
23 changed files with 783 additions and 42 deletions
|
@ -12,19 +12,20 @@ import net.minecraftforge.network.NetworkRegistry;
|
|||
import net.minecraftforge.network.PacketDistributor;
|
||||
import net.minecraftforge.network.simple.SimpleChannel;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* Networking system!
|
||||
*/
|
||||
public class ModMessages {
|
||||
private static SimpleChannel INSTANCE;
|
||||
private static int PACKET_ID=0;
|
||||
/**
|
||||
* INTERNAL USE ONLY. DO NOT USE THIS, IF YOU ARE NOT LIBZONTRECK!!!!!!
|
||||
* @return
|
||||
* INTERNAL USE ONLY
|
||||
*/
|
||||
private static AtomicInteger PACKET_ID=new AtomicInteger(0);
|
||||
public static int id()
|
||||
{
|
||||
return PACKET_ID++;
|
||||
return PACKET_ID.getAndIncrement();
|
||||
}
|
||||
public static void register()
|
||||
{
|
||||
|
@ -44,12 +45,13 @@ public class ModMessages {
|
|||
packet.register(net);
|
||||
}
|
||||
|
||||
net.messageBuilder(ChestGUIOpenC2S.class, id(), NetworkDirection.PLAY_TO_SERVER)
|
||||
net.messageBuilder(ChestGUIOpenC2S.class, PACKET_ID.getAndIncrement(), NetworkDirection.PLAY_TO_SERVER)
|
||||
.decoder(ChestGUIOpenC2S::new)
|
||||
.encoder(ChestGUIOpenC2S::toBytes)
|
||||
.consumer(ChestGUIOpenC2S::handle)
|
||||
.consumerMainThread(ChestGUIOpenC2S::handle)
|
||||
.add();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package dev.zontreck.libzontreck.networking;
|
||||
|
||||
import dev.zontreck.libzontreck.events.RegisterPacketsEvent;
|
||||
import dev.zontreck.libzontreck.networking.packets.S2CPlaySoundPacket;
|
||||
import dev.zontreck.libzontreck.networking.packets.S2CWalletInitialSyncPacket;
|
||||
import dev.zontreck.libzontreck.networking.packets.S2CWalletUpdatedPacket;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
|
||||
public class NetworkEvents
|
||||
{
|
||||
@SubscribeEvent
|
||||
public void onRegisterPackets(RegisterPacketsEvent ev)
|
||||
{
|
||||
ev.packets.add(new S2CWalletUpdatedPacket());
|
||||
ev.packets.add(new S2CPlaySoundPacket());
|
||||
ev.packets.add(new S2CWalletInitialSyncPacket());
|
||||
}
|
||||
}
|
|
@ -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