Commit 7d6e88cdecb463e37e403eaced9f3c7260a0beca

Authored by Mumfrey
1 parent a23a9ea1

Add PlayerClickListener

src/client/java/com/mumfrey/liteloader/client/ClientProxy.java
@@ -10,10 +10,12 @@ import java.io.File; @@ -10,10 +10,12 @@ import java.io.File;
10 import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; 10 import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
11 import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; 11 import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
12 12
  13 +import com.mumfrey.liteloader.PlayerInteractionListener.MouseButton;
13 import com.mumfrey.liteloader.client.ducks.IFramebuffer; 14 import com.mumfrey.liteloader.client.ducks.IFramebuffer;
14 import com.mumfrey.liteloader.core.Proxy; 15 import com.mumfrey.liteloader.core.Proxy;
15 16
16 import net.minecraft.client.Minecraft; 17 import net.minecraft.client.Minecraft;
  18 +import net.minecraft.client.entity.EntityPlayerSP;
17 import net.minecraft.client.gui.GuiNewChat; 19 import net.minecraft.client.gui.GuiNewChat;
18 import net.minecraft.client.renderer.RenderGlobal; 20 import net.minecraft.client.renderer.RenderGlobal;
19 import net.minecraft.client.renderer.entity.Render; 21 import net.minecraft.client.renderer.entity.Render;
@@ -187,4 +189,14 @@ public abstract class ClientProxy extends Proxy @@ -187,4 +189,14 @@ public abstract class ClientProxy extends Proxy
187 { 189 {
188 ClientProxy.broker.onPostRenderEntity(source, entity, x, y, z, yaw, pTicks, render); 190 ClientProxy.broker.onPostRenderEntity(source, entity, x, y, z, yaw, pTicks, render);
189 } 191 }
  192 +
  193 + public static boolean onClickMouse(EntityPlayerSP player, MouseButton button)
  194 + {
  195 + return ClientProxy.broker.onClickMouse(player, button);
  196 + }
  197 +
  198 + public static boolean onMouseHeld(EntityPlayerSP player, MouseButton button)
  199 + {
  200 + return ClientProxy.broker.onMouseHeld(player, button);
  201 + }
190 } 202 }
src/client/java/com/mumfrey/liteloader/client/LiteLoaderEventBrokerClient.java
@@ -17,6 +17,8 @@ import com.mumfrey.liteloader.HUDRenderListener; @@ -17,6 +17,8 @@ import com.mumfrey.liteloader.HUDRenderListener;
17 import com.mumfrey.liteloader.InitCompleteListener; 17 import com.mumfrey.liteloader.InitCompleteListener;
18 import com.mumfrey.liteloader.OutboundChatFilter; 18 import com.mumfrey.liteloader.OutboundChatFilter;
19 import com.mumfrey.liteloader.OutboundChatListener; 19 import com.mumfrey.liteloader.OutboundChatListener;
  20 +import com.mumfrey.liteloader.PlayerClickListener;
  21 +import com.mumfrey.liteloader.PlayerInteractionListener.MouseButton;
20 import com.mumfrey.liteloader.PostRenderListener; 22 import com.mumfrey.liteloader.PostRenderListener;
21 import com.mumfrey.liteloader.PreRenderListener; 23 import com.mumfrey.liteloader.PreRenderListener;
22 import com.mumfrey.liteloader.RenderListener; 24 import com.mumfrey.liteloader.RenderListener;
@@ -37,6 +39,7 @@ import com.mumfrey.liteloader.launch.LoaderProperties; @@ -37,6 +39,7 @@ import com.mumfrey.liteloader.launch.LoaderProperties;
37 import com.mumfrey.liteloader.util.log.LiteLoaderLogger; 39 import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
38 40
39 import net.minecraft.client.Minecraft; 41 import net.minecraft.client.Minecraft;
  42 +import net.minecraft.client.entity.EntityPlayerSP;
40 import net.minecraft.client.gui.GuiNewChat; 43 import net.minecraft.client.gui.GuiNewChat;
41 import net.minecraft.client.gui.ScaledResolution; 44 import net.minecraft.client.gui.ScaledResolution;
42 import net.minecraft.client.renderer.RenderGlobal; 45 import net.minecraft.client.renderer.RenderGlobal;
@@ -100,6 +103,8 @@ public class LiteLoaderEventBrokerClient extends LiteLoaderEventBroker<Minecraft @@ -100,6 +103,8 @@ public class LiteLoaderEventBrokerClient extends LiteLoaderEventBroker<Minecraft
100 private FastIterableDeque<ScreenshotListener> screenshotListeners = new HandlerList<ScreenshotListener>(ScreenshotListener.class, 103 private FastIterableDeque<ScreenshotListener> screenshotListeners = new HandlerList<ScreenshotListener>(ScreenshotListener.class,
101 ReturnLogicOp.AND_BREAK_ON_FALSE); 104 ReturnLogicOp.AND_BREAK_ON_FALSE);
102 private FastIterableDeque<EntityRenderListener> entityRenderListeners = new HandlerList<EntityRenderListener>(EntityRenderListener.class); 105 private FastIterableDeque<EntityRenderListener> entityRenderListeners = new HandlerList<EntityRenderListener>(EntityRenderListener.class);
  106 + private FastIterableDeque<PlayerClickListener> playerClickListeners = new HandlerList<PlayerClickListener>(PlayerClickListener.class,
  107 + ReturnLogicOp.AND);
103 108
104 @SuppressWarnings("cast") 109 @SuppressWarnings("cast")
105 public LiteLoaderEventBrokerClient(LiteLoader loader, GameEngineClient engine, LoaderProperties properties) 110 public LiteLoaderEventBrokerClient(LiteLoader loader, GameEngineClient engine, LoaderProperties properties)
@@ -146,6 +151,7 @@ public class LiteLoaderEventBrokerClient extends LiteLoaderEventBroker&lt;Minecraft @@ -146,6 +151,7 @@ public class LiteLoaderEventBrokerClient extends LiteLoaderEventBroker&lt;Minecraft
146 delegate.registerInterface(OutboundChatFilter.class); 151 delegate.registerInterface(OutboundChatFilter.class);
147 delegate.registerInterface(ScreenshotListener.class); 152 delegate.registerInterface(ScreenshotListener.class);
148 delegate.registerInterface(EntityRenderListener.class); 153 delegate.registerInterface(EntityRenderListener.class);
  154 + delegate.registerInterface(PlayerClickListener.class);
149 } 155 }
150 156
151 /* (non-Javadoc) 157 /* (non-Javadoc)
@@ -267,6 +273,14 @@ public class LiteLoaderEventBrokerClient extends LiteLoaderEventBroker&lt;Minecraft @@ -267,6 +273,14 @@ public class LiteLoaderEventBrokerClient extends LiteLoaderEventBroker&lt;Minecraft
267 { 273 {
268 this.entityRenderListeners.add(entityRenderListener); 274 this.entityRenderListeners.add(entityRenderListener);
269 } 275 }
  276 +
  277 + /**
  278 + * @param playerClickListener
  279 + */
  280 + public void addEntityRenderListener(PlayerClickListener playerClickListener)
  281 + {
  282 + this.playerClickListeners.add(playerClickListener);
  283 + }
