RoughlyEnoughItemsNetwork.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * This file is licensed under the MIT License, part of Roughly Enough Items.
  3. * Copyright (c) 2018, 2019, 2020 shedaniel
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. package me.shedaniel.rei;
  24. import com.google.common.collect.Lists;
  25. import com.google.common.collect.Maps;
  26. import io.netty.buffer.Unpooled;
  27. import me.shedaniel.math.api.Executor;
  28. import me.shedaniel.rei.server.InputSlotCrafter;
  29. import net.fabricmc.api.ModInitializer;
  30. import net.fabricmc.fabric.api.network.ServerSidePacketRegistry;
  31. import net.fabricmc.loader.api.FabricLoader;
  32. import net.minecraft.container.Container;
  33. import net.minecraft.container.CraftingContainer;
  34. import net.minecraft.container.PlayerContainer;
  35. import net.minecraft.entity.player.PlayerInventory;
  36. import net.minecraft.item.ItemStack;
  37. import net.minecraft.network.PacketByteBuf;
  38. import net.minecraft.server.network.ServerPlayerEntity;
  39. import net.minecraft.text.TranslatableText;
  40. import net.minecraft.util.Formatting;
  41. import net.minecraft.util.Identifier;
  42. import net.minecraft.util.Util;
  43. import net.minecraft.util.math.MathHelper;
  44. import java.util.Comparator;
  45. import java.util.List;
  46. import java.util.Map;
  47. public class RoughlyEnoughItemsNetwork implements ModInitializer {
  48. public static final Identifier DELETE_ITEMS_PACKET = new Identifier("roughlyenoughitems", "delete_item");
  49. public static final Identifier CREATE_ITEMS_PACKET = new Identifier("roughlyenoughitems", "create_item");
  50. public static final Identifier CREATE_ITEMS_GRAB_PACKET = new Identifier("roughlyenoughitems", "create_item_grab");
  51. public static final Identifier CREATE_ITEMS_MESSAGE_PACKET = new Identifier("roughlyenoughitems", "ci_msg");
  52. public static final Identifier MOVE_ITEMS_PACKET = new Identifier("roughlyenoughitems", "move_items");
  53. public static final Identifier NOT_ENOUGH_ITEMS_PACKET = new Identifier("roughlyenoughitems", "og_not_enough");
  54. @Override
  55. public void onInitialize() {
  56. boolean loaded = FabricLoader.getInstance().isModLoaded("fabric-networking-v0");
  57. if (!loaded) {
  58. RoughlyEnoughItemsState.error("Fabric API is not installed!", "https://www.curseforge.com/minecraft/mc-mods/fabric-api/files/all");
  59. return;
  60. }
  61. Executor.run(() -> () -> {
  62. FabricLoader.getInstance().getEntrypoints("rei_containers", Runnable.class).forEach(Runnable::run);
  63. ServerSidePacketRegistry.INSTANCE.register(DELETE_ITEMS_PACKET, (packetContext, packetByteBuf) -> {
  64. ServerPlayerEntity player = (ServerPlayerEntity) packetContext.getPlayer();
  65. if (player.getServer().getPermissionLevel(player.getGameProfile()) < player.getServer().getOpPermissionLevel()) {
  66. player.addMessage(new TranslatableText("text.rei.no_permission_cheat").formatted(Formatting.RED), false);
  67. return;
  68. }
  69. if (!player.inventory.getCursorStack().isEmpty()) {
  70. player.inventory.setCursorStack(ItemStack.EMPTY);
  71. player.updateCursorStack();
  72. }
  73. });
  74. ServerSidePacketRegistry.INSTANCE.register(CREATE_ITEMS_PACKET, (packetContext, packetByteBuf) -> {
  75. ServerPlayerEntity player = (ServerPlayerEntity) packetContext.getPlayer();
  76. if (player.getServer().getPermissionLevel(player.getGameProfile()) < player.getServer().getOpPermissionLevel()) {
  77. player.addMessage(new TranslatableText("text.rei.no_permission_cheat").formatted(Formatting.RED), false);
  78. return;
  79. }
  80. ItemStack stack = packetByteBuf.readItemStack();
  81. if (player.inventory.insertStack(stack.copy())) {
  82. ServerSidePacketRegistry.INSTANCE.sendToPlayer(player, RoughlyEnoughItemsNetwork.CREATE_ITEMS_MESSAGE_PACKET, new PacketByteBuf(Unpooled.buffer()).writeItemStack(stack.copy()).writeString(player.getEntityName(), 32767));
  83. } else
  84. player.addMessage(new TranslatableText("text.rei.failed_cheat_items"), false);
  85. });
  86. ServerSidePacketRegistry.INSTANCE.register(CREATE_ITEMS_GRAB_PACKET, (packetContext, packetByteBuf) -> {
  87. ServerPlayerEntity player = (ServerPlayerEntity) packetContext.getPlayer();
  88. if (player.getServer().getPermissionLevel(player.getGameProfile()) < player.getServer().getOpPermissionLevel()) {
  89. player.addMessage(new TranslatableText("text.rei.no_permission_cheat").formatted(Formatting.RED), false);
  90. return;
  91. }
  92. PlayerInventory inventory = player.inventory;
  93. ItemStack itemStack = packetByteBuf.readItemStack();
  94. ItemStack stack = itemStack.copy();
  95. if (!inventory.getCursorStack().isEmpty() && ItemStack.areItemsEqual(inventory.getCursorStack(), stack) && ItemStack.areTagsEqual(inventory.getCursorStack(), stack)) {
  96. stack.setCount(MathHelper.clamp(stack.getCount() + inventory.getCursorStack().getCount(), 1, stack.getMaxCount()));
  97. } else if (!inventory.getCursorStack().isEmpty()) {
  98. return;
  99. }
  100. inventory.setCursorStack(stack.copy());
  101. player.updateCursorStack();
  102. ServerSidePacketRegistry.INSTANCE.sendToPlayer(player, RoughlyEnoughItemsNetwork.CREATE_ITEMS_MESSAGE_PACKET, new PacketByteBuf(Unpooled.buffer()).writeItemStack(itemStack.copy()).writeString(player.getEntityName(), 32767));
  103. });
  104. ServerSidePacketRegistry.INSTANCE.register(MOVE_ITEMS_PACKET, (packetContext, packetByteBuf) -> {
  105. Identifier category = packetByteBuf.readIdentifier();
  106. ServerPlayerEntity player = (ServerPlayerEntity) packetContext.getPlayer();
  107. Container container = player.container;
  108. PlayerContainer playerContainer = player.playerContainer;
  109. try {
  110. boolean shift = packetByteBuf.readBoolean();
  111. Map<Integer, List<ItemStack>> input = Maps.newHashMap();
  112. int mapSize = packetByteBuf.readInt();
  113. for (int i = 0; i < mapSize; i++) {
  114. List<ItemStack> list = Lists.newArrayList();
  115. int count = packetByteBuf.readInt();
  116. for (int j = 0; j < count; j++) {
  117. list.add(packetByteBuf.readItemStack());
  118. }
  119. input.put(i, list);
  120. }
  121. try {
  122. InputSlotCrafter.start(category, container, player, input, shift);
  123. } catch (InputSlotCrafter.NotEnoughMaterialsException e) {
  124. if (!(container instanceof CraftingContainer))
  125. return;
  126. PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
  127. buf.writeInt(input.size());
  128. input.entrySet().stream().sorted(Comparator.comparingInt(Map.Entry::getKey)).forEach(entry -> {
  129. List<ItemStack> stacks = entry.getValue();
  130. buf.writeInt(stacks.size());
  131. for (ItemStack stack : stacks) {
  132. buf.writeItemStack(stack);
  133. }
  134. });
  135. if (ServerSidePacketRegistry.INSTANCE.canPlayerReceive(player, NOT_ENOUGH_ITEMS_PACKET)) {
  136. ServerSidePacketRegistry.INSTANCE.sendToPlayer(player, NOT_ENOUGH_ITEMS_PACKET, buf);
  137. }
  138. } catch (IllegalStateException e) {
  139. player.sendMessage(new TranslatableText(e.getMessage()).formatted(Formatting.RED), Util.NIL_UUID);
  140. } catch (Exception e) {
  141. player.sendMessage(new TranslatableText("error.rei.internal.error", e.getMessage()).formatted(Formatting.RED), Util.NIL_UUID);
  142. e.printStackTrace();
  143. }
  144. } catch (Exception e) {
  145. e.printStackTrace();
  146. }
  147. });
  148. });
  149. }
  150. }