Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
925
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. /*
  2. * * Copyright 2019-2020 github.com/ReflxctionDev
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package io.github.reflxction.chatbot.bridge;
  17.  
  18. import com.github.steveice10.mc.auth.exception.request.RequestException;
  19. import com.github.steveice10.mc.protocol.MinecraftConstants;
  20. import com.github.steveice10.mc.protocol.MinecraftProtocol;
  21. import com.github.steveice10.mc.protocol.data.message.Message;
  22. import com.github.steveice10.mc.protocol.packet.ingame.client.ClientChatPacket;
  23. import com.github.steveice10.mc.protocol.packet.ingame.server.ServerChatPacket;
  24. import com.github.steveice10.mc.protocol.packet.ingame.server.ServerJoinGamePacket;
  25. import com.github.steveice10.packetlib.Client;
  26. import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
  27. import com.github.steveice10.packetlib.event.session.PacketReceivedEvent;
  28. import com.github.steveice10.packetlib.event.session.SessionAdapter;
  29. import com.github.steveice10.packetlib.tcp.TcpSessionFactory;
  30. import io.github.reflxction.chatbot.ChatBotPlugin;
  31. import io.github.reflxction.chatbot.PluginSettings;
  32. import net.dv8tion.jda.api.JDA;
  33. import net.dv8tion.jda.api.entities.TextChannel;
  34. import org.apache.commons.lang.StringUtils;
  35. import org.bukkit.Bukkit;
  36. import org.bukkit.ChatColor;
  37. import org.jetbrains.annotations.NotNull;
  38. import org.jetbrains.annotations.Nullable;
  39.  
  40. import java.net.Proxy;
  41.  
  42. import static io.github.reflxction.chatbot.PluginSettings.ACCOUNT_MAIL;
  43. import static io.github.reflxction.chatbot.PluginSettings.ACCOUNT_PASSWORD;
  44.  
  45. public class MinecraftBridge implements ChatBridge {
  46.  
  47. private static final Proxy PROXY = Proxy.NO_PROXY;
  48. private static final Proxy AUTH_PROXY = Proxy.NO_PROXY;
  49.  
  50. private static final String IP_ADDRESS = "play.hypixel.net";
  51. private static final int PORT = 25565;
  52.  
  53. private static Client CLIENT = connect();
  54.  
  55. private static MinecraftProtocol protocol;
  56.  
  57. /**
  58. * Invoked when a message is received, and sends it to the appropriate channel
  59. *
  60. * @param sender Sender of the message
  61. * @param message Message that was sent
  62. * @param channel Discord channel
  63. * @param bot JDA instance
  64. */
  65. @Override
  66. public void onReceived(String sender, String message, @Nullable TextChannel channel, @Nullable JDA bot) {
  67. CLIENT.getSession().send(new ClientChatPacket("/gc [Discord] " + sender + ": " + message));
  68. }
  69.  
  70. /**
  71. * Invoked when a message has been sent using this bridge
  72. *
  73. * @param sender Sender of this message
  74. * @param message Message that was sent
  75. * @param channel Discord channel
  76. * @param bot JDA instance
  77. */
  78. @Override
  79. public void onSent(String sender, String message, @Nullable TextChannel channel, @Nullable JDA bot) {
  80. if (sender.contains(protocol.getProfile().getName())) return;
  81. DISCORD.onReceived(sender, message, channel, bot);
  82. }
  83.  
  84. /**
  85. * Invoked when this bridge logs in to its services
  86. */
  87. public static void onBotLogin() {
  88. CLIENT.getSession().addListener(new SessionAdapter() {
  89. @Override
  90. public void packetReceived(PacketReceivedEvent event) {
  91. if (event.getPacket() instanceof ServerJoinGamePacket) {
  92. ChatBotPlugin.getPlugin().getLogger().info("Successfully connected to Hypixel");
  93. } else if (event.getPacket() instanceof ServerChatPacket) {
  94. Message m = event.<ServerChatPacket>getPacket().getMessage();
  95. String text = ChatColor.stripColor(m.getFullText());
  96. if (!text.startsWith("Guild > ")) return;
  97. String[] parts = text.split(":", 2);
  98. String sender = StringUtils.replaceOnce(parts[0], "Guild > ", "");
  99. String message = parts[1];
  100. TextChannel channel = (TextChannel) ChatBotPlugin.getBot().getGuildChannelById(PluginSettings.GUILD_CHANNEL.get());
  101. ChatBridge.MINECRAFT.onSent(sender, message, channel, ChatBotPlugin.getBot());
  102. }
  103. }
  104.  
  105. @Override
  106. public void disconnected(DisconnectedEvent event) {
  107. ChatBotPlugin.getPlugin().getLogger().info("Disconnected: " + Message.fromString(event.getReason()).getFullText());
  108. if (event.getCause() != null) {
  109. event.getCause().printStackTrace();
  110. }
  111. ChatBotPlugin.getPlugin().getLogger().info("Reconnecting in 10 seconds.");
  112. Bukkit.getScheduler().runTaskLater(ChatBotPlugin.getPlugin(), MinecraftBridge::connectToHypixel, 10 * 20);
  113. }
  114. });
  115. }
  116.  
  117. /**
  118. * Invoked when this bridge logs in to its services
  119. */
  120. public static void connectToHypixel() {
  121. CLIENT.getSession().connect();
  122. Bukkit.getScheduler().runTaskLater(ChatBotPlugin.getPlugin(), () -> CLIENT.getSession().send(new ClientChatPacket("/ac ยง")), 4 * 20);
  123. }
  124.  
  125. @NotNull
  126. private static Client connect() {
  127. try {
  128. protocol = new MinecraftProtocol(ACCOUNT_MAIL.get(), ACCOUNT_PASSWORD.get());
  129. System.out.println("Successfully authenticated user.");
  130. } catch (RequestException e) {
  131. throw new RuntimeException(e);
  132. }
  133.  
  134. Client client = new Client(IP_ADDRESS, PORT, protocol, new TcpSessionFactory(PROXY));
  135. client.getSession().setFlag(MinecraftConstants.AUTH_PROXY_KEY, AUTH_PROXY);
  136. return CLIENT = client;
  137. }
  138.  
  139. public static void load() {
  140. }
  141.  
  142. static {
  143. onBotLogin();
  144. connectToHypixel();
  145. }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement