RoughlyEnoughItemsNetwork.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.item.ItemStack;
  36. import net.minecraft.network.PacketByteBuf;
  37. import net.minecraft.server.network.ServerPlayerEntity;
  38. import net.minecraft.text.TranslatableText;
  39. import net.minecraft.util.Formatting;
  40. import net.minecraft.util.Identifier;
  41. import net.minecraft.util.Util;
  42. import java.util.Comparator;
  43. import java.util.List;
  44. import java.util.Map;
  45. public class RoughlyEnoughItemsNetwork implements ModInitializer {
  46. public static final Identifier DELETE_ITEMS_PACKET = new Identifier("roughlyenoughitems", "delete_item");
  47. public static final Identifier CREATE_ITEMS_PACKET = new Identifier("roughlyenoughitems", "create_item");
  48. public static final Identifier CREATE_ITEMS_MESSAGE_PACKET = new Identifier("roughlyenoughitems", "ci_msg");
  49. public static final Identifier MOVE_ITEMS_PACKET = new Identifier("roughlyenoughitems", "move_items");
  50. public static final Identifier NOT_ENOUGH_ITEMS_PACKET = new Identifier("roughlyenoughitems", "og_not_enough");
  51. @Override
  52. public void onInitialize() {
  53. boolean loaded = FabricLoader.getInstance().isModLoaded("fabric-networking-v0");
  54. if (!loaded) {
  55. RoughlyEnoughItemsState.error("Fabric API is not installed!", "https://www.curseforge.com/minecraft/mc-mods/fabric-api/files/all");
  56. return;
  57. }
  58. Executor.run(() -> () -> {
  59. FabricLoader.getInstance().getEntrypoints("rei_containers", Runnable.class).forEach(Runnable::run);
  60. ServerSidePacketRegistry.INSTANCE.register(DELETE_ITEMS_PACKET, (packetContext, packetByteBuf) -> {
  61. ServerPlayerEntity player = (ServerPlayerEntity) packetContext.getPlayer();
  62. if (player.getServer().getPermissionLevel(player.getGameProfile()) < player.getServer().getOpPermissionLevel()) {
  63. player.addMessage(new TranslatableText("text.rei.no_permission_cheat").formatted(Formatting.RED), false);
  64. return;
  65. }
  66. if (!player.inventory.getCursorStack().isEmpty())
  67. player.inventory.setCursorStack(ItemStack.EMPTY);
  68. });
  69. ServerSidePacketRegistry.INSTANCE.register(CREATE_ITEMS_PACKET, (packetContext, packetByteBuf) -> {
  70. ServerPlayerEntity player = (ServerPlayerEntity) packetContext.getPlayer();
  71. if (player.getServer().getPermissionLevel(player.getGameProfile()) < player.getServer().getOpPermissionLevel()) {
  72. player.addMessage(new TranslatableText("text.rei.no_permission_cheat").formatted(Formatting.RED), false);
  73. return;
  74. }
  75. ItemStack stack = packetByteBuf.readItemStack();
  76. if (player.inventory.insertStack(stack.copy())) {
  77. ServerSidePacketRegistry.INSTANCE.sendToPlayer(player, RoughlyEnoughItemsNetwork.CREATE_ITEMS_MESSAGE_PACKET, new PacketByteBuf(Unpooled.buffer()).writeItemStack(stack.copy()).writeString(player.getEntityName(), 32767));
  78. } else
  79. player.addMessage(new TranslatableText("text.rei.failed_cheat_items"), false);
  80. });
  81. ServerSidePacketRegistry.INSTANCE.register(MOVE_ITEMS_PACKET, (packetContext, packetByteBuf) -> {
  82. Identifier category = packetByteBuf.readIdentifier();
  83. ServerPlayerEntity player = (ServerPlayerEntity) packetContext.getPlayer();
  84. Container container = player.container;
  85. PlayerContainer playerContainer = player.playerContainer;
  86. try {
  87. boolean shift = packetByteBuf.readBoolean();
  88. Map<Integer, List<ItemStack>> input = Maps.newHashMap();
  89. int mapSize = packetByteBuf.readInt();
  90. for (int i = 0; i < mapSize; i++) {
  91. List<ItemStack> list = Lists.newArrayList();
  92. int count = packetByteBuf.readInt();
  93. for (int j = 0; j < count; j++) {
  94. list.add(packetByteBuf.readItemStack());
  95. }
  96. input.put(i, list);
  97. }
  98. try {
  99. InputSlotCrafter.start(category, container, player, input, shift);
  100. } catch (InputSlotCrafter.NotEnoughMaterialsException e) {
  101. if (!(container instanceof CraftingContainer))
  102. return;
  103. PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
  104. buf.writeInt(input.size());
  105. input.entrySet().stream().sorted(Comparator.comparingInt(Map.Entry::getKey)).forEach(entry -> {
  106. List<ItemStack> stacks = entry.getValue();
  107. buf.writeInt(stacks.size());
  108. for (ItemStack stack : stacks) {
  109. buf.writeItemStack(stack);
  110. }
  111. });
  112. if (ServerSidePacketRegistry.INSTANCE.canPlayerReceive(player, NOT_ENOUGH_ITEMS_PACKET)) {
  113. ServerSidePacketRegistry.INSTANCE.sendToPlayer(player, NOT_ENOUGH_ITEMS_PACKET, buf);
  114. }
  115. } catch (IllegalStateException e) {
  116. player.sendMessage(new TranslatableText(e.getMessage()).formatted(Formatting.RED), Util.NIL_UUID);
  117. } catch (Exception e) {
  118. player.sendMessage(new TranslatableText("error.rei.internal.error", e.getMessage()).formatted(Formatting.RED), Util.NIL_UUID);
  119. e.printStackTrace();
  120. }
  121. } catch (Exception e) {
  122. e.printStackTrace();
  123. }
  124. });
  125. });
  126. }
  127. }