diff options
| author | shedaniel <daniel@shedaniel.me> | 2022-10-22 01:11:04 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2022-10-22 01:12:32 +0800 |
| commit | 0ef0f8b21df4b9a603aaa2ab4a35d395ef6437c1 (patch) | |
| tree | f6f38140f94fa98adcad5b880fdc2aa3bd4df844 /runtime-engine/networking/src/main | |
| parent | bb7920e447d599d23abfac3b67d8823cf24f8150 (diff) | |
| download | RoughlyEnoughItems-0ef0f8b21df4b9a603aaa2ab4a35d395ef6437c1.tar.gz RoughlyEnoughItems-0ef0f8b21df4b9a603aaa2ab4a35d395ef6437c1.tar.bz2 RoughlyEnoughItems-0ef0f8b21df4b9a603aaa2ab4a35d395ef6437c1.zip | |
Make it not crash
Diffstat (limited to 'runtime-engine/networking/src/main')
9 files changed, 610 insertions, 0 deletions
diff --git a/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/client/networking/modules/CheatItemStatusNetworkModule.java b/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/client/networking/modules/CheatItemStatusNetworkModule.java new file mode 100644 index 000000000..45c687cdf --- /dev/null +++ b/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/client/networking/modules/CheatItemStatusNetworkModule.java @@ -0,0 +1,78 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.client.networking.modules; + +import dev.architectury.networking.NetworkManager; +import dev.architectury.networking.transformers.SplitPacketTransformer; +import dev.architectury.utils.Env; +import dev.architectury.utils.EnvExecutor; +import io.netty.buffer.Unpooled; +import me.shedaniel.rei.api.common.networking.NetworkModule; +import me.shedaniel.rei.api.common.networking.NetworkModuleKey; +import me.shedaniel.rei.api.common.util.EntryStacks; +import net.minecraft.client.Minecraft; +import net.minecraft.client.resources.language.I18n; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.chat.TextComponent; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.item.ItemStack; + +import java.util.Collections; +import java.util.Map; + +public class CheatItemStatusNetworkModule implements NetworkModule<Map.Entry<ItemStack, String>> { + public static final ResourceLocation ID = new ResourceLocation("roughlyenoughitems", "ci_msg"); + + @Override + public NetworkModuleKey<Map.Entry<ItemStack, String>> getKey() { + return NetworkModule.CHEAT_STATUS_REPLY; + } + + @Override + public boolean canUse(Object target) { + return NetworkManager.canPlayerReceive((ServerPlayer) target, CheatItemStatusNetworkModule.ID); + } + + @Override + public void onInitialize() { + EnvExecutor.runInEnv(Env.CLIENT, () -> () -> { + NetworkManager.registerReceiver(NetworkManager.c2s(), ID, Collections.singletonList(new SplitPacketTransformer()), (buf, context) -> { + ItemStack stack = buf.readItem(); + String player = buf.readUtf(32767); + if (Minecraft.getInstance().player != null) { + Minecraft.getInstance().player.displayClientMessage(new TextComponent(I18n.get("text.rei.cheat_items").replaceAll("\\{item_name}", EntryStacks.of(stack.copy()).asFormattedText().getString()).replaceAll("\\{item_count}", stack.copy().getCount() + "").replaceAll("\\{player_name}", player)), false); + } + }); + }); + } + + @Override + public void send(Object target, Map.Entry<ItemStack, String> data) { + FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); + buf.writeItem(data.getKey().copy()); + buf.writeUtf(data.getValue(), 32767); + NetworkManager.sendToServer(CheatItemStatusNetworkModule.ID, buf); + } +} diff --git a/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/client/networking/modules/NotEnoughItemsNetworkModule.java b/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/client/networking/modules/NotEnoughItemsNetworkModule.java new file mode 100644 index 000000000..c135eb921 --- /dev/null +++ b/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/client/networking/modules/NotEnoughItemsNetworkModule.java @@ -0,0 +1,111 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.client.networking.modules; + +import com.google.common.collect.Lists; +import dev.architectury.networking.NetworkManager; +import dev.architectury.networking.transformers.SplitPacketTransformer; +import dev.architectury.utils.Env; +import dev.architectury.utils.EnvExecutor; +import io.netty.buffer.Unpooled; +import me.shedaniel.rei.api.common.networking.NetworkModule; +import me.shedaniel.rei.api.common.networking.NetworkModuleKey; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.client.gui.screens.inventory.CraftingScreen; +import net.minecraft.client.gui.screens.recipebook.GhostRecipe; +import net.minecraft.client.gui.screens.recipebook.RecipeBookComponent; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.inventory.CraftingMenu; +import net.minecraft.world.inventory.Slot; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.minecraft.world.item.crafting.Ingredient; + +import java.util.Collections; +import java.util.List; + +public class NotEnoughItemsNetworkModule implements NetworkModule<List<List<ItemStack>>> { + public static final ResourceLocation ID = new ResourceLocation("roughlyenoughitems", "og_not_enough"); + + @Override + public NetworkModuleKey<List<List<ItemStack>>> getKey() { + return NetworkModule.NOT_ENOUGH_ITEMS; + } + + @Override + public boolean canUse(Object target) { + return NetworkManager.canPlayerReceive((ServerPlayer) target, NotEnoughItemsNetworkModule.ID); + } + + @Override + public void onInitialize() { + EnvExecutor.runInEnv(Env.CLIENT, () -> () -> { + NetworkManager.registerReceiver(NetworkManager.c2s(), ID, Collections.singletonList(new SplitPacketTransformer()), (buf, context) -> { + Screen currentScreen = Minecraft.getInstance().screen; + if (currentScreen instanceof CraftingScreen craftingScreen) { + RecipeBookComponent recipeBookGui = craftingScreen.getRecipeBookComponent(); + GhostRecipe ghostSlots = recipeBookGui.ghostRecipe; + ghostSlots.clear(); + + List<List<ItemStack>> input = Lists.newArrayList(); + int mapSize = buf.readInt(); + for (int i = 0; i < mapSize; i++) { + List<ItemStack> list = Lists.newArrayList(); + int count = buf.readInt(); + for (int j = 0; j < count; j++) { + list.add(buf.readItem()); + } + input.add(list); + } + + ghostSlots.addIngredient(Ingredient.of(Items.STONE), 381203812, 12738291); + CraftingMenu container = craftingScreen.getMenu(); + for (int i = 0; i < input.size(); i++) { + List<ItemStack> stacks = input.get(i); + if (!stacks.isEmpty()) { + Slot slot = container.getSlot(i + container.getResultSlotIndex() + 1); + ghostSlots.addIngredient(Ingredient.of(stacks.toArray(new ItemStack[0])), slot.x, slot.y); + } + } + } + }); + }); + } + + @Override + public void send(Object target, List<List<ItemStack>> data) { + FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); + buf.writeInt(data.size()); + for (List<ItemStack> stacks : data) { + buf.writeInt(stacks.size()); + for (ItemStack stack : stacks) { + buf.writeItem(stack); + } + } + NetworkManager.sendToServer(NotEnoughItemsNetworkModule.ID, buf); + } +} diff --git a/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/common/networking/NetworkingHelperImpl.java b/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/common/networking/NetworkingHelperImpl.java new file mode 100644 index 000000000..bb3e7a6fa --- /dev/null +++ b/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/common/networking/NetworkingHelperImpl.java @@ -0,0 +1,89 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.common.networking; + +import me.shedaniel.rei.api.common.networking.NetworkModule; +import me.shedaniel.rei.api.common.networking.NetworkModuleKey; +import me.shedaniel.rei.api.common.networking.NetworkingHelper; +import me.shedaniel.rei.impl.common.Internals; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.Minecraft; +import net.minecraft.server.level.ServerPlayer; + +import java.util.HashMap; +import java.util.Map; + +public class NetworkingHelperImpl implements NetworkingHelper { + private static final Map<NetworkModuleKey<?>, NetworkModule<?>> MODULES = new HashMap<>(); + + static { + for (NetworkModule<?> module : Internals.resolveServices(NetworkModule.class)) { + MODULES.put(module.getKey(), module); + } + } + + @Override + public <T> boolean has(NetworkModuleKey<T> moduleKey) { + return MODULES.containsKey(moduleKey); + } + + @Override + public <T> boolean canUse(NetworkModuleKey<T> moduleKey) { + return has(moduleKey) && MODULES.get(moduleKey).canUse(null); + } + + @Override + public <T> boolean canPlayerUse(ServerPlayer player, NetworkModuleKey<T> moduleKey) { + return has(moduleKey) && MODULES.get(moduleKey).canUse(player); + } + + @Override + public <T> void send(Object target, NetworkModuleKey<T> moduleKey, T data) { + if (canUse(moduleKey)) { + ((NetworkModule<T>) MODULES.get(moduleKey)).send(target, data); + } + } + + @SuppressWarnings("Convert2Lambda") + @Override + @Environment(EnvType.CLIENT) + public Client client() { + return new Client() { + @Environment(EnvType.CLIENT) + @Override + public boolean hasOperatorPermission() { + try { + return Minecraft.getInstance().getConnection().getSuggestionsProvider().hasPermission(1); + } catch (NullPointerException e) { + return true; + } + } + }; + } + + @Override + public void startReload() { + } +} diff --git a/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/common/networking/modules/CheatItemGiveNetworkModule.java b/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/common/networking/modules/CheatItemGiveNetworkModule.java new file mode 100644 index 000000000..b503e7811 --- /dev/null +++ b/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/common/networking/modules/CheatItemGiveNetworkModule.java @@ -0,0 +1,78 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.common.networking.modules; + +import dev.architectury.networking.NetworkManager; +import dev.architectury.networking.transformers.SplitPacketTransformer; +import io.netty.buffer.Unpooled; +import me.shedaniel.rei.api.common.networking.NetworkModule; +import me.shedaniel.rei.api.common.networking.NetworkModuleKey; +import me.shedaniel.rei.api.common.networking.NetworkingHelper; +import net.minecraft.ChatFormatting; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.chat.TranslatableComponent; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.item.ItemStack; + +import java.util.AbstractMap; +import java.util.Collections; + +public class CheatItemGiveNetworkModule implements NetworkModule<ItemStack> { + public static final ResourceLocation ID = new ResourceLocation("roughlyenoughitems", "create_item"); + + @Override + public NetworkModuleKey<ItemStack> getKey() { + return NetworkModule.CHEAT_GIVE; + } + + @Override + public boolean canUse(Object target) { + return NetworkManager.canServerReceive(CheatItemGiveNetworkModule.ID); + } + + @Override + public void onInitialize() { + NetworkManager.registerReceiver(NetworkManager.c2s(), ID, Collections.singletonList(new SplitPacketTransformer()), (buf, context) -> { + ServerPlayer player = (ServerPlayer) context.getPlayer(); + if (player.getServer().getProfilePermissions(player.getGameProfile()) < player.getServer().getOperatorUserPermissionLevel()) { + player.displayClientMessage(new TranslatableComponent("text.rei.no_permission_cheat").withStyle(ChatFormatting.RED), false); + return; + } + ItemStack stack = buf.readItem(); + if (player.getInventory().add(stack.copy())) { + NetworkingHelper.getInstance().sendToPlayer(player, NetworkModule.CHEAT_STATUS_REPLY, new AbstractMap.SimpleEntry<>(stack.copy(), player.getScoreboardName())); + } else { + player.displayClientMessage(new TranslatableComponent("text.rei.failed_cheat_items"), false); + } + }); + } + + @Override + public void send(Object target, ItemStack data) { + FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); + buf.writeItem(data.copy()); + NetworkManager.sendToServer(CheatItemGiveNetworkModule.ID, buf); + } +} diff --git a/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/common/networking/modules/CheatItemGrabNetworkModule.java b/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/common/networking/modules/CheatItemGrabNetworkModule.java new file mode 100644 index 000000000..0c76f1e14 --- /dev/null +++ b/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/common/networking/modules/CheatItemGrabNetworkModule.java @@ -0,0 +1,86 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.common.networking.modules; + +import dev.architectury.networking.NetworkManager; +import dev.architectury.networking.transformers.SplitPacketTransformer; +import io.netty.buffer.Unpooled; +import me.shedaniel.rei.api.common.networking.NetworkModule; +import me.shedaniel.rei.api.common.networking.NetworkModuleKey; +import me.shedaniel.rei.api.common.networking.NetworkingHelper; +import net.minecraft.ChatFormatting; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.chat.TranslatableComponent; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.util.Mth; +import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.item.ItemStack; + +import java.util.AbstractMap; +import java.util.Collections; + +public class CheatItemGrabNetworkModule implements NetworkModule<ItemStack> { + public static final ResourceLocation ID = new ResourceLocation("roughlyenoughitems", "create_item_grab"); + + @Override + public NetworkModuleKey<ItemStack> getKey() { + return NetworkModule.CHEAT_GRAB; + } + + @Override + public boolean canUse(Object target) { + return NetworkManager.canServerReceive(CheatItemGrabNetworkModule.ID); + } + + @Override + public void onInitialize() { + NetworkManager.registerReceiver(NetworkManager.c2s(), ID, Collections.singletonList(new SplitPacketTransformer()), (buf, context) -> { + ServerPlayer player = (ServerPlayer) context.getPlayer(); + if (player.getServer().getProfilePermissions(player.getGameProfile()) < player.getServer().getOperatorUserPermissionLevel()) { + player.displayClientMessage(new TranslatableComponent("text.rei.no_permission_cheat").withStyle(ChatFormatting.RED), false); + return; + } + + AbstractContainerMenu menu = player.containerMenu; + ItemStack itemStack = buf.readItem(); + ItemStack stack = itemStack.copy(); + if (!menu.getCarried().isEmpty() && ItemStack.isSameIgnoreDurability(menu.getCarried(), stack) && ItemStack.tagMatches(menu.getCarried(), stack)) { + stack.setCount(Mth.clamp(stack.getCount() + menu.getCarried().getCount(), 1, stack.getMaxStackSize())); + } else if (!menu.getCarried().isEmpty()) { + return; + } + menu.setCarried(stack.copy()); + menu.broadcastChanges(); + NetworkingHelper.getInstance().sendToPlayer(player, NetworkModule.CHEAT_STATUS_REPLY, new AbstractMap.SimpleEntry<>(stack.copy(), player.getScoreboardName())); + }); + } + + @Override + public void send(Object target, ItemStack data) { + FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); + buf.writeItem(data.copy()); + NetworkManager.sendToServer(CheatItemGrabNetworkModule.ID, buf); + } +} diff --git a/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/common/networking/modules/CheatItemHotbarNetworkModule.java b/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/common/networking/modules/CheatItemHotbarNetworkModule.java new file mode 100644 index 000000000..01c406901 --- /dev/null +++ b/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/common/networking/modules/CheatItemHotbarNetworkModule.java @@ -0,0 +1,85 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.common.networking.modules; + +import dev.architectury.networking.NetworkManager; +import dev.architectury.networking.transformers.SplitPacketTransformer; +import io.netty.buffer.Unpooled; +import me.shedaniel.rei.api.common.networking.NetworkModule; +import me.shedaniel.rei.api.common.networking.NetworkModuleKey; +import me.shedaniel.rei.api.common.networking.NetworkingHelper; +import net.minecraft.ChatFormatting; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.chat.TranslatableComponent; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.item.ItemStack; + +import java.util.AbstractMap; +import java.util.Collections; +import java.util.Map; + +public class CheatItemHotbarNetworkModule implements NetworkModule<Map.Entry<ItemStack, Integer>> { + public static final ResourceLocation ID = new ResourceLocation("roughlyenoughitems", "create_item_hotbar"); + + @Override + public NetworkModuleKey<Map.Entry<ItemStack, Integer>> getKey() { + return NetworkModule.CHEAT_HOTBAR; + } + + @Override + public boolean canUse(Object target) { + return NetworkManager.canServerReceive(CheatItemHotbarNetworkModule.ID); + } + + @Override + public void onInitialize() { + NetworkManager.registerReceiver(NetworkManager.c2s(), ID, Collections.singletonList(new SplitPacketTransformer()), (buf, context) -> { + ServerPlayer player = (ServerPlayer) context.getPlayer(); + if (player.getServer().getProfilePermissions(player.getGameProfile()) < player.getServer().getOperatorUserPermissionLevel()) { + player.displayClientMessage(new TranslatableComponent("text.rei.no_permission_cheat").withStyle(ChatFormatting.RED), false); + return; + } + ItemStack stack = buf.readItem(); + int hotbarSlotId = buf.readVarInt(); + if (hotbarSlotId >= 0 && hotbarSlotId < 9) { + AbstractContainerMenu menu = player.containerMenu; + player.getInventory().items.set(hotbarSlotId, stack.copy()); + menu.broadcastChanges(); + NetworkingHelper.getInstance().sendToPlayer(player, NetworkModule.CHEAT_STATUS_REPLY, new AbstractMap.SimpleEntry<>(stack.copy(), player.getScoreboardName())); + } else { + player.displayClientMessage(new TranslatableComponent("text.rei.failed_cheat_items"), false); + } + }); + } + + @Override + public void send(Object target, Map.Entry<ItemStack, Integer> data) { + FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); + buf.writeItem(data.getKey().copy()); + buf.writeVarInt(data.getValue()); + NetworkManager.sendToServer(CheatItemHotbarNetworkModule.ID, buf); + } +} diff --git a/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/common/networking/modules/DeleteItemNetworkModule.java b/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/common/networking/modules/DeleteItemNetworkModule.java new file mode 100644 index 000000000..af5c40889 --- /dev/null +++ b/runtime-engine/networking/src/main/java/me/shedaniel/rei/impl/common/networking/modules/DeleteItemNetworkModule.java @@ -0,0 +1,76 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.common.networking.modules; + +import dev.architectury.networking.NetworkManager; +import dev.architectury.networking.transformers.SplitPacketTransformer; +import io.netty.buffer.Unpooled; +import me.shedaniel.rei.api.common.networking.NetworkModule; +import me.shedaniel.rei.api.common.networking.NetworkModuleKey; +import net.minecraft.ChatFormatting; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.chat.TranslatableComponent; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.util.Unit; +import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.item.ItemStack; + +import java.util.Collections; + +public class DeleteItemNetworkModule implements NetworkModule<Unit> { + public static final ResourceLocation ID = new ResourceLocation("roughlyenoughitems", "delete_item"); + + @Override + public NetworkModuleKey<Unit> getKey() { + return NetworkModule.DELETE_ITEM; + } + + @Override + public boolean canUse(Object target) { + return NetworkManager.canServerReceive(DeleteItemNetworkModule.ID); + } + + @Override + public void onInitialize() { + NetworkManager.registerReceiver(NetworkManager.c2s(), ID, Collections.singletonList(new SplitPacketTransformer()), (buf, context) -> { + ServerPlayer player = (ServerPlayer) context.getPlayer(); + if (player.getServer().getProfilePermissions(player.getGameProfile()) < player.getServer().getOperatorUserPermissionLevel()) { + player.displayClientMessage(new TranslatableComponent("text.rei.no_permission_cheat").withStyle(ChatFormatting.RED), false); + return; + } + AbstractContainerMenu menu = player.containerMenu; + if (!menu.getCarried().isEmpty()) { + menu.setCarried(ItemStack.EMPTY); + menu.broadcastChanges(); + } + }); + } + + @Override + public void send(Object target, Unit data) { + FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); + NetworkManager.sendToServer(DeleteItemNetworkModule.ID, buf); + } +} diff --git a/runtime-engine/networking/src/main/resources/META-INF/services/me.shedaniel.rei.api.common.networking.NetworkModule b/runtime-engine/networking/src/main/resources/META-INF/services/me.shedaniel.rei.api.common.networking.NetworkModule new file mode 100644 index 000000000..df413c4d9 --- /dev/null +++ b/runtime-engine/networking/src/main/resources/META-INF/services/me.shedaniel.rei.api.common.networking.NetworkModule @@ -0,0 +1,6 @@ +me.shedaniel.rei.impl.client.networking.modules.CheatItemStatusNetworkModule +me.shedaniel.rei.impl.client.networking.modules.NotEnoughItemsNetworkModule +me.shedaniel.rei.impl.common.networking.modules.CheatItemGiveNetworkModule +me.shedaniel.rei.impl.common.networking.modules.CheatItemGrabNetworkModule +me.shedaniel.rei.impl.common.networking.modules.CheatItemHotbarNetworkModule +me.shedaniel.rei.impl.common.networking.modules.DeleteItemNetworkModule
\ No newline at end of file diff --git a/runtime-engine/networking/src/main/resources/META-INF/services/me.shedaniel.rei.api.common.networking.NetworkingHelper b/runtime-engine/networking/src/main/resources/META-INF/services/me.shedaniel.rei.api.common.networking.NetworkingHelper new file mode 100644 index 000000000..1020dd0a4 --- /dev/null +++ b/runtime-engine/networking/src/main/resources/META-INF/services/me.shedaniel.rei.api.common.networking.NetworkingHelper @@ -0,0 +1 @@ +me.shedaniel.rei.impl.common.networking.NetworkingHelperImpl
\ No newline at end of file |
