MixinGuiContainerCreative.java 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package me.shedaniel.mixins;
  2. import me.shedaniel.listenerdefinitions.GuiKeyDown;
  3. import net.minecraft.client.gui.GuiTextField;
  4. import net.minecraft.client.gui.inventory.GuiContainerCreative;
  5. import net.minecraft.client.renderer.InventoryEffectRenderer;
  6. import net.minecraft.inventory.Container;
  7. import net.minecraft.inventory.Slot;
  8. import net.minecraft.item.ItemGroup;
  9. import net.minecraft.util.math.MathHelper;
  10. import org.dimdev.riftloader.RiftLoader;
  11. import org.spongepowered.asm.mixin.Mixin;
  12. import org.spongepowered.asm.mixin.Overwrite;
  13. import org.spongepowered.asm.mixin.Shadow;
  14. import javax.annotation.Nullable;
  15. import java.util.Objects;
  16. @Mixin(GuiContainerCreative.class)
  17. public abstract class MixinGuiContainerCreative extends InventoryEffectRenderer {
  18. @Shadow
  19. private static int selectedTabIndex = ItemGroup.BUILDING_BLOCKS.getIndex();
  20. @Shadow
  21. private GuiTextField searchField;
  22. @Shadow
  23. protected abstract boolean hasTmpInventory(@Nullable Slot p_208018_1_);
  24. @Shadow
  25. protected abstract void setCurrentCreativeTab(ItemGroup tab);
  26. @Shadow
  27. protected abstract void updateCreativeSearch();
  28. @Shadow
  29. private boolean field_195377_F;
  30. @Shadow
  31. protected abstract boolean needsScrollBars();
  32. @Shadow
  33. private float currentScroll;
  34. public MixinGuiContainerCreative(Container inventorySlotsIn) {
  35. super(inventorySlotsIn);
  36. }
  37. @Overwrite
  38. public boolean keyPressed(int p_keyPressed_1_, int p_keyPressed_2_, int p_keyPressed_3_) {
  39. this.field_195377_F = false;
  40. if (selectedTabIndex != ItemGroup.SEARCH.getIndex()) {
  41. if (selectedTabIndex != ItemGroup.INVENTORY.getIndex()) {
  42. if (this.mc.gameSettings.keyBindChat.matchesKey(p_keyPressed_1_, p_keyPressed_2_)) {
  43. this.field_195377_F = true;
  44. this.setCurrentCreativeTab(ItemGroup.SEARCH);
  45. return true;
  46. }
  47. } else for(GuiKeyDown listener : RiftLoader.instance.getListeners(GuiKeyDown.class))
  48. if (listener.keyDown(p_keyPressed_1_, p_keyPressed_2_, p_keyPressed_3_))
  49. return true;
  50. } else {
  51. boolean flag = !this.hasTmpInventory(this.hoveredSlot) || this.hoveredSlot != null && this.hoveredSlot.getHasStack();
  52. if (flag && this.func_195363_d(p_keyPressed_1_, p_keyPressed_2_)) {
  53. this.field_195377_F = true;
  54. return true;
  55. } else {
  56. String s = this.searchField.getText();
  57. if (this.searchField.keyPressed(p_keyPressed_1_, p_keyPressed_2_, p_keyPressed_3_)) {
  58. if (!Objects.equals(s, this.searchField.getText()))
  59. this.updateCreativeSearch();
  60. return true;
  61. }
  62. }
  63. }
  64. return super.keyPressed(p_keyPressed_1_, p_keyPressed_2_, p_keyPressed_3_);
  65. }
  66. @Overwrite
  67. public boolean mouseScrolled(double p_mouseScrolled_1_) {
  68. if (!this.needsScrollBars()) {
  69. return super.mouseScrolled(p_mouseScrolled_1_);
  70. } else {
  71. int i = (((GuiContainerCreative.ContainerCreative) this.inventorySlots).itemList.size() + 9 - 1) / 9 - 5;
  72. this.currentScroll = (float) ((double) this.currentScroll - p_mouseScrolled_1_ / (double) i);
  73. this.currentScroll = MathHelper.clamp(this.currentScroll, 0.0F, 1.0F);
  74. ((GuiContainerCreative.ContainerCreative) this.inventorySlots).scrollTo(this.currentScroll);
  75. return true;
  76. }
  77. }
  78. }