Search in sources :

Example 21 with NbtCompound

use of net.minecraft.nbt.NbtCompound in project FabricWaystones by LordDeatHunter.

the class WaystonePacketHandler method registerPacketHandlers.

public static void registerPacketHandlers() {
    ServerPlayNetworking.registerGlobalReceiver(REMOVE_WAYSTONE_OWNER, (server, player, networkHandler, data, sender) -> {
        NbtCompound tag = data.readNbt();
        server.execute(() -> {
            if (tag == null || !tag.contains("waystone_hash") || !tag.contains("waystone_owner")) {
                return;
            }
            String hash = tag.getString("waystone_hash");
            UUID owner = tag.getUuid("waystone_owner");
            if ((player.getUuid().equals(owner) || player.hasPermissionLevel(2))) {
                Waystones.WAYSTONE_STORAGE.setOwner(hash, null);
            }
        });
    });
    ServerPlayNetworking.registerGlobalReceiver(WAYSTONE_GUI_SLOT_CLICK, (server, player, networkHandler, data, sender) -> {
        NbtCompound tag = data.readNbt();
        server.execute(() -> {
            if (tag == null || !tag.contains("sync_id") || !tag.contains("clicked_slot")) {
                return;
            }
            int syncId = tag.getInt("sync_id");
            int button = tag.getInt("clicked_slot");
            if (player.currentScreenHandler.syncId == syncId) {
                player.currentScreenHandler.onButtonClick(player, button);
            }
        });
    });
    ServerPlayNetworking.registerGlobalReceiver(RENAME_WAYSTONE, (server, player, networkHandler, data, sender) -> {
        NbtCompound tag = data.readNbt();
        if (tag == null || !tag.contains("waystone_name") || !tag.contains("waystone_hash") || !tag.contains("waystone_owner")) {
            return;
        }
        String name = tag.getString("waystone_name");
        String hash = tag.getString("waystone_hash");
        UUID owner = tag.getUuid("waystone_owner");
        server.execute(() -> {
            if (Waystones.WAYSTONE_STORAGE.containsHash(hash) && ((player.getUuid().equals(owner) && owner.equals(Waystones.WAYSTONE_STORAGE.getWaystoneEntity(hash).getOwner())) || player.hasPermissionLevel(2))) {
                Waystones.WAYSTONE_STORAGE.renameWaystone(hash, name);
            }
        });
    });
    ServerPlayNetworking.registerGlobalReceiver(FORGET_WAYSTONE, (server, player, networkHandler, data, sender) -> {
        NbtCompound tag = data.readNbt();
        if (tag == null || !tag.contains("waystone_hash")) {
            return;
        }
        String hash = tag.getString("waystone_hash");
        server.execute(() -> {
            var waystone = Waystones.WAYSTONE_STORAGE.getWaystoneEntity(hash);
            if (waystone == null || waystone.isGlobal()) {
                return;
            }
            if (player.getUuid().equals(waystone.getOwner())) {
                waystone.setOwner(null);
            }
            ((PlayerEntityMixinAccess) player).forgetWaystone(hash);
        });
    });
    ServerPlayNetworking.registerGlobalReceiver(REQUEST_PLAYER_SYNC, (server, player, networkHandler, data, sender) -> server.execute(((PlayerEntityMixinAccess) player)::syncData));
    ServerPlayNetworking.registerGlobalReceiver(TOGGLE_GLOBAL_WAYSTONE, (server, player, networkHandler, data, sender) -> {
        NbtCompound tag = data.readNbt();
        if (tag == null || !tag.contains("waystone_hash") || !tag.contains("waystone_owner")) {
            return;
        }
        if (!Config.getInstance().canPlayersToggleGlobal() && !player.hasPermissionLevel(2)) {
            return;
        }
        server.execute(() -> {
            String hash = tag.getString("waystone_hash");
            UUID owner = tag.getUuid("waystone_owner");
            if (Waystones.WAYSTONE_STORAGE.containsHash(hash) && ((player.getUuid().equals(owner) && owner.equals(Waystones.WAYSTONE_STORAGE.getWaystoneEntity(hash).getOwner())) || player.hasPermissionLevel(2))) {
                Waystones.WAYSTONE_STORAGE.toggleGlobal(hash);
            }
        });
    });
    ServerPlayNetworking.registerGlobalReceiver(SYNC_PLAYER_FROM_CLIENT, (server, player, networkHandler, data, sender) -> {
        NbtCompound tag = data.readNbt();
        server.execute(() -> ((PlayerEntityMixinAccess) player).fromTagW(tag));
    });
    ServerPlayNetworking.registerGlobalReceiver(TELEPORT_TO_WAYSTONE, (server, player, networkHandler, data, sender) -> {
        NbtCompound tag = data.readNbt();
        if (tag == null || !tag.contains("waystone_hash")) {
            return;
        }
        server.execute(() -> {
            if (!tag.contains("waystone_hash")) {
                return;
            }
            String hash = tag.getString("waystone_hash");
            var waystone = Waystones.WAYSTONE_STORAGE.getWaystoneEntity(hash);
            if (waystone == null) {
                return;
            }
            if (waystone.getWorld() != null && !(waystone.getWorld().getBlockState(waystone.getPos()).getBlock() instanceof WaystoneBlock)) {
                Waystones.WAYSTONE_STORAGE.removeWaystone(hash);
                waystone.getWorld().removeBlockEntity(waystone.getPos());
            } else {
                waystone.teleportPlayer(player, true);
            }
        });
    });
}
Also used : PlayerEntityMixinAccess(wraith.waystones.access.PlayerEntityMixinAccess) NbtCompound(net.minecraft.nbt.NbtCompound) WaystoneBlock(wraith.waystones.block.WaystoneBlock) UUID(java.util.UUID)

Example 22 with NbtCompound

use of net.minecraft.nbt.NbtCompound in project FabricWaystones by LordDeatHunter.

the class WaystoneStorage method fromTag.

public void fromTag(NbtCompound tag) {
    if (tag == null || !tag.contains("waystones")) {
        return;
    }
    WAYSTONES.clear();
    var globals = new HashSet<String>();
    for (var element : tag.getList("global_waystones", NbtElement.STRING_TYPE)) {
        globals.add(element.asString());
    }
    var waystones = tag.getList("waystones", NbtElement.COMPOUND_TYPE);
    for (int i = 0; i < waystones.size(); ++i) {
        NbtCompound waystoneTag = waystones.getCompound(i);
        if (!waystoneTag.contains("hash") || !waystoneTag.contains("name") || !waystoneTag.contains("dimension") || !waystoneTag.contains("position")) {
            continue;
        }
        String name = waystoneTag.getString("name");
        String hash = waystoneTag.getString("hash");
        String dimension = waystoneTag.getString("dimension");
        int[] coordinates = waystoneTag.getIntArray("position");
        BlockPos pos = new BlockPos(coordinates[0], coordinates[1], coordinates[2]);
        WAYSTONES.put(hash, new Lazy(name, pos, hash, dimension, globals.contains(hash)));
    }
}
Also used : NbtCompound(net.minecraft.nbt.NbtCompound) BlockPos(net.minecraft.util.math.BlockPos) NbtString(net.minecraft.nbt.NbtString)

Example 23 with NbtCompound

use of net.minecraft.nbt.NbtCompound in project FabricWaystones by LordDeatHunter.

the class WaystoneStorage method loadOrSaveWaystones.

public void loadOrSaveWaystones(boolean save) {
    if (server == null) {
        return;
    }
    ServerWorld world = server.getWorld(ServerWorld.OVERWORLD);
    if (save) {
        state.markDirty();
        sendToAllPlayers();
    } else {
        try {
            NbtCompound compoundTag = world.getPersistentStateManager().readNbt(ID, SharedConstants.getGameVersion().getWorldVersion());
            state.writeNbt(compoundTag.getCompound("data"));
        } catch (IOException ignored) {
        }
    }
    world.getPersistentStateManager().save();
}
Also used : ServerWorld(net.minecraft.server.world.ServerWorld) NbtCompound(net.minecraft.nbt.NbtCompound) IOException(java.io.IOException)

