Advertisement
Guest User

Untitled

a guest
Jun 4th, 2020
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.11 KB | None | 0 0
  1.  
  2. package net.mcreator.electrona.gui;
  3.  
  4. import org.lwjgl.opengl.GL11;
  5.  
  6. import net.minecraftforge.fml.network.NetworkEvent;
  7. import net.minecraftforge.fml.network.IContainerFactory;
  8. import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
  9. import net.minecraftforge.eventbus.api.SubscribeEvent;
  10. import net.minecraftforge.event.RegistryEvent;
  11. import net.minecraftforge.api.distmarker.OnlyIn;
  12. import net.minecraftforge.api.distmarker.Dist;
  13.  
  14. import net.minecraft.world.World;
  15. import net.minecraft.util.text.TranslationTextComponent;
  16. import net.minecraft.util.text.ITextComponent;
  17. import net.minecraft.util.math.BlockPos;
  18. import net.minecraft.util.ResourceLocation;
  19. import net.minecraft.tileentity.TileEntity;
  20. import net.minecraft.network.PacketBuffer;
  21. import net.minecraft.item.Items;
  22. import net.minecraft.item.ItemStack;
  23. import net.minecraft.inventory.container.Slot;
  24. import net.minecraft.inventory.container.ContainerType;
  25. import net.minecraft.inventory.container.Container;
  26. import net.minecraft.inventory.Inventory;
  27. import net.minecraft.inventory.IInventory;
  28. import net.minecraft.entity.player.ServerPlayerEntity;
  29. import net.minecraft.entity.player.PlayerInventory;
  30. import net.minecraft.entity.player.PlayerEntity;
  31. import net.minecraft.client.gui.widget.button.Button;
  32. import net.minecraft.client.gui.screen.inventory.ContainerScreen;
  33. import net.minecraft.client.gui.ScreenManager;
  34. import net.minecraft.client.Minecraft;
  35.  
  36. import net.mcreator.electrona.procedures.XPGeneratorHarvestProcedure;
  37. import net.mcreator.electrona.ElectronaModElements;
  38. import net.mcreator.electrona.ElectronaMod;
  39.  
  40. import java.util.function.Supplier;
  41. import java.util.Map;
  42. import java.util.HashMap;
  43.  
  44. @ElectronaModElements.ModElement.Tag
  45. public class XPGeneratorGUIGui extends ElectronaModElements.ModElement {
  46.     public static HashMap guistate = new HashMap();
  47.     private static ContainerType<GuiContainerMod> containerType = null;
  48.     public XPGeneratorGUIGui(ElectronaModElements instance) {
  49.         super(instance, 106);
  50.         elements.addNetworkMessage(ButtonPressedMessage.class, ButtonPressedMessage::buffer, ButtonPressedMessage::new,
  51.                 ButtonPressedMessage::handler);
  52.         elements.addNetworkMessage(GUISlotChangedMessage.class, GUISlotChangedMessage::buffer, GUISlotChangedMessage::new,
  53.                 GUISlotChangedMessage::handler);
  54.         containerType = new ContainerType<>(new GuiContainerModFactory());
  55.         FMLJavaModLoadingContext.get().getModEventBus().register(this);
  56.     }
  57.  
  58.     @OnlyIn(Dist.CLIENT)
  59.     public void initElements() {
  60.         ScreenManager.registerFactory(containerType, GuiWindow::new);
  61.     }
  62.  
  63.     @SubscribeEvent
  64.     public void registerContainer(RegistryEvent.Register<ContainerType<?>> event) {
  65.         event.getRegistry().register(containerType.setRegistryName("xpgeneratorgui"));
  66.     }
  67.     public static class GuiContainerModFactory implements IContainerFactory {
  68.         public GuiContainerMod create(int id, PlayerInventory inv, PacketBuffer extraData) {
  69.             return new GuiContainerMod(id, inv, extraData);
  70.         }
  71.     }
  72.  
  73.     public static class GuiContainerMod extends Container implements Supplier<Map<Integer, Slot>> {
  74.         private World world;
  75.         private PlayerEntity entity;
  76.         private int x, y, z;
  77.         private IInventory internal;
  78.         private Map<Integer, Slot> customSlots = new HashMap<>();
  79.         public GuiContainerMod(int id, PlayerInventory inv, PacketBuffer extraData) {
  80.             super(containerType, id);
  81.             this.entity = inv.player;
  82.             this.world = inv.player.world;
  83.             this.internal = new Inventory(1);
  84.             if (extraData != null) {
  85.                 BlockPos pos = extraData.readBlockPos();
  86.                 this.x = pos.getX();
  87.                 this.y = pos.getY();
  88.                 this.z = pos.getZ();
  89.                 TileEntity ent = inv.player != null ? inv.player.world.getTileEntity(pos) : null;
  90.                 if (ent instanceof IInventory)
  91.                     this.internal = (IInventory) ent;
  92.             }
  93.             internal.openInventory(inv.player);
  94.             this.customSlots.put(0, this.addSlot(new Slot(internal, 0, 81, 19) {
  95.                 @Override
  96.                 public boolean isItemValid(ItemStack stack) {
  97.                     return (new ItemStack(Items.EMERALD, (int) (1)).getItem() == stack.getItem());
  98.                 }
  99.             }));
  100.             int si;
  101.             int sj;
  102.             for (si = 0; si < 3; ++si)
  103.                 for (sj = 0; sj < 9; ++sj)
  104.                     this.addSlot(new Slot(inv, sj + (si + 1) * 9, 0 + 8 + sj * 18, 0 + 84 + si * 18));
  105.             for (si = 0; si < 9; ++si)
  106.                 this.addSlot(new Slot(inv, si, 0 + 8 + si * 18, 0 + 142));
  107.         }
  108.  
  109.         public Map<Integer, Slot> get() {
  110.             return customSlots;
  111.         }
  112.  
  113.         @Override
  114.         public boolean canInteractWith(PlayerEntity player) {
  115.             return internal.isUsableByPlayer(player);
  116.         }
  117.  
  118.         @Override
  119.         public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
  120.             ItemStack itemstack = ItemStack.EMPTY;
  121.             Slot slot = (Slot) this.inventorySlots.get(index);
  122.             if (slot != null && slot.getHasStack()) {
  123.                 ItemStack itemstack1 = slot.getStack();
  124.                 itemstack = itemstack1.copy();
  125.                 if (index < 1) {
  126.                     if (!this.mergeItemStack(itemstack1, 1, this.inventorySlots.size(), true)) {
  127.                         return ItemStack.EMPTY;
  128.                     }
  129.                     slot.onSlotChange(itemstack1, itemstack);
  130.                 } else if (!this.mergeItemStack(itemstack1, 0, 1, false)) {
  131.                     if (index < 1 + 27) {
  132.                         if (!this.mergeItemStack(itemstack1, 1 + 27, this.inventorySlots.size(), true)) {
  133.                             return ItemStack.EMPTY;
  134.                         }
  135.                     } else {
  136.                         if (!this.mergeItemStack(itemstack1, 1, 1 + 27, false)) {
  137.                             return ItemStack.EMPTY;
  138.                         }
  139.                     }
  140.                     return ItemStack.EMPTY;
  141.                 }
  142.                 if (itemstack1.getCount() == 0) {
  143.                     slot.putStack(ItemStack.EMPTY);
  144.                 } else {
  145.                     slot.onSlotChanged();
  146.                 }
  147.                 if (itemstack1.getCount() == itemstack.getCount()) {
  148.                     return ItemStack.EMPTY;
  149.                 }
  150.                 slot.onTake(playerIn, itemstack1);
  151.             }
  152.             return itemstack;
  153.         }
  154.  
  155.         @Override /**
  156.                      * Merges provided ItemStack with the first avaliable one in the
  157.                      * container/player inventor between minIndex (included) and maxIndex
  158.                      * (excluded). Args : stack, minIndex, maxIndex, negativDirection. /!\ the
  159.                      * Container implementation do not check if the item is valid for the slot
  160.                      */
  161.         protected boolean mergeItemStack(ItemStack stack, int startIndex, int endIndex, boolean reverseDirection) {
  162.             boolean flag = false;
  163.             int i = startIndex;
  164.             if (reverseDirection) {
  165.                 i = endIndex - 1;
  166.             }
  167.             if (stack.isStackable()) {
  168.                 while (!stack.isEmpty()) {
  169.                     if (reverseDirection) {
  170.                         if (i < startIndex) {
  171.                             break;
  172.                         }
  173.                     } else if (i >= endIndex) {
  174.                         break;
  175.                     }
  176.                     Slot slot = this.inventorySlots.get(i);
  177.                     ItemStack itemstack = slot.getStack();
  178.                     if (slot.isItemValid(itemstack) && !itemstack.isEmpty() && areItemsAndTagsEqual(stack, itemstack)) {
  179.                         int j = itemstack.getCount() + stack.getCount();
  180.                         int maxSize = Math.min(slot.getSlotStackLimit(), stack.getMaxStackSize());
  181.                         if (j <= maxSize) {
  182.                             stack.setCount(0);
  183.                             itemstack.setCount(j);
  184.                             slot.putStack(itemstack);
  185.                             flag = true;
  186.                         } else if (itemstack.getCount() < maxSize) {
  187.                             stack.shrink(maxSize - itemstack.getCount());
  188.                             itemstack.setCount(maxSize);
  189.                             slot.putStack(itemstack);
  190.                             flag = true;
  191.                         }
  192.                     }
  193.                     if (reverseDirection) {
  194.                         --i;
  195.                     } else {
  196.                         ++i;
  197.                     }
  198.                 }
  199.             }
  200.             if (!stack.isEmpty()) {
  201.                 if (reverseDirection) {
  202.                     i = endIndex - 1;
  203.                 } else {
  204.                     i = startIndex;
  205.                 }
  206.                 while (true) {
  207.                     if (reverseDirection) {
  208.                         if (i < startIndex) {
  209.                             break;
  210.                         }
  211.                     } else if (i >= endIndex) {
  212.                         break;
  213.                     }
  214.                     Slot slot1 = this.inventorySlots.get(i);
  215.                     ItemStack itemstack1 = slot1.getStack();
  216.                     if (itemstack1.isEmpty() && slot1.isItemValid(stack)) {
  217.                         if (stack.getCount() > slot1.getSlotStackLimit()) {
  218.                             slot1.putStack(stack.split(slot1.getSlotStackLimit()));
  219.                         } else {
  220.                             slot1.putStack(stack.split(stack.getCount()));
  221.                         }
  222.                         slot1.onSlotChanged();
  223.                         flag = true;
  224.                         break;
  225.                     }
  226.                     if (reverseDirection) {
  227.                         --i;
  228.                     } else {
  229.                         ++i;
  230.                     }
  231.                 }
  232.             }
  233.             return flag;
  234.         }
  235.  
  236.         @Override
  237.         public void onContainerClosed(PlayerEntity playerIn) {
  238.             super.onContainerClosed(playerIn);
  239.             internal.closeInventory(playerIn);
  240.             if ((internal instanceof Inventory) && (playerIn instanceof ServerPlayerEntity)) {
  241.                 this.clearContainer(playerIn, playerIn.world, internal);
  242.             }
  243.         }
  244.  
  245.         private void slotChanged(int slotid, int ctype, int meta) {
  246.             if (this.world != null && this.world.isRemote) {
  247.                 ElectronaMod.PACKET_HANDLER.sendToServer(new GUISlotChangedMessage(slotid, x, y, z, ctype, meta));
  248.                 handleSlotAction(entity, slotid, ctype, meta, x, y, z);
  249.             }
  250.         }
  251.     }
  252.  
  253.     @OnlyIn(Dist.CLIENT)
  254.     public static class GuiWindow extends ContainerScreen<GuiContainerMod> {
  255.         private World world;
  256.         private int x, y, z;
  257.         private PlayerEntity entity;
  258.         public GuiWindow(GuiContainerMod container, PlayerInventory inventory, ITextComponent text) {
  259.             super(container, inventory, text);
  260.             this.world = container.world;
  261.             this.x = container.x;
  262.             this.y = container.y;
  263.             this.z = container.z;
  264.             this.entity = container.entity;
  265.             this.xSize = 176;
  266.             this.ySize = 166;
  267.         }
  268.         private static final ResourceLocation texture = new ResourceLocation("electrona:textures/xpgeneratorgui.png");
  269.         @Override
  270.         public void render(int mouseX, int mouseY, float partialTicks) {
  271.             this.renderBackground();
  272.             super.render(mouseX, mouseY, partialTicks);
  273.             this.renderHoveredToolTip(mouseX, mouseY);
  274.         }
  275.  
  276.         @Override
  277.         protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) {
  278.             GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
  279.             Minecraft.getInstance().getTextureManager().bindTexture(texture);
  280.             int k = (this.width - this.xSize) / 2;
  281.             int l = (this.height - this.ySize) / 2;
  282.             this.blit(k, l, 0, 0, this.xSize, this.ySize);
  283.         }
  284.  
  285.         @Override
  286.         public void tick() {
  287.             super.tick();
  288.         }
  289.        
  290.         @Override
  291.         protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
  292.             this.font.drawString(new TranslationTextComponent("electrona.xp_generator_gui.name").getFormattedText(), 8, 7, -16777216);
  293.             this.font.drawString(new TranslationTextComponent("electrona.generic_gui.power").getFormattedText(), 121, 7, -16777216);
  294.             this.font.drawString("" + ((int) new Object() {
  295.                 public double getValue(BlockPos pos, String tag) {
  296.                     TileEntity tileEntity = world.getTileEntity(pos);
  297.                     if (tileEntity != null)
  298.                         return tileEntity.getTileData().getDouble(tag);
  299.                     return 0;
  300.                 }
  301.             }.getValue(new BlockPos((int) x, (int) y, (int) z), "ElectronicPower")) + " ELs", 121, 17, -3407821);
  302.             this.font.drawString(new TranslationTextComponent("electrona.xp_generator_gui.xp_levels").getFormattedText(), 121, 26, -16777216);
  303.             this.font.drawString("" + ((int) new Object() {
  304.                 public double getValue(BlockPos pos, String tag) {
  305.                     TileEntity tileEntity = world.getTileEntity(pos);
  306.                     if (tileEntity != null)
  307.                         return tileEntity.getTileData().getDouble(tag);
  308.                     return 0;
  309.                 }
  310.             }.getValue(new BlockPos((int) x, (int) y, (int) z), "XPlevels")) + "", 121, 36, -13395712);
  311.             this.font.drawString(new TranslationTextComponent("electrona.xp_generator_gui.fuel").getFormattedText(), 79, 37, -16777216);
  312.         }
  313.  
  314.         @Override
  315.         public void removed() {
  316.             super.removed();
  317.             Minecraft.getInstance().keyboardListener.enableRepeatEvents(false);
  318.         }
  319.  
  320.         @Override
  321.         public void init(Minecraft minecraft, int width, int height) {
  322.             super.init(minecraft, width, height);
  323.             minecraft.keyboardListener.enableRepeatEvents(true);
  324.             this.addButton(new Button(this.guiLeft + 98, this.guiTop + 60, 70, 20,
  325.                     new TranslationTextComponent("electrona.xp_generator_gui.harvest_button").getFormattedText(), e -> {
  326.                         ElectronaMod.PACKET_HANDLER.sendToServer(new ButtonPressedMessage(0, x, y, z));
  327.                         handleButtonAction(entity, 0, x, y, z);
  328.                     }));
  329.         }
  330.     }
  331.  
  332.     public static class ButtonPressedMessage {
  333.         int buttonID, x, y, z;
  334.         public ButtonPressedMessage(PacketBuffer buffer) {
  335.             this.buttonID = buffer.readInt();
  336.             this.x = buffer.readInt();
  337.             this.y = buffer.readInt();
  338.             this.z = buffer.readInt();
  339.         }
  340.  
  341.         public ButtonPressedMessage(int buttonID, int x, int y, int z) {
  342.             this.buttonID = buttonID;
  343.             this.x = x;
  344.             this.y = y;
  345.             this.z = z;
  346.         }
  347.  
  348.         public static void buffer(ButtonPressedMessage message, PacketBuffer buffer) {
  349.             buffer.writeInt(message.buttonID);
  350.             buffer.writeInt(message.x);
  351.             buffer.writeInt(message.y);
  352.             buffer.writeInt(message.z);
  353.         }
  354.  
  355.         public static void handler(ButtonPressedMessage message, Supplier<NetworkEvent.Context> contextSupplier) {
  356.             NetworkEvent.Context context = contextSupplier.get();
  357.             context.enqueueWork(() -> {
  358.                 PlayerEntity entity = context.getSender();
  359.                 int buttonID = message.buttonID;
  360.                 int x = message.x;
  361.                 int y = message.y;
  362.                 int z = message.z;
  363.                 handleButtonAction(entity, buttonID, x, y, z);
  364.             });
  365.             context.setPacketHandled(true);
  366.         }
  367.     }
  368.  
  369.     public static class GUISlotChangedMessage {
  370.         int slotID, x, y, z, changeType, meta;
  371.         public GUISlotChangedMessage(int slotID, int x, int y, int z, int changeType, int meta) {
  372.             this.slotID = slotID;
  373.             this.x = x;
  374.             this.y = y;
  375.             this.z = z;
  376.             this.changeType = changeType;
  377.             this.meta = meta;
  378.         }
  379.  
  380.         public GUISlotChangedMessage(PacketBuffer buffer) {
  381.             this.slotID = buffer.readInt();
  382.             this.x = buffer.readInt();
  383.             this.y = buffer.readInt();
  384.             this.z = buffer.readInt();
  385.             this.changeType = buffer.readInt();
  386.             this.meta = buffer.readInt();
  387.         }
  388.  
  389.         public static void buffer(GUISlotChangedMessage message, PacketBuffer buffer) {
  390.             buffer.writeInt(message.slotID);
  391.             buffer.writeInt(message.x);
  392.             buffer.writeInt(message.y);
  393.             buffer.writeInt(message.z);
  394.             buffer.writeInt(message.changeType);
  395.             buffer.writeInt(message.meta);
  396.         }
  397.  
  398.         public static void handler(GUISlotChangedMessage message, Supplier<NetworkEvent.Context> contextSupplier) {
  399.             NetworkEvent.Context context = contextSupplier.get();
  400.             context.enqueueWork(() -> {
  401.                 PlayerEntity entity = context.getSender();
  402.                 int slotID = message.slotID;
  403.                 int changeType = message.changeType;
  404.                 int meta = message.meta;
  405.                 int x = message.x;
  406.                 int y = message.y;
  407.                 int z = message.z;
  408.                 handleSlotAction(entity, slotID, changeType, meta, x, y, z);
  409.             });
  410.             context.setPacketHandled(true);
  411.         }
  412.     }
  413.     private static void handleButtonAction(PlayerEntity entity, int buttonID, int x, int y, int z) {
  414.         World world = entity.world;
  415.         // security measure to prevent arbitrary chunk generation
  416.         if (!world.isBlockLoaded(new BlockPos(x, y, z)))
  417.             return;
  418.         if (buttonID == 0) {
  419.             {
  420.                 java.util.HashMap<String, Object> $_dependencies = new java.util.HashMap<>();
  421.                 $_dependencies.put("entity", entity);
  422.                 $_dependencies.put("x", x);
  423.                 $_dependencies.put("y", y);
  424.                 $_dependencies.put("z", z);
  425.                 $_dependencies.put("world", world);
  426.                 XPGeneratorHarvestProcedure.executeProcedure($_dependencies);
  427.             }
  428.         }
  429.     }
  430.  
  431.     private static void handleSlotAction(PlayerEntity entity, int slotID, int changeType, int meta, int x, int y, int z) {
  432.         World world = entity.world;
  433.         // security measure to prevent arbitrary chunk generation
  434.         if (!world.isBlockLoaded(new BlockPos(x, y, z)))
  435.             return;
  436.     }
  437. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement