From 21ea7ff70a676f2acde63ea1fe9d8ebde35760b5 Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Tue, 11 Dec 2018 22:42:00 +0100 Subject: [PATCH] use this in your extensions its cool --- .../extensions/happyspeech/HappySpeech.java | 2 +- .../extensions/extra/harble/ChatConsole.java | 123 ++++++++++++++++++ .../{hashing => harble}/HashSupport.java | 2 +- 3 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 G-Earth/src/main/java/gearth/extensions/extra/harble/ChatConsole.java rename G-Earth/src/main/java/gearth/extensions/extra/{hashing => harble}/HashSupport.java (99%) diff --git a/Extensions/HappySpeech/src/main/java/extensions/happyspeech/HappySpeech.java b/Extensions/HappySpeech/src/main/java/extensions/happyspeech/HappySpeech.java index b23ba59..730dbd4 100644 --- a/Extensions/HappySpeech/src/main/java/extensions/happyspeech/HappySpeech.java +++ b/Extensions/HappySpeech/src/main/java/extensions/happyspeech/HappySpeech.java @@ -2,7 +2,7 @@ package extensions.happyspeech; import gearth.extensions.Extension; import gearth.extensions.ExtensionInfo; -import gearth.extensions.extra.hashing.HashSupport; +import gearth.extensions.extra.harble.HashSupport; import gearth.protocol.HMessage; import gearth.protocol.HPacket; diff --git a/G-Earth/src/main/java/gearth/extensions/extra/harble/ChatConsole.java b/G-Earth/src/main/java/gearth/extensions/extra/harble/ChatConsole.java new file mode 100644 index 0000000..d07e9de --- /dev/null +++ b/G-Earth/src/main/java/gearth/extensions/extra/harble/ChatConsole.java @@ -0,0 +1,123 @@ +package gearth.extensions.extra.harble; + +import gearth.extensions.ExtensionInfo; +import gearth.extensions.IExtension; +import gearth.protocol.HMessage; +import gearth.protocol.HPacket; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Jonas on 3/12/2018. + */ + +/** + * Must be created in initextension + */ +public class ChatConsole { + + private volatile int chatid; + private volatile String name; + private volatile HashSupport hashSupport; + private volatile String infoMessage; + + private volatile boolean firstTime = true; + + + public ChatConsole(final HashSupport hashSupport, IExtension extension) { + this(hashSupport, extension, null); + } + + /** + * infomessage will be used as response for :info and for initialize + * @param hashSupport + * @param extension + * @param infoMessage + */ + public ChatConsole(final HashSupport hashSupport, IExtension extension, String infoMessage) { + this.hashSupport = hashSupport; + this.name = extension.getClass().getAnnotation(ExtensionInfo.class).Title(); + chatid = this.name.hashCode(); + this.infoMessage = infoMessage; + + final boolean[] doOncePerConnection = {false}; + + extension.onConnect((s, i, s1) -> doOncePerConnection[0] = true); + + extension.intercept(HMessage.Side.TOSERVER, hMessage -> { + // if the first packet on init is not 4000, the extension was already running, so we open the chat instantly + if (firstTime) { + firstTime = false; + if (hMessage.getPacket().headerId() != 4000) { + doOncePerConnection[0] = false; + createChat(); + } + } + }); + + hashSupport.intercept(HMessage.Side.TOCLIENT, "Friends", hMessage -> { + if (doOncePerConnection[0]) { + doOncePerConnection[0] = false; + + new Thread(() -> { + try { + Thread.sleep(1000); + createChat(); + + } catch (InterruptedException e) { + e.printStackTrace(); + } + }).start(); + } + }); + + hashSupport.intercept(HMessage.Side.TOSERVER, "FriendPrivateMessage", hMessage -> { + HPacket packet = hMessage.getPacket(); + if (packet.readInteger() == chatid) { + hMessage.setBlocked(true); + String str = packet.readString(); + if (str.equals(":info") && infoMessage != null) { + writeOutput(infoMessage, false); + } + else { + notifyChatInputListeners(str); + } + } + }); + } + + private void createChat() { + hashSupport.sendToClient("UpdateFriend", + 0, 1, false, false, "", chatid, " [G-Earth] - " + name, 1, true, false, "", 0, "", 0, true, true, true, "" + ); + + if (infoMessage != null) { + writeOutput(infoMessage, false); + } + } + + public void writeOutput(String string, boolean asInvite) { + if (asInvite) { + hashSupport.sendToClient("ReceiveInvitation", chatid, string); + } + else { + hashSupport.sendToClient("ReceivePrivateMessage", chatid, string, 0, ""); + } + } + + public interface ChatInputListener { + void inputEntered(String input); + } + private List chatInputListenerList = new ArrayList(); + public void onInput(ChatInputListener listener) { + chatInputListenerList.add(listener); + } + private void notifyChatInputListeners (String s) { + for (ChatInputListener listener : chatInputListenerList) { + listener.inputEntered(s); + } + } + + +} diff --git a/G-Earth/src/main/java/gearth/extensions/extra/hashing/HashSupport.java b/G-Earth/src/main/java/gearth/extensions/extra/harble/HashSupport.java similarity index 99% rename from G-Earth/src/main/java/gearth/extensions/extra/hashing/HashSupport.java rename to G-Earth/src/main/java/gearth/extensions/extra/harble/HashSupport.java index b398ba7..ea2c7c0 100644 --- a/G-Earth/src/main/java/gearth/extensions/extra/hashing/HashSupport.java +++ b/G-Earth/src/main/java/gearth/extensions/extra/harble/HashSupport.java @@ -1,4 +1,4 @@ -package gearth.extensions.extra.hashing; +package gearth.extensions.extra.harble; import gearth.Main; import gearth.extensions.Extension;