-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLiteModWatson.java
569 lines (521 loc) · 18.5 KB
/
LiteModWatson.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
package watson;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.PositionedSoundRecord;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.multiplayer.ServerData;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.network.INetHandler;
import net.minecraft.network.play.client.C01PacketChatMessage;
import net.minecraft.network.play.server.S01PacketJoinGame;
import net.minecraft.util.IChatComponent;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import watson.chat.Chat;
import watson.chat.ChatProcessor;
import watson.cli.ClientCommandManager;
import watson.db.BlockEditSet;
import watson.debug.Log;
import watson.gui.ModifiedKeyBinding;
import watson.gui.MouseButton;
import watson.gui.WatsonConfigPanel;
import com.google.gson.Gson;
import com.mojang.realmsclient.dto.RealmsServer;
import com.mumfrey.liteloader.ChatFilter;
import com.mumfrey.liteloader.Configurable;
import com.mumfrey.liteloader.JoinGameListener;
import com.mumfrey.liteloader.OutboundChatFilter;
import com.mumfrey.liteloader.PostRenderListener;
import com.mumfrey.liteloader.Tickable;
import com.mumfrey.liteloader.core.LiteLoaderEventBroker;
import com.mumfrey.liteloader.modconfig.ConfigPanel;
import com.mumfrey.liteloader.modconfig.ConfigStrategy;
import com.mumfrey.liteloader.modconfig.ExposableOptions;
import com.mumfrey.liteloader.transformers.event.EventInfo;
// ----------------------------------------------------------------------------
/**
* Main LiteMod entry point and event handler.
*
* @author totemo
*/
@ExposableOptions(strategy = ConfigStrategy.Versioned, filename = "watson.json")
public class LiteModWatson implements JoinGameListener, ChatFilter, Tickable,
PostRenderListener, OutboundChatFilter, Configurable
{
// --------------------------------------------------------------------------
/**
* Default constructor.
*/
public LiteModWatson()
{
}
// --------------------------------------------------------------------------
/**
* getName() should be used to return the display name of your mod and MUST
* NOT return null
*
* @see com.mumfrey.liteloader.LiteMod#getName()
*/
@Override
public String getName()
{
return "Watson";
}
// --------------------------------------------------------------------------
/**
* Read the mod version from the metadata.
*
* Also works in the Eclipse run configuration if the Ant build runs at least
* once to update res/litemod.json from build/litemod.template.json.
*
* @see com.mumfrey.liteloader.LiteMod#getVersion()
*/
@Override
public String getVersion()
{
InputStream is = null;
try
{
Gson gson = new Gson();
is = getLiteModJsonStream();
@SuppressWarnings("unchecked")
Map<String, String> meta = gson.fromJson(new InputStreamReader(is),
HashMap.class);
String version = meta.get("version");
if (version == null)
{
version = "(missing version info)";
}
return version;
}
catch (Exception ex)
{
return "(error loading version)";
}
finally
{
if (is != null)
{
try
{
is.close();
}
catch (Exception ex)
{
}
}
}
} // getVersion
// --------------------------------------------------------------------------
/**
* init() is called very early in the initialisation cycle, before the game is
* fully initialised, this means that it is important that your mod does not
* interact with the game in any way at this point.
*
* @see com.mumfrey.liteloader.LiteMod#init(java.io.File)
*/
@Override
public void init(File configPath)
{
Configuration.instance.load();
Log.info("Loading Watson version " + getVersion());
Controller.instance.initialise();
}
// --------------------------------------------------------------------------
/**
* upgradeSettings is used to notify a mod that its version-specific settings
* are being migrated
*
* @see com.mumfrey.liteloader.LiteMod#upgradeSettings(java.lang.String,
* java.io.File, java.io.File)
*/
@Override
public void upgradeSettings(String version, File configPath, File oldConfigPath)
{
}
// --------------------------------------------------------------------------
/**
* Perform actions triggered on initial join.
*
* @see com.mumfrey.liteloader.JoinGameListener#onJoinGame(net.minecraft.network.INetHandler,
* net.minecraft.network.play.server.S01PacketJoinGame,
* net.minecraft.client.multiplayer.ServerData,
* com.mojang.realmsclient.dto.RealmsServer)
*/
@Override
public void onJoinGame(INetHandler netHandler, S01PacketJoinGame joinGamePacket, ServerData serverData,
RealmsServer realmsServer)
{
if (Configuration.instance.isEnabled())
{
_gameJoinTime = System.currentTimeMillis();
// Only set display settings on first connect. Subsequent connects
// should retain the previous display state.
Minecraft mc = Minecraft.getMinecraft();
Controller.instance.getDisplaySettings().configure(Controller.instance.getServerIP(),
mc.theWorld.getWorldInfo().getGameType());
}
}
// --------------------------------------------------------------------------
/**
* @see com.mumfrey.liteloader.ChatFilter#onChat(net.minecraft.util.IChatComponent,
* java.lang.String,
* com.mumfrey.liteloader.core.LiteLoaderEventBroker.ReturnValue)
*/
@Override
public boolean onChat(IChatComponent chat, String message, LiteLoaderEventBroker.ReturnValue<IChatComponent> newMessage)
{
boolean allowChat = ChatProcessor.instance.onChat(chat);
if (allowChat)
{
newMessage.set(Chat.getChatHighlighter().highlight(chat));
}
return allowChat;
}
// --------------------------------------------------------------------------
/**
* @see com.mumfrey.liteloader.Tickable#onTick(net.minecraft.client.Minecraft,
* float, boolean, boolean)
*/
@Override
public void onTick(Minecraft minecraft, float partialTicks, boolean inGame, boolean clock)
{
fixStuckKeys();
// Check for the screenshot key, even if there is a GuiScreen up that would
// ordinarily swall the key events. We need to screenie chat.
if (Configuration.instance.KEYBIND_SCREENSHOT.isHeld())
{
Configuration.instance.KEYBIND_SCREENSHOT.perform();
}
SyncTaskQueue.instance.runTasks();
Controller.instance.processServerChatQueue();
// With Forge, onJoinGame() gets called before the chat GUI is ready to
// display the welcome message.
if (_gameJoinTime != 0 &&
System.currentTimeMillis() - _gameJoinTime > 1000 &&
Chat.isChatGuiReady())
{
Chat.localOutput(String.format(Locale.US, "Watson %s. Type /w help, for help.", getVersion()));
_gameJoinTime = 0;
}
} // onTick
// --------------------------------------------------------------------------
/**
* @see com.mumfrey.liteloader.PostRenderListener#onPostRenderEntities(float)
*/
@Override
public void onPostRenderEntities(float partialTicks)
{
if (Configuration.instance.isEnabled()
&& Controller.instance.getDisplaySettings().isDisplayed())
{
RenderHelper.disableStandardItemLighting();
OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, 240, 240);
GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GlStateManager.enableBlend();
GlStateManager.disableTexture2D();
GlStateManager.disableLighting();
GlStateManager.depthMask(false);
GlStateManager.disableDepth();
boolean foggy = GL11.glIsEnabled(GL11.GL_FOG);
GlStateManager.disableFog();
GlStateManager.pushMatrix();
GlStateManager.translate(-getPlayerX(partialTicks),
-getPlayerY(partialTicks),
-getPlayerZ(partialTicks));
BlockEditSet edits = Controller.instance.getBlockEditSet();
edits.drawOutlines();
edits.drawVectors();
Controller.instance.drawSelection();
// Test code. X marks the spot.
// GL11.glLineWidth(3.0f);
// GL11.glColor4f(1.0f, 0.0f, 0.0f, 0.8f);
// Tessellator tess = Tessellator.instance;
// tess.startDrawing(GL11.GL_LINES);
// tess.addVertex(-5, 27, -5);
// tess.addVertex(5, 27, 5);
// tess.addVertex(5, 27, -5);
// tess.addVertex(-5, 27, 5);
// tess.draw();
GlStateManager.popMatrix();
edits.drawAnnotations();
edits.getOreDB().drawDepositLabels();
// More test code.
// drawBillboard(0, 70, 0, 0x80000000, 0xFFFFFFFF, 0.02,
// "Test Billboard");
// Only re-enable fog if it was enabled before we messed with it.
// Or else, fog is *all* you'll see with Optifine.
if (foggy)
{
GlStateManager.enableFog();
}
GlStateManager.enableDepth();
GlStateManager.depthMask(true);
GlStateManager.enableLighting();
GlStateManager.enableTexture2D();
GlStateManager.disableBlend();
RenderHelper.enableStandardItemLighting();
}
} // onPostRenderEntities
// --------------------------------------------------------------------------
/**
* @see com.mumfrey.liteloader.PostRenderListener#onPostRender(float)
*/
@Override
public void onPostRender(float partialTicks)
{
}
// --------------------------------------------------------------------------
private double getPlayerX(float partialTicks)
{
EntityPlayerSP p = Minecraft.getMinecraft().thePlayer;
return p.prevPosX + (p.posX - p.prevPosX) * partialTicks;
}
// --------------------------------------------------------------------------
private double getPlayerY(float partialTicks)
{
EntityPlayerSP p = Minecraft.getMinecraft().thePlayer;
return p.prevPosY + (p.posY - p.prevPosY) * partialTicks;
}
// --------------------------------------------------------------------------
private double getPlayerZ(float partialTicks)
{
EntityPlayerSP p = Minecraft.getMinecraft().thePlayer;
return p.prevPosZ + (p.posZ - p.prevPosZ) * partialTicks;
}
// --------------------------------------------------------------------------
/**
* @see com.mumfrey.liteloader.OutboundChatFilter#onSendChatMessage(java.lang.String)
*/
@Override
public boolean onSendChatMessage(String chat)
{
// Send the chat to server if not handled locally as a command.
return !ClientCommandManager.instance.handleClientCommand(chat);
}
// --------------------------------------------------------------------------
/**
* This method acts as an entry point into
* ClientCommandManager.handleClientCommand(), for use by the watson_macros
* mod.
*
* The method was originally a callback used by a transformer, but that is no
* longer the case.
*/
public static void sendChatMessage(EntityPlayerSP player, String chat)
{
if (!ClientCommandManager.instance.handleClientCommand(chat))
{
player.sendQueue.addToSendQueue(new C01PacketChatMessage(chat));
}
}
// --------------------------------------------------------------------------
/**
* @see com.mumfrey.liteloader.Configurable#getConfigPanelClass()
*/
@Override
public Class<? extends ConfigPanel> getConfigPanelClass()
{
return WatsonConfigPanel.class;
}
// --------------------------------------------------------------------------
/**
* Return an InputStream that reads "/litemod.json".
*
* When running under the IDE, that's easy because the file is copied to the
* res/ directory and getResourceAsStream() can access it directly. When
* running as an installed mod file, getResourceAsStream() may return a
* reference to the litemod.json file for another mod, depending on the order
* of the mods in the ClassLoader. In that circumstance, we use a specially
* crafted URL that references litemod.json via the URL of the .litemod (JAR)
* file.
*
* @return the InputStream, or null on failure.
*/
private InputStream getLiteModJsonStream()
{
String classURL = getClass().getResource("/" + getClass().getName().replace('.', '/') + ".class").toString();
if (classURL.contains("!"))
{
String jarURL = classURL.substring(0, classURL.indexOf('!'));
try
{
URL resourceURL = new URL(jarURL + "!/litemod.json");
return resourceURL.openStream();
}
catch (IOException ex)
{
}
return null;
}
else
{
// No JAR. Running under the IDE.
return getClass().getResourceAsStream("/litemod.json");
}
} // getLiteModJsonStream
// --------------------------------------------------------------------------
/**
* This EventInjectionTransformer listener for KeyBinding.onTick() intercepts
* key events and mouse clicks and checks for ModifiedKeyBinding activations.
*/
public static void onKeyBindingOnTick(EventInfo<?> event, int keyCode)
{
ModifiedKeyBinding binding = getActivatedKeyBinding(keyCode);
if (binding != null)
{
// If Watson should handle the key, then cancel Minecraft's normal
// handling of that key (which may be bound).
event.cancel();
}
}
// --------------------------------------------------------------------------
/**
* This EventInjectionTransformer listener for KeyBinding.setKeyBindState()
* intercepts key events and mouse clicks and checks for ModifiedKeyBinding
* activations.
*/
public static void onKeyBindingSetKeyBindState(EventInfo<?> event, int
keyCode, boolean down)
{
ModifiedKeyBinding binding = getActivatedKeyBinding(keyCode);
if (down && binding != null)
{
// If Watson should handle the key, then cancel Minecraft's normal
// handling of that key (which may be bound).
performKeyBinding(binding);
event.cancel();
}
}
// --------------------------------------------------------------------------
/**
* This EventInjectionTransformer listener for
* InventoryPlayer.changeCurrentItem() intercepts mouse scrolls events and
* checks for ModifiedKeyBinding activations.
*/
public static void onInventoryPlayerChangeCurrentItem(EventInfo<InventoryPlayer> event, int wheelDelta)
{
int keyCode = (wheelDelta < 0) ? MouseButton.SCROLL_DOWN.getCode()
: (wheelDelta > 0) ? keyCode = MouseButton.SCROLL_UP.getCode() : 0;
if (keyCode != 0)
{
ModifiedKeyBinding binding = getActivatedKeyBinding(keyCode);
if (binding != null)
{
performKeyBinding(binding);
event.cancel();
}
}
} // onInventoryPlayerChangeCurrentItem
// --------------------------------------------------------------------------
/**
* Play a button press sound and call the {@link ActionHandler} for a
* {@link ModifiedKeyBinding}.
*/
protected static void performKeyBinding(ModifiedKeyBinding binding)
{
Minecraft mc = Minecraft.getMinecraft();
mc.getSoundHandler().playSound(PositionedSoundRecord.create(new ResourceLocation("gui.button.press"), 1.0F));
binding.perform();
}
// --------------------------------------------------------------------------
/**
* If a key binding is activated, return it.
*
* If the Watson display is disabled, key bindings that require the display to
* be visible will not be considered activated.
*
* @param keyCode the most recently pressed key.
* @return the activated key binding, or null if none is active.
*/
protected static ModifiedKeyBinding getActivatedKeyBinding(int keyCode)
{
ModifiedKeyBinding matchingKeyBinding = null;
for (ModifiedKeyBinding keyBinding : Configuration.instance.getAllModifiedKeyBindings())
{
// SKip over the screenshot keybinding, since we check it in onTick() so
// that we can take screenshots even with the chat GUI up.
if (keyBinding == Configuration.instance.KEYBIND_SCREENSHOT)
{
continue;
}
if (keyBinding.isActivated(keyCode))
{
matchingKeyBinding = keyBinding;
break;
}
}
// Don't match display-dependent key bindings when the display is disabled.
if (matchingKeyBinding != null && matchingKeyBinding.isDisplayDependent() &&
!Controller.instance.getDisplaySettings().isDisplayed())
{
matchingKeyBinding = null;
}
return matchingKeyBinding;
}
// --------------------------------------------------------------------------
/**
* This method is called on every tick to check whether Minecraft has lost
* keyboard focus.
*
* When the user switches away from Minecraft, e.g. using Alt-Tab, LWJGL never
* receives the key up event on those keys and the LWJGL Keyboard class
* considers them to be stuck down.
*
* The fix, found at http://forum.lwjgl.org/index.php?topic=4517.0 is to
* destroy() and re-create() the Keyboard. Minecraft KeyBindings must also be
* un-pressed.
*/
protected void fixStuckKeys()
{
if (Display.isActive())
{
// When focus is regained...
if (_focusWasLost)
{
// Reset Minecraft KeyBindings and re-create the Keyboard from scratch.
KeyBinding.unPressAllKeys();
Keyboard.destroy();
try
{
Keyboard.create();
}
catch (LWJGLException ex)
{
Log.exception(Level.SEVERE, "Exception fixing stuck keys", ex);
}
}
_focusWasLost = false;
}
else
{
_focusWasLost = true;
}
} // fixStuckKeys
// --------------------------------------------------------------------------
/**
* Set, upon joining the game, to the current time to trigger the welcome
* message a second later. When 0, no welcome message is shown.
*/
protected static long _gameJoinTime = 0;
/**
* This flag is set to true to record the event of Minecraft having lost
* keyboard focus.
*/
protected boolean _focusWasLost = false;
} // class LiteModWatson