Browse Source

Add events for modcompat

Jared 4 years ago
parent
commit
c00b736dd9

+ 45 - 0
src/main/java/com/blamejared/controlling/api/events/KeyEntryListenersEvent.java

@@ -0,0 +1,45 @@
+package com.blamejared.controlling.api.events;
+
+import com.blamejared.controlling.client.gui.GuiNewKeyBindingList;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import com.mojang.blaze3d.matrix.MatrixStack;
+import net.minecraft.client.gui.IGuiEventListener;
+import net.minecraftforge.eventbus.api.Event;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * GetKeyEntryListenersEvent is called to get the values for {@link GuiNewKeyBindingList.KeyEntry#getEventListeners()}.
+ * Allowing for mods to add more listeners.
+ */
+public class KeyEntryListenersEvent extends Event {
+    
+    private final GuiNewKeyBindingList.KeyEntry entry;
+    
+    private final List<IGuiEventListener> listeners;
+    
+    public KeyEntryListenersEvent(GuiNewKeyBindingList.KeyEntry entry) {
+        
+        this.entry = entry;
+        this.listeners = new ArrayList<>();
+        
+        getListeners().add(entry.getBtnChangeKeyBinding());
+        getListeners().add(entry.getBtnResetKeyBinding());
+    }
+    
+    
+    public List<IGuiEventListener> getListeners() {
+        
+        return listeners;
+    }
+    
+    public GuiNewKeyBindingList.KeyEntry getEntry() {
+        
+        return entry;
+    }
+    
+}

+ 62 - 0
src/main/java/com/blamejared/controlling/api/events/KeyEntryMouseClickedEvent.java

@@ -0,0 +1,62 @@
+package com.blamejared.controlling.api.events;
+
+import com.blamejared.controlling.client.gui.GuiNewKeyBindingList;
+import net.minecraft.client.gui.IGuiEventListener;
+import net.minecraftforge.eventbus.api.Event;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * KeyEntryMouseClickedEvent is called at the start of {@link GuiNewKeyBindingList.KeyEntry#mouseClicked(double, double, int)}.
+ *
+ * If you are consuming this event, call {@link KeyEntryMouseClickedEvent#setHandled(boolean)} with a value of {@code true}.
+ */
+public class KeyEntryMouseClickedEvent extends Event {
+    
+    private final GuiNewKeyBindingList.KeyEntry entry;
+    private final double mouseX;
+    private final double mouseY;
+    private final int buttonId;
+    
+    private boolean handled;
+    
+    public KeyEntryMouseClickedEvent(GuiNewKeyBindingList.KeyEntry entry, double mouseX, double mouseY, int buttonId) {
+        
+        this.entry = entry;
+        this.mouseX = mouseX;
+        this.mouseY = mouseY;
+        this.buttonId = buttonId;
+    }
+    
+    public GuiNewKeyBindingList.KeyEntry getEntry() {
+        
+        return entry;
+    }
+    
+    public double getMouseX() {
+        
+        return mouseX;
+    }
+    
+    public double getMouseY() {
+        
+        return mouseY;
+    }
+    
+    public int getButtonId() {
+        
+        return buttonId;
+    }
+    
+    public boolean isHandled() {
+        
+        return handled;
+    }
+    
+    public void setHandled(boolean handled) {
+        
+        this.handled = handled;
+    }
+    
+}

+ 58 - 0
src/main/java/com/blamejared/controlling/api/events/KeyEntryMouseReleasedEvent.java

@@ -0,0 +1,58 @@
+package com.blamejared.controlling.api.events;
+
+import com.blamejared.controlling.client.gui.GuiNewKeyBindingList;
+import net.minecraftforge.eventbus.api.Event;
+
+/**
+ * KeyEntryMouseReleasedEvent is called at the start of {@link GuiNewKeyBindingList.KeyEntry#mouseReleased(double, double, int)}.
+ *
+ * If you are consuming this event, call {@link KeyEntryMouseReleasedEvent#setHandled(boolean)} with a value of {@code true}.
+ */
+public class KeyEntryMouseReleasedEvent extends Event {
+    
+    private final GuiNewKeyBindingList.KeyEntry entry;
+    private final double mouseX;
+    private final double mouseY;
+    private final int buttonId;
+    
+    private boolean handled;
+    
+    public KeyEntryMouseReleasedEvent(GuiNewKeyBindingList.KeyEntry entry, double mouseX, double mouseY, int buttonId) {
+        
+        this.entry = entry;
+        this.mouseX = mouseX;
+        this.mouseY = mouseY;
+        this.buttonId = buttonId;
+    }
+    
+    public GuiNewKeyBindingList.KeyEntry getEntry() {
+        
+        return entry;
+    }
+    
+    public double getMouseX() {
+        
+        return mouseX;
+    }
+    
+    public double getMouseY() {
+        
+        return mouseY;
+    }
+    
+    public int getButtonId() {
+        
+        return buttonId;
+    }
+    
+    public boolean isHandled() {
+        
+        return handled;
+    }
+    
+    public void setHandled(boolean handled) {
+        
+        this.handled = handled;
+    }
+    
+}

+ 96 - 0
src/main/java/com/blamejared/controlling/api/events/KeyEntryRenderEvent.java

@@ -0,0 +1,96 @@
+package com.blamejared.controlling.api.events;
+
+import com.blamejared.controlling.client.gui.GuiNewKeyBindingList;
+import com.mojang.blaze3d.matrix.MatrixStack;
+import net.minecraftforge.eventbus.api.Event;
+
+/**
+ * RenderKeyEntryEvent is called at the top of {@link com.blamejared.controlling.client.gui.GuiNewKeyBindingList.KeyEntry#render(MatrixStack, int, int, int, int, int, int, int, boolean, float)}
+ * is called, allowing mods to render additional info.
+ */
+public class KeyEntryRenderEvent extends Event {
+    
+    private final GuiNewKeyBindingList.KeyEntry entry;
+    
+    private final MatrixStack stack;
+    private final int slotIndex;
+    private final int y;
+    private final int x;
+    private final int rowLeft;
+    private final int rowWidth;
+    private final int mouseX;
+    private final int mouseY;
+    private final boolean hovered;
+    private final float partialTicks;
+    
+    public KeyEntryRenderEvent(GuiNewKeyBindingList.KeyEntry entry, MatrixStack stack, int slotIndex, int y, int x, int rowLeft, int rowWidth, int mouseX, int mouseY, boolean hovered, float partialTicks) {
+        
+        this.entry = entry;
+        this.stack = stack;
+        this.slotIndex = slotIndex;
+        this.y = y;
+        this.x = x;
+        this.rowLeft = rowLeft;
+        this.rowWidth = rowWidth;
+        this.mouseX = mouseX;
+        this.mouseY = mouseY;
+        this.hovered = hovered;
+        this.partialTicks = partialTicks;
+    }
+    
+    public GuiNewKeyBindingList.KeyEntry getEntry() {
+        
+        return entry;
+    }
+    
+    public MatrixStack getStack() {
+        
+        return stack;
+    }
+    
+    public int getSlotIndex() {
+        
+        return slotIndex;
+    }
+    
+    public int getY() {
+        
+        return y;
+    }
+    
+    public int getX() {
+        
+        return x;
+    }
+    
+    public int getRowLeft() {
+        
+        return rowLeft;
+    }
+    
+    public int getRowWidth() {
+        
+        return rowWidth;
+    }
+    
+    public int getMouseX() {
+        
+        return mouseX;
+    }
+    
+    public int getMouseY() {
+        
+        return mouseY;
+    }
+    
+    public boolean isHovered() {
+        
+        return hovered;
+    }
+    
+    public float getPartialTicks() {
+        
+        return partialTicks;
+    }
+    
+}

+ 78 - 17
src/main/java/com/blamejared/controlling/client/gui/GuiNewKeyBindingList.java

@@ -1,5 +1,9 @@
 package com.blamejared.controlling.client.gui;
 
+import com.blamejared.controlling.api.events.KeyEntryListenersEvent;
+import com.blamejared.controlling.api.events.KeyEntryMouseClickedEvent;
+import com.blamejared.controlling.api.events.KeyEntryMouseReleasedEvent;
+import com.blamejared.controlling.api.events.KeyEntryRenderEvent;
 import com.google.common.collect.ImmutableList;
 import com.mojang.blaze3d.matrix.MatrixStack;
 import net.minecraft.client.Minecraft;
@@ -12,6 +16,7 @@ import net.minecraft.client.settings.KeyBinding;
 import net.minecraft.util.math.MathHelper;
 import net.minecraft.util.text.*;
 import net.minecraftforge.api.distmarker.*;
+import net.minecraftforge.common.MinecraftForge;
 import net.minecraftforge.fml.client.gui.GuiUtils;
 import org.apache.commons.lang3.ArrayUtils;
 
@@ -25,6 +30,7 @@ public class GuiNewKeyBindingList extends GuiCustomList {
     private int maxListLabelWidth;
     
     public GuiNewKeyBindingList(ControlsScreen controls, Minecraft mcIn) {
+        
         super(controls, mcIn);
         this.width = controls.width + 45;
         this.height = controls.height;
@@ -61,30 +67,38 @@ public class GuiNewKeyBindingList extends GuiCustomList {
     
     @Override
     protected void renderDecorations(MatrixStack matrixStack, int mouseX, int mouseY) {
+        
         Entry entry = this.getEntryAtPos(mouseY);
         if(!(entry instanceof KeyEntry)) {
             return;
         }
         KeyEntry keyEntry = (KeyEntry) entry;
-        GuiUtils.drawHoveringText(matrixStack, Collections.singletonList(new TranslationTextComponent(keyEntry.getKeybinding().getKeyCategory())), mouseX, mouseY, mc.currentScreen.width, mc.currentScreen.height, 0, mc.fontRenderer);
+        GuiUtils.drawHoveringText(matrixStack, Collections.singletonList(new TranslationTextComponent(keyEntry
+                .getKeybinding()
+                .getKeyCategory())), mouseX, mouseY, mc.currentScreen.width, mc.currentScreen.height, 0, mc.fontRenderer);
     }
     
     public Entry getEntryAtPos(double mouseY) {
-        if(mouseY <= getTop() || mouseY >= getBottom()){
+        
+        if(mouseY <= getTop() || mouseY >= getBottom()) {
             return null;
         }
-        int i1 = MathHelper.floor(mouseY - (double) this.y0) - this.headerHeight + (int) this.getScrollAmount() - 4;
+        int i1 = MathHelper.floor(mouseY - (double) this.y0) - this.headerHeight + (int) this
+                .getScrollAmount() - 4;
         int j1 = i1 / this.itemHeight;
-        return i1 >= 0 && j1 < this.getItemCount() ? this.getEventListeners().get(j1) : null;
+        return i1 >= 0 && j1 < this.getItemCount() ? this.getEventListeners()
+                .get(j1) : null;
     }
     
     @Override
     protected int getScrollbarPosition() {
+        
         return super.getScrollbarPosition() + 15 + 20;
     }
     
     @Override
     public int getRowWidth() {
+        
         return super.getRowWidth() + 32;
     }
     
@@ -96,23 +110,28 @@ public class GuiNewKeyBindingList extends GuiCustomList {
         private final String name;
         
         public CategoryEntry(String name) {
+            
             this.labelText = I18n.format(name);
             this.labelWidth = GuiNewKeyBindingList.this.mc.fontRenderer.getStringWidth(this.labelText);
             this.name = name;
         }
         
         public String getName() {
+            
             return name;
         }
         
         @Override
         public List<? extends IGuiEventListener> getEventListeners() {
+            
             return ImmutableList.of();
         }
         
-        public void render(MatrixStack stack, int p_render_1_, int p_render_2_, int p_render_3_, int p_render_4_, int p_render_5_, int p_render_6_, int p_render_7_, boolean p_render_8_, float p_render_9_) {
-            GuiNewKeyBindingList.this.minecraft.fontRenderer.drawString(stack, this.labelText, (float) (GuiNewKeyBindingList.this.minecraft.currentScreen.width / 2 - this.labelWidth / 2), (float) (p_render_2_ + p_render_5_ - 9 - 1), 16777215);
+        public void render(MatrixStack stack, int slotIndex, int y, int x, int rowLeft, int rowWidth, int mouseX, int mouseY, boolean hovered, float partialTicks) {
+            
+            GuiNewKeyBindingList.this.minecraft.fontRenderer.drawString(stack, this.labelText, (float) (GuiNewKeyBindingList.this.minecraft.currentScreen.width / 2 - this.labelWidth / 2), (float) (y + rowWidth - 9 - 1), 16777215);
         }
+        
     }
     
     @OnlyIn(Dist.CLIENT)
@@ -131,6 +150,7 @@ public class GuiNewKeyBindingList extends GuiCustomList {
         
         
         private KeyEntry(final KeyBinding name) {
+            
             this.keybinding = name;
             this.keyDesc = I18n.format(name.getKeyDescription());
             this.btnChangeKeyBinding = new Button(0, 0, 75 + 20 /*Forge: add space*/, 20, new StringTextComponent(this.keyDesc), (p_214386_2_) -> {
@@ -138,32 +158,38 @@ public class GuiNewKeyBindingList extends GuiCustomList {
             }) {
                 @Override
                 protected IFormattableTextComponent getNarrationMessage() {
-                    return name.isInvalid() ? new TranslationTextComponent("narrator.controls.unbound", GuiNewKeyBindingList.KeyEntry.this.keyDesc) : new TranslationTextComponent("narrator.controls.bound", GuiNewKeyBindingList.KeyEntry.this.keyDesc, super.getNarrationMessage());
+                    
+                    return name.isInvalid() ? new TranslationTextComponent("narrator.controls.unbound", GuiNewKeyBindingList.KeyEntry.this.keyDesc) : new TranslationTextComponent("narrator.controls.bound", GuiNewKeyBindingList.KeyEntry.this.keyDesc, super
+                            .getNarrationMessage());
                 }
             };
             this.btnResetKeyBinding = new Button(0, 0, 50, 20, new TranslationTextComponent("controls.reset"), (p_214387_2_) -> {
                 keybinding.setToDefault();
-                GuiNewKeyBindingList.this.minecraft.gameSettings.setKeyBindingCode(name, name.getDefault());
+                GuiNewKeyBindingList.this.minecraft.gameSettings.setKeyBindingCode(name, name
+                        .getDefault());
                 KeyBinding.resetKeyBindingArrayAndHash();
             }) {
                 @Override
                 protected IFormattableTextComponent getNarrationMessage() {
+                    
                     return new TranslationTextComponent("narrator.controls.reset", GuiNewKeyBindingList.KeyEntry.this.keyDesc);
                 }
             };
         }
         
         @Override
-        public void render(MatrixStack stack, int slotIndex, int y, int x, int p_render_4_, int p_render_5_, int mouseX, int mouseY, boolean p_render_8_, float p_render_9_) {
+        public void render(MatrixStack stack, int slotIndex, int y, int x, int rowLeft, int rowWidth, int mouseX, int mouseY, boolean hovered, float partialTicks) {
+            
+            MinecraftForge.EVENT_BUS.post(new KeyEntryRenderEvent(this, stack, slotIndex, y, x, rowLeft, rowWidth, mouseX, mouseY, hovered, partialTicks));
             int i = y;
             int j = x;
             boolean flag = GuiNewKeyBindingList.this.controlsScreen.buttonId == this.keybinding;
             int length = Math.max(0, j + 90 - GuiNewKeyBindingList.this.maxListLabelWidth);
-            GuiNewKeyBindingList.this.mc.fontRenderer.drawString(stack, this.keyDesc, (float) (length), (float) (y + p_render_5_ / 2 - 9 / 2), 16777215);
+            GuiNewKeyBindingList.this.mc.fontRenderer.drawString(stack, this.keyDesc, (float) (length), (float) (y + rowWidth / 2 - 9 / 2), 16777215);
             this.btnResetKeyBinding.x = x + 190 + 20;
             this.btnResetKeyBinding.y = y;
             this.btnResetKeyBinding.active = !this.keybinding.isDefault();
-            this.btnResetKeyBinding.render(stack, mouseX, mouseY, p_render_9_);
+            this.btnResetKeyBinding.render(stack, mouseX, mouseY, partialTicks);
             
             
             this.btnChangeKeyBinding.x = j + 105;
@@ -182,23 +208,38 @@ public class GuiNewKeyBindingList extends GuiCustomList {
             }
             ITextComponent message = this.btnChangeKeyBinding.getMessage();
             if(flag) {
-                this.btnChangeKeyBinding.setMessage(new StringTextComponent(TextFormatting.WHITE + "> " + TextFormatting.YELLOW + message.getString() + TextFormatting.WHITE + " <"));
+                this.btnChangeKeyBinding.setMessage(new StringTextComponent(TextFormatting.WHITE + "> " + TextFormatting.YELLOW + message
+                        .getString() + TextFormatting.WHITE + " <"));
             } else if(flag1) {
-                IFormattableTextComponent modConflict = TextComponentUtils.func_240648_a_(message.copyRaw(), message.getStyle().setColor(Color.fromInt(16755200)));
-                IFormattableTextComponent keyConflict = TextComponentUtils.func_240648_a_(message.copyRaw(), message.getStyle().setColor(Color.fromInt(16755200)));
+                IFormattableTextComponent modConflict = TextComponentUtils.func_240648_a_(message
+                        .copyRaw(), message.getStyle()
+                        .setColor(Color.fromInt(16755200)));
+                IFormattableTextComponent keyConflict = TextComponentUtils.func_240648_a_(message
+                        .copyRaw(), message.getStyle()
+                        .setColor(Color.fromInt(16755200)));
                 
                 this.btnChangeKeyBinding.setMessage(keyCodeModifierConflict ? modConflict : keyConflict);
             }
             
-            this.btnChangeKeyBinding.render(stack, mouseX, mouseY, p_render_9_);
+            this.btnChangeKeyBinding.render(stack, mouseX, mouseY, partialTicks);
         }
         
-        public List<? extends IGuiEventListener> getEventListeners() {
-            return ImmutableList.of(this.btnChangeKeyBinding, this.btnResetKeyBinding);
+        public List<IGuiEventListener> getEventListeners() {
+            
+            KeyEntryListenersEvent event = new KeyEntryListenersEvent(this);
+            MinecraftForge.EVENT_BUS.post(event);
+            return event.getListeners();
         }
         
         @Override
         public boolean mouseClicked(double mouseX, double mouseY, int buttonId) {
+            
+            KeyEntryMouseClickedEvent event = new KeyEntryMouseClickedEvent(this, mouseX, mouseY, buttonId);
+            MinecraftForge.EVENT_BUS.post(event);
+            if(event.isHandled()) {
+                return true;
+            }
+            
             if(this.btnChangeKeyBinding.mouseClicked(mouseX, mouseY, buttonId)) {
                 return true;
             } else {
@@ -208,16 +249,36 @@ public class GuiNewKeyBindingList extends GuiCustomList {
         
         @Override
         public boolean mouseReleased(double mouseX, double mouseY, int buttonId) {
+    
+            KeyEntryMouseReleasedEvent event = new KeyEntryMouseReleasedEvent(this, mouseX, mouseY, buttonId);
+            MinecraftForge.EVENT_BUS.post(event);
+            if(event.isHandled()) {
+                return true;
+            }
+            
             return this.btnChangeKeyBinding.mouseReleased(mouseX, mouseY, buttonId);
         }
         
         public KeyBinding getKeybinding() {
+            
             return keybinding;
         }
         
         public String getKeyDesc() {
+            
             return keyDesc;
         }
         
+        public Button getBtnResetKeyBinding() {
+            
+            return btnResetKeyBinding;
+        }
+        
+        public Button getBtnChangeKeyBinding() {
+            
+            return btnChangeKeyBinding;
+        }
+        
     }
+    
 }