270 284
271 /** 285 /**
272 * Late initialisation callback 286 * Late initialisation callback
@@ -581,4 +595,14 @@ public class LiteLoaderEventBrokerClient extends LiteLoaderEventBroker&lt;Minecraft @@ -581,4 +595,14 @@ public class LiteLoaderEventBrokerClient extends LiteLoaderEventBroker&lt;Minecraft
581 { 595 {
582 this.entityRenderListeners.all().onPostRenderEntity(render, entity, xPos, yPos, zPos, yaw, partialTicks); 596 this.entityRenderListeners.all().onPostRenderEntity(render, entity, xPos, yPos, zPos, yaw, partialTicks);
583 } 597 }
  598 +
  599 + public boolean onClickMouse(EntityPlayerSP player, MouseButton button)
  600 + {
  601 + return this.playerClickListeners.all().onMouseClicked(player, button);
  602 + }
  603 +
  604 + public boolean onMouseHeld(EntityPlayerSP player, MouseButton button)
  605 + {
  606 + return this.playerClickListeners.all().onMouseHeld(player, button);
  607 + }
584 } 608 }
src/client/java/com/mumfrey/liteloader/client/mixin/MixinMinecraft.java
@@ -16,6 +16,7 @@ import org.spongepowered.asm.mixin.injection.Inject; @@ -16,6 +16,7 @@ import org.spongepowered.asm.mixin.injection.Inject;
16 import org.spongepowered.asm.mixin.injection.Redirect; 16 import org.spongepowered.asm.mixin.injection.Redirect;
17 import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; 17 import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
18 18
  19 +import com.mumfrey.liteloader.PlayerInteractionListener.MouseButton;
19 import com.mumfrey.liteloader.client.ClientProxy; 20 import com.mumfrey.liteloader.client.ClientProxy;
20 import com.mumfrey.liteloader.client.overlays.IMinecraft; 21 import com.mumfrey.liteloader.client.overlays.IMinecraft;
21 22
@@ -35,6 +36,9 @@ public abstract class MixinMinecraft implements IMinecraft @@ -35,6 +36,9 @@ public abstract class MixinMinecraft implements IMinecraft
35 @Shadow private int serverPort; 36 @Shadow private int serverPort;
36 37
37 @Shadow abstract void resize(int width, int height); 38 @Shadow abstract void resize(int width, int height);
  39 + @Shadow private void clickMouse() {}
  40 + @Shadow private void rightClickMouse() {}
  41 + @Shadow private void middleClickMouse() {}
38 42
39 @Inject(method = "startGame()V", at = @At("RETURN")) 43 @Inject(method = "startGame()V", at = @At("RETURN"))
40 private void onStartupComplete(CallbackInfo ci) 44 private void onStartupComplete(CallbackInfo ci)
@@ -93,7 +97,7 @@ public abstract class MixinMinecraft implements IMinecraft @@ -93,7 +97,7 @@ public abstract class MixinMinecraft implements IMinecraft
93 ClientProxy.onTimerUpdate(); 97 ClientProxy.onTimerUpdate();
94 } 98 }
95 99
96 - @Inject (method = "runGameLoop()V", at = @At( 100 + @Inject(method = "runGameLoop()V", at = @At(
97 value = "INVOKE_STRING", 101 value = "INVOKE_STRING",
98 target = "Lnet/minecraft/profiler/Profiler;endStartSection(Ljava/lang/String;)V", 102 target = "Lnet/minecraft/profiler/Profiler;endStartSection(Ljava/lang/String;)V",
99 args = "ldc=gameRenderer" 103 args = "ldc=gameRenderer"
@@ -102,6 +106,71 @@ public abstract class MixinMinecraft implements IMinecraft @@ -102,6 +106,71 @@ public abstract class MixinMinecraft implements IMinecraft
102 { 106 {
103 ClientProxy.onRender(); 107 ClientProxy.onRender();
104 } 108 }
  109 +
  110 + @Redirect(method = "processKeyBinds()V", at = @At(
  111 + value = "INVOKE",
  112 + target = "Lnet/minecraft/client/Minecraft;clickMouse()V"
  113 + ))
  114 + private void onClickMouse(Minecraft self)
  115 + {
  116 + if (ClientProxy.onClickMouse(self.thePlayer, MouseButton.LEFT))
  117 + {
  118 + this.clickMouse();
  119 + }
  120 + }
  121 +
  122 + @Inject(method = "sendClickBlockToController(Z)V", at = @At(
  123 + value = "FIELD",
  124 + target = "Lnet/minecraft/client/Minecraft;objectMouseOver:Lnet/minecraft/util/math/RayTraceResult;",
  125 + ordinal = 0
  126 + ),
  127 + cancellable = true
  128 + )
  129 + private void onMouseHeld(boolean leftClick, CallbackInfo ci)
  130 + {
  131 + if (!ClientProxy.onMouseHeld(((Minecraft)(Object)this).thePlayer, MouseButton.LEFT))
  132 + {
  133 + ci.cancel();
  134 + }
  135 + }
  136 +
  137 + @Redirect(method = "processKeyBinds()V", at = @At(
  138 + value = "INVOKE",
  139 + target = "Lnet/minecraft/client/Minecraft;rightClickMouse()V",
  140 + ordinal = 0
  141 + ))
  142 + private void onRightClickMouse(Minecraft self)
  143 + {
  144 + if (ClientProxy.onClickMouse(self.thePlayer, MouseButton.RIGHT))
  145 + {
  146 + this.rightClickMouse();
  147 + }
  148 + }
  149 +
  150 + @Redirect(method = "processKeyBinds()V", at = @At(
  151 + value = "INVOKE",
  152 + target = "Lnet/minecraft/client/Minecraft;rightClickMouse()V",
  153 + ordinal = 1
  154 + ))
  155 + private void onRightMouseHeld(Minecraft self)
  156 + {
  157 + if (ClientProxy.onMouseHeld(self.thePlayer, MouseButton.RIGHT))
  158 + {
  159 + this.rightClickMouse();
  160 + }
  161 + }
  162 +
  163 + @Redirect(method = "processKeyBinds()V", at = @At(
  164 + value = "INVOKE",
  165 + target = "Lnet/minecraft/client/Minecraft;middleClickMouse()V"
  166 + ))
  167 + private void onMiddleClickMouse(Minecraft self)
  168 + {
  169 + if (ClientProxy.onClickMouse(self.thePlayer, MouseButton.MIDDLE))
  170 + {
  171 + this.middleClickMouse();
  172 + }
  173 + }
105 174
106 @Override 175 @Override
107 public Timer getTimer() 176 public Timer getTimer()
src/main/java/com/mumfrey/liteloader/PlayerClickListener.java 0 → 100644
  1 +/*
  2 + * This file is part of LiteLoader.
  3 + * Copyright (C) 2012-16 Adam Mummery-Smith
  4 + * All Rights Reserved.
  5 + */
  6 +package com.mumfrey.liteloader;
  7 +
  8 +import com.mumfrey.liteloader.PlayerInteractionListener.MouseButton;
  9 +
  10 +import net.minecraft.client.entity.EntityPlayerSP;
  11 +
  12 +/**
  13 + * Interface for mods which want to listen for and intercept mouse events on the
  14 + * client side <em>before</em> they trigger remote actions, or otherwise just
  15 + * be notified that a click is about to be dispatched.
  16 + *
  17 + * @author Adam Mummery-Smith
  18 + */
  19 +public interface PlayerClickListener extends LiteMod
  20 +{
  21 + /**
  22 + * For left, middle and right clicks. Called when the click event is
  23 + * processed, before any handling has been done. Note that the context of
  24 + * the click can be obtained from:
  25 + *
  26 + * <ul>
  27 + * <li><tt>Minecraft.objectMouseOver</tt> the object currently under
  28 + * cursor.</li>
  29 + * <li><tt>EntityUtilities.rayTraceFromEntity</tt> using the supplied
  30 + * player is useful to determine objects under the cursor at longer
  31 + * distances than the player's reach.</li>
  32 + * <li><tt>player.isRowingBoat()</tt> is used to inhibit a lot of vanilla
  33 + * click behaviour and is worth checking state if acting upon clicks.
  34 + * </li>
  35 + * </ul>
  36 + *
  37 + * @param player The local player
  38 + * @param button The mouse button which was clicked
  39 + * @return true to allow the click to be processed normally, false to
  40 + * inhibit further processing of the click. Other listeners will still
  41 + * be notified.
  42 + */
  43 + public abstract boolean onMouseClicked(EntityPlayerSP player, MouseButton button);
  44 +
  45 + /**
  46 + * For left and right clicks only, when the player holds the key down the
  47 + * game periodically processes additional events (eg. mining), this event is
  48 + * raised after the initial click when the mouse button is held down.
  49 + *
  50 + * @param player The local player
  51 + * @param button The mouse button being held, only valid for LEFT and RIGHT
  52 + * @return true to allow the button held event to be processed normally,
  53 + * false to inhibit further processing of the click. Other listeners
  54 + * will still be notified.
  55 + */
  56 + public abstract boolean onMouseHeld(EntityPlayerSP player, MouseButton button);
  57 +}
src/main/java/com/mumfrey/liteloader/PlayerInteractionListener.java
@@ -14,7 +14,9 @@ import net.minecraft.util.math.RayTraceResult.Type; @@ -14,7 +14,9 @@ import net.minecraft.util.math.RayTraceResult.Type;
14 14
15 /** 15 /**
16 * Interface for mods which want to observe the player's "interaction" status 16 * Interface for mods which want to observe the player's "interaction" status
17 - * (player mouse clicks), allows block interaction events to be cancelled. 17 + * (player mouse clicks), allows block interaction events to be cancelled. This
  18 + * listener handles clicks on the server side (integrated server in single
  19 + * player). For client-side interaction events use {@link PlayerClickListener}.
18 * 20 *
19 * @author Adam Mummery-Smith 21 * @author Adam Mummery-Smith
20 */ 22 */
@@ -26,7 +28,8 @@ public interface PlayerInteractionListener extends LiteMod @@ -26,7 +28,8 @@ public interface PlayerInteractionListener extends LiteMod
26 public static enum MouseButton 28 public static enum MouseButton
27 { 29 {
28 LEFT, 30 LEFT,
29 - RIGHT 31 + RIGHT,
  32 + MIDDLE
30 } 33 }
31 34
32 /** 35 /**