Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * * Copyright 2019-2020 github.com/ReflxctionDev
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package io.github.reflxction.chatbot.bridge;
- import com.github.steveice10.mc.auth.exception.request.RequestException;
- import com.github.steveice10.mc.protocol.MinecraftConstants;
- import com.github.steveice10.mc.protocol.MinecraftProtocol;
- import com.github.steveice10.mc.protocol.data.message.Message;
- import com.github.steveice10.mc.protocol.packet.ingame.client.ClientChatPacket;
- import com.github.steveice10.mc.protocol.packet.ingame.server.ServerChatPacket;
- import com.github.steveice10.mc.protocol.packet.ingame.server.ServerJoinGamePacket;
- import com.github.steveice10.packetlib.Client;
- import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
- import com.github.steveice10.packetlib.event.session.PacketReceivedEvent;
- import com.github.steveice10.packetlib.event.session.SessionAdapter;
- import com.github.steveice10.packetlib.tcp.TcpSessionFactory;
- import io.github.reflxction.chatbot.ChatBotPlugin;
- import io.github.reflxction.chatbot.PluginSettings;
- import net.dv8tion.jda.api.JDA;
- import net.dv8tion.jda.api.entities.TextChannel;
- import org.apache.commons.lang.StringUtils;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.jetbrains.annotations.NotNull;
- import org.jetbrains.annotations.Nullable;
- import java.net.Proxy;
- import static io.github.reflxction.chatbot.PluginSettings.ACCOUNT_MAIL;
- import static io.github.reflxction.chatbot.PluginSettings.ACCOUNT_PASSWORD;
- public class MinecraftBridge implements ChatBridge {
- private static final Proxy PROXY = Proxy.NO_PROXY;
- private static final Proxy AUTH_PROXY = Proxy.NO_PROXY;
- private static final String IP_ADDRESS = "play.hypixel.net";
- private static final int PORT = 25565;
- private static Client CLIENT = connect();
- private static MinecraftProtocol protocol;
- /**
- * Invoked when a message is received, and sends it to the appropriate channel
- *
- * @param sender Sender of the message
- * @param message Message that was sent
- * @param channel Discord channel
- * @param bot JDA instance
- */
- @Override
- public void onReceived(String sender, String message, @Nullable TextChannel channel, @Nullable JDA bot) {
- CLIENT.getSession().send(new ClientChatPacket("/gc [Discord] " + sender + ": " + message));
- }
- /**
- * Invoked when a message has been sent using this bridge
- *
- * @param sender Sender of this message
- * @param message Message that was sent
- * @param channel Discord channel
- * @param bot JDA instance
- */
- @Override
- public void onSent(String sender, String message, @Nullable TextChannel channel, @Nullable JDA bot) {
- if (sender.contains(protocol.getProfile().getName())) return;
- DISCORD.onReceived(sender, message, channel, bot);
- }
- /**
- * Invoked when this bridge logs in to its services
- */
- public static void onBotLogin() {
- CLIENT.getSession().addListener(new SessionAdapter() {
- @Override
- public void packetReceived(PacketReceivedEvent event) {
- if (event.getPacket() instanceof ServerJoinGamePacket) {
- ChatBotPlugin.getPlugin().getLogger().info("Successfully connected to Hypixel");
- } else if (event.getPacket() instanceof ServerChatPacket) {
- Message m = event.<ServerChatPacket>getPacket().getMessage();
- String text = ChatColor.stripColor(m.getFullText());
- if (!text.startsWith("Guild > ")) return;
- String[] parts = text.split(":", 2);
- String sender = StringUtils.replaceOnce(parts[0], "Guild > ", "");
- String message = parts[1];
- TextChannel channel = (TextChannel) ChatBotPlugin.getBot().getGuildChannelById(PluginSettings.GUILD_CHANNEL.get());
- ChatBridge.MINECRAFT.onSent(sender, message, channel, ChatBotPlugin.getBot());
- }
- }
- @Override
- public void disconnected(DisconnectedEvent event) {
- ChatBotPlugin.getPlugin().getLogger().info("Disconnected: " + Message.fromString(event.getReason()).getFullText());
- if (event.getCause() != null) {
- event.getCause().printStackTrace();
- }
- ChatBotPlugin.getPlugin().getLogger().info("Reconnecting in 10 seconds.");
- Bukkit.getScheduler().runTaskLater(ChatBotPlugin.getPlugin(), MinecraftBridge::connectToHypixel, 10 * 20);
- }
- });
- }
- /**
- * Invoked when this bridge logs in to its services
- */
- public static void connectToHypixel() {
- CLIENT.getSession().connect();
- Bukkit.getScheduler().runTaskLater(ChatBotPlugin.getPlugin(), () -> CLIENT.getSession().send(new ClientChatPacket("/ac ยง")), 4 * 20);
- }
- @NotNull
- private static Client connect() {
- try {
- protocol = new MinecraftProtocol(ACCOUNT_MAIL.get(), ACCOUNT_PASSWORD.get());
- System.out.println("Successfully authenticated user.");
- } catch (RequestException e) {
- throw new RuntimeException(e);
- }
- Client client = new Client(IP_ADDRESS, PORT, protocol, new TcpSessionFactory(PROXY));
- client.getSession().setFlag(MinecraftConstants.AUTH_PROXY_KEY, AUTH_PROXY);
- return CLIENT = client;
- }
- public static void load() {
- }
- static {
- onBotLogin();
- connectToHypixel();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement