Search in sources :

Example 1 with PlayerEntityMixinAccess

use of wraith.waystones.access.PlayerEntityMixinAccess 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 2 with PlayerEntityMixinAccess

use of wraith.waystones.access.PlayerEntityMixinAccess in project FabricWaystones by LordDeatHunter.

the class WaystonesEventManager method registerEvents.

public static void registerEvents() {
    ServerLifecycleEvents.SERVER_STARTED.register((server) -> {
        if (Waystones.WAYSTONE_STORAGE == null) {
            Waystones.WAYSTONE_STORAGE = new WaystoneStorage(server);
        }
    });
    ServerLifecycleEvents.SERVER_STOPPED.register((server) -> {
        Waystones.WAYSTONE_STORAGE.loadOrSaveWaystones(true);
        Waystones.WAYSTONE_STORAGE = null;
    });
    ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
        PacketByteBuf data = PacketByteBufs.create();
        data.writeNbt(Config.getInstance().toNbtCompound());
        ServerPlayNetworking.send(handler.player, Utils.ID("waystone_config_update"), data);
        Waystones.WAYSTONE_STORAGE.sendToPlayer(handler.player);
        Waystones.WAYSTONE_STORAGE.sendCompatData(handler.player);
    });
    ServerLifecycleEvents.SERVER_STARTING.register(WaystonesWorldgen::registerVanillaVillageWorldgen);
    ServerPlayerEvents.AFTER_RESPAWN.register((oldPlayer, newPlayer, alive) -> ((PlayerEntityMixinAccess) newPlayer).syncData());
    CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(CommandManager.literal("waystones").then(CommandManager.literal("reload").requires(source -> source.hasPermissionLevel(1)).executes(context -> {
        Config.getInstance().loadConfig();
        PacketByteBuf data = PacketByteBufs.create();
        data.writeNbt(Config.getInstance().toNbtCompound());
        for (ServerPlayerEntity player : context.getSource().getServer().getPlayerManager().getPlayerList()) {
            ServerPlayNetworking.send(player, Utils.ID("waystone_config_update"), data);
        }
        ServerPlayerEntity player = context.getSource().getPlayer();
        if (player != null) {
            player.sendMessage(new LiteralText("§6[§eWaystones§6] §3has successfully reloaded!"), false);
        }
        return 1;
    })).then(CommandManager.literal("display").executes(context -> {
        ServerPlayerEntity player = context.getSource().getPlayer();
        if (player == null) {
            return 1;
        }
        Config.getInstance().print(player);
        return 1;
    }))));
}
Also used : LiteralText(net.minecraft.text.LiteralText) Waystones(wraith.waystones.Waystones) PlayerEntityMixinAccess(wraith.waystones.access.PlayerEntityMixinAccess) ServerPlayConnectionEvents(net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) ServerPlayerEvents(net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents) CommandRegistrationCallback(net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback) ServerPlayNetworking(net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking) PacketByteBuf(net.minecraft.network.PacketByteBuf) ServerLifecycleEvents(net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents) PacketByteBufs(net.fabricmc.fabric.api.networking.v1.PacketByteBufs) CommandManager(net.minecraft.server.command.CommandManager) PacketByteBuf(net.minecraft.network.PacketByteBuf) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) LiteralText(net.minecraft.text.LiteralText)

Example 3 with PlayerEntityMixinAccess

use of wraith.waystones.access.PlayerEntityMixinAccess in project FabricWaystones by LordDeatHunter.

the class WaystonesClient method registerPacketHandlers.

private void registerPacketHandlers() {
    ClientPlayNetworking.registerGlobalReceiver(Utils.ID("waystone_packet"), (client, networkHandler, data, sender) -> {
        var nbt = data.readNbt();
        client.execute(() -> {
            if (Waystones.WAYSTONE_STORAGE == null) {
                Waystones.WAYSTONE_STORAGE = new WaystoneStorage(null);
            }
            Waystones.WAYSTONE_STORAGE.fromTag(nbt);
            if (client.player == null) {
                return;
            }
            HashSet<String> toForget = new HashSet<>();
            for (String hash : ((PlayerEntityMixinAccess) client.player).getDiscoveredWaystones()) {
                if (!Waystones.WAYSTONE_STORAGE.containsHash(hash)) {
                    toForget.add(hash);
                }
            }
            ((PlayerEntityMixinAccess) client.player).forgetWaystones(toForget);
            if (client.player.currentScreenHandler instanceof UniversalWaystoneScreenHandler) {
                ((UniversalWaystoneScreenHandler) client.player.currentScreenHandler).updateWaystones(client.player);
            }
        });
    });
    ClientPlayNetworking.registerGlobalReceiver(Utils.ID("waystone_config_update"), (client, networkHandler, data, sender) -> {
        NbtCompound tag = data.readNbt();
        client.execute(() -> Config.getInstance().loadConfig(tag));
    });
    ClientPlayNetworking.registerGlobalReceiver(Utils.ID("sync_player"), (client, networkHandler, data, sender) -> {
        NbtCompound tag = data.readNbt();
        client.execute(() -> {
            if (client.player != null) {
                ((PlayerEntityMixinAccess) client.player).fromTagW(tag);
            }
        });
    });
}
Also used : PlayerEntityMixinAccess(wraith.waystones.access.PlayerEntityMixinAccess) WaystoneStorage(wraith.waystones.util.WaystoneStorage) UniversalWaystoneScreenHandler(wraith.waystones.screen.UniversalWaystoneScreenHandler) NbtCompound(net.minecraft.nbt.NbtCompound) HashSet(java.util.HashSet)

Example 4 with PlayerEntityMixinAccess

use of wraith.waystones.access.PlayerEntityMixinAccess in project FabricWaystones by LordDeatHunter.

the class WaystoneScrollItem method useOnBlock.

@Override
public ActionResult useOnBlock(ItemUsageContext context) {
    if (context.getWorld().getBlockState(context.getBlockPos()).getBlock() instanceof WaystoneBlock && context.getPlayer() != null) {
        HashSet<String> discovered = ((PlayerEntityMixinAccess) context.getPlayer()).getDiscoveredWaystones();
        ItemStack stack = context.getStack();
        if (discovered.isEmpty()) {
            return ActionResult.FAIL;
        }
        NbtCompound tag = new NbtCompound();
        NbtList list = new NbtList();
        for (String hash : discovered) {
            list.add(NbtString.of(hash));
        }
        tag.put("waystones", list);
        stack.setNbt(tag);
    }
    return super.useOnBlock(context);
}
Also used : PlayerEntityMixinAccess(wraith.waystones.access.PlayerEntityMixinAccess) NbtCompound(net.minecraft.nbt.NbtCompound) NbtList(net.minecraft.nbt.NbtList) WaystoneBlock(wraith.waystones.block.WaystoneBlock) NbtString(net.minecraft.nbt.NbtString) ItemStack(net.minecraft.item.ItemStack)

Example 5 with PlayerEntityMixinAccess

use of wraith.waystones.access.PlayerEntityMixinAccess in project FabricWaystones by LordDeatHunter.

the class ScrollOfInfiniteKnowledgeItem method use.

@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
    ItemStack stack = user.getStackInHand(hand);
    if (Waystones.WAYSTONE_STORAGE == null) {
        return TypedActionResult.fail(stack);
    }
    int learned = 0;
    HashSet<String> toLearn = new HashSet<>();
    for (String hash : Waystones.WAYSTONE_STORAGE.getAllHashes()) {
        if (!((PlayerEntityMixinAccess) user).hasDiscoveredWaystone(hash)) {
            var waystone = Waystones.WAYSTONE_STORAGE.getWaystoneEntity(hash);
            if (waystone != null && waystone.getOwner() == null) {
                waystone.setOwner(user);
            }
            toLearn.add(hash);
            ++learned;
        }
    }
    Text text;
    if (learned > 0) {
        if (learned > 1) {
            text = new TranslatableText("waystones.learned.infinite.multiple", new LiteralText(String.valueOf(learned)).styled(style -> style.withColor(TextColor.parse(new TranslatableText("waystones.learned.infinite.multiple.arg_color").getString()))));
        } else {
            text = new TranslatableText("waystones.learned.infinite.single");
        }
        ((PlayerEntityMixinAccess) user).discoverWaystones(toLearn);
        if (!user.isCreative() && Config.getInstance().consumeInfiniteScroll()) {
            stack.decrement(1);
        }
    } else {
        text = new TranslatableText("waystones.learned.infinite.none");
    }
    if (!world.isClient) {
        user.sendMessage(text, false);
    }
    if (stack.isEmpty()) {
        user.setStackInHand(hand, ItemStack.EMPTY);
    }
    stack = user.getStackInHand(hand);
    return TypedActionResult.success(stack, world.isClient());
}
Also used : TranslatableText(net.minecraft.text.TranslatableText) PlayerEntityMixinAccess(wraith.waystones.access.PlayerEntityMixinAccess) LiteralText(net.minecraft.text.LiteralText) TranslatableText(net.minecraft.text.TranslatableText) Text(net.minecraft.text.Text) ItemStack(net.minecraft.item.ItemStack) HashSet(java.util.HashSet) LiteralText(net.minecraft.text.LiteralText)

Aggregations

PlayerEntityMixinAccess (wraith.waystones.access.PlayerEntityMixinAccess)6 NbtCompound (net.minecraft.nbt.NbtCompound)4 HashSet (java.util.HashSet)3 ItemStack (net.minecraft.item.ItemStack)3 LiteralText (net.minecraft.text.LiteralText)3 NbtList (net.minecraft.nbt.NbtList)2 NbtString (net.minecraft.nbt.NbtString)2 Text (net.minecraft.text.Text)2 TranslatableText (net.minecraft.text.TranslatableText)2 WaystoneBlock (wraith.waystones.block.WaystoneBlock)2 UUID (java.util.UUID)1 CommandRegistrationCallback (net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback)1 ServerPlayerEvents (net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents)1 ServerLifecycleEvents (net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents)1 PacketByteBufs (net.fabricmc.fabric.api.networking.v1.PacketByteBufs)1 ServerPlayConnectionEvents (net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents)1 ServerPlayNetworking (net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking)1 PacketByteBuf (net.minecraft.network.PacketByteBuf)1 CommandManager (net.minecraft.server.command.CommandManager)1 ServerPlayerEntity (net.minecraft.server.network.ServerPlayerEntity)1