Example 24 with NbtCompound

use of net.minecraft.nbt.NbtCompound in project FabricWaystones by LordDeatHunter.

the class WaystoneStorage method toTag.

public NbtCompound toTag(NbtCompound tag) {
    if (tag == null) {
        tag = new NbtCompound();
    }
    NbtList waystones = new NbtList();
    for (Map.Entry<String, WaystoneValue> waystone : WAYSTONES.entrySet()) {
        String hash = waystone.getKey();
        WaystoneValue entity = waystone.getValue();
        NbtCompound waystoneTag = new NbtCompound();
        waystoneTag.putString("hash", hash);
        waystoneTag.putString("name", entity.getWaystoneName());
        BlockPos pos = entity.way_getPos();
        waystoneTag.putIntArray("position", Arrays.asList(pos.getX(), pos.getY(), pos.getZ()));
        waystoneTag.putString("dimension", entity.getWorldName());
        waystones.add(waystoneTag);
    }
    tag.put("waystones", waystones);
    NbtList globals = new NbtList();
    var globalWaystones = getGlobals();
    for (String globalWaystone : globalWaystones) {
        globals.add(NbtString.of(globalWaystone));
    }
    tag.put("global_waystones", globals);
    return tag;
}
Also used : NbtCompound(net.minecraft.nbt.NbtCompound) NbtList(net.minecraft.nbt.NbtList) WaystoneValue(wraith.waystones.access.WaystoneValue) BlockPos(net.minecraft.util.math.BlockPos) NbtString(net.minecraft.nbt.NbtString) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 25 with NbtCompound

use of net.minecraft.nbt.NbtCompound in project FabricWaystones by LordDeatHunter.

the class CompatibilityLayer method writeNbt.

public NbtCompound writeNbt(NbtCompound tag) {
    if (COMPATABILITY_ENABLED)
        return tag;
    if (tag == null)
        tag = new NbtCompound();
    NbtList compatWaystones = new NbtList();
    for (Map.Entry<String, HashSet<String>> player : COMPATABILITY_MAP.entrySet()) {
        NbtCompound playerTag = new NbtCompound();
        playerTag.putString("username", player.getKey());
        NbtList knownWaystones = new NbtList();
        for (String hash : player.getValue()) {
            NbtCompound waystone = new NbtCompound();
            waystone.putString("hash", hash);
            knownWaystones.add(waystone);
        }
        playerTag.put("waystones", knownWaystones);
        compatWaystones.add(playerTag);
    }
    tag.put("waystones_compat", compatWaystones);
    return tag;
}
Also used : NbtCompound(net.minecraft.nbt.NbtCompound) NbtList(net.minecraft.nbt.NbtList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

NbtCompound (net.minecraft.nbt.NbtCompound)318 NbtList (net.minecraft.nbt.NbtList)93 ItemStack (net.minecraft.item.ItemStack)69 NbtElement (net.minecraft.nbt.NbtElement)28 IOException (java.io.IOException)25 LiteralText (net.minecraft.text.LiteralText)24 Identifier (net.minecraft.util.Identifier)24 Inject (org.spongepowered.asm.mixin.injection.Inject)21 BlockPos (net.minecraft.util.math.BlockPos)20 NbtString (net.minecraft.nbt.NbtString)16 File (java.io.File)13 HashMap (java.util.HashMap)10 List (java.util.List)9 Text (net.minecraft.text.Text)9 Vec3d (net.minecraft.util.math.Vec3d)9 Map (java.util.Map)8 Items (net.minecraft.item.Items)8 TranslatableText (net.minecraft.text.TranslatableText)8 HashSet (java.util.HashSet)7 BlockEntity (net.minecraft.block.entity.BlockEntity)7