move java files to src\main\java

also resolves a few issues regarding the Java
environment
This commit is contained in:
nzBryce101
2024-04-05 14:07:10 +13:00
parent 97a0631b7c
commit 254b7f9a99
12 changed files with 69 additions and 2 deletions

BIN
src/main/java/MCbot.jar Normal file

Binary file not shown.

117
src/main/java/MCbot.java Normal file
View File

@@ -0,0 +1,117 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.Statistic;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class MCbot extends JavaPlugin {
@Override
public void onEnable() {
getLogger().info("MCbot enabled");
// Start your socket server here
startSocketServer();
}
@Override
public void onDisable() {
getLogger().info("MCbot disabled");
// Stop your socket server here if needed
}
private void startSocketServer() {
// Implement your socket server logic here
// Example: Create a new thread for socket server
new Thread(() -> {
try {
try (// Create a socket server on port 8888
ServerSocket serverSocket = new ServerSocket(8888)) {
getLogger().info("Socket server started on port 8888");
while (true) {
// Accept incoming client connections
Socket clientSocket = serverSocket.accept();
getLogger().info("Client connected: " + clientSocket.getInetAddress());
// Handle client communication (e.g., read commands and send responses)
handleClient(clientSocket);
}
}
} catch (IOException e) {
getLogger().severe("Error starting socket server: " + e.getMessage());
}
}).start();
}
private void handleClient(Socket clientSocket) {
// Implement logic to handle client communication here
// Example: Read commands from client and send responses
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true);
// Read command from client
String command = reader.readLine();
getLogger().info("Received command from client: " + command);
// Process command and send response
String response = processCommand(command);
writer.println(response);
// Close client connection
clientSocket.close();
} catch (IOException e) {
getLogger().severe("Error handling client: " + e.getMessage());
}
}
private String processCommand(String command) {
// Implement logic to process commands here
// Example: If command is "!players", return player list from Minecraft server
if (command.equals("!players")) {
// Use Spigot API to get player list
String playerList = String.join(", ", getServer().getOnlinePlayers().stream()
.map(player -> player.getName())
.collect(Collectors.toList()));
return "Players online: " + playerList;
}
if (command.startsWith("!exp ")) {
// Extract player name from command
String playerName = command.substring(5); // Assuming "!exp " is 5 characters long
// Get player object by name
Player player = Bukkit.getPlayer(playerName);
if (player != null) {
// Get player's experience level
int expLevel = player.getLevel();
return playerName + "'s experience level: " + expLevel;
} else {
return "Player not found: " + playerName;
}
}
if (command.startsWith("!deaths ")) {
//Extract player name from command
String playerName = command.substring(8); // Assuming "!deaths " is at least 8 chars long
// Get Player object by name
Player player = Bukkit.getPlayer(playerName);
if(player != null) {
// Get Player's Death Count
int deaths = player.getStatistic(Statistic.DEATHS);
return playerName + "has died" + deaths + "times";
}else {
return "Player not found: " + playerName;
}
}
// Add more command processing logic as needed
return "Unknown command: " + command;
}
}

View File

@@ -0,0 +1,90 @@
import com.google.api.client.googleapis.auth.oauth2.Credential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.googleapis.http.HttpRequest;
import com.google.api.client.googleapis.http.HttpRequestInitializer;
import com.google.api.client.googleapis.json.JsonFactory;
import com.google.api.client.goolgeapis.json.jackson2.JacksonFactory;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.LiveChatMessage;
import com.google.api.services.youtube.model.LiveChatMessageListResponse;
import com.google.api.services.youtube.model.LiveChatMessageSnippet;
import com.google.api.services.youtube.model.LiveChatTextMessageDetails;
import com.google.api.services.youtube.model.LiveChatTextMessageDetails.MessageTextDetails;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.List;
public class MCchatbotyt {
private static final String APPLICATION_NAME = "MC chatbot for YT";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static YouTube youtube;
public static void main(String[] args) throws GeneralSecurityException, IOException {
// Set up credentials
Credential credential = authorize();
// Set up YouTube API client
youtube = new YouTube.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
JSON_FACTORY,
new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
request.setInterceptor(credential);
}
})
.setApplicationName(APPLICATION_NAME)
.build();
// Get live chat ID for your live stream
String liveChatId = getLiveChatId("5lAKSeFQS2M");
// Poll for new chat messages every few seconds
while (true) {
List<LiveChatMessage> messages = getLiveChatMessages(liveChatId);
for (LiveChatMessage message : messages) {
String author = message.getSnippet().getAuthorChannelId();
String text = message.getSnippet().getDisplayMessage();
System.out.println("New message from " + author + ": " + text);
// Add your logic to handle incoming messages here
}
try {
Thread.sleep(5000); // Poll every 5 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static Credential authorize() throws IOException, GeneralSecurityException {
// Load client secrets JSON file (you need to create this file with your OAuth client ID and secret)
GoogleCredential credential = GoogleCredential.fromStream(
YouTubeLiveChatBot.class.getResourceAsStream("/client_secrets.json"))
.createScoped(List.of("https://www.googleapis.com/auth/youtube"));
return credential;
}
private static String getLiveChatId(String liveStreamId) throws IOException {
YouTube.LiveChatMessages.List liveChatRequest = youtube.liveChatMessages()
.list("id,snippet")
.setLiveChatId(liveStreamId);
LiveChatMessageListResponse response = liveChatRequest.execute();
List<LiveChatMessage> messages = response.getItems();
if (messages != null && !messages.isEmpty()) {
return messages.get(0).getSnippet().getLiveChatId();
}
return null;
}
private static List<LiveChatMessage> getLiveChatMessages(String liveChatId) throws IOException {
YouTube.LiveChatMessages.List liveChatRequest = youtube.liveChatMessages()
.list("id,snippet")
.setLiveChatId(liveChatId);
LiveChatMessageListResponse response = liveChatRequest.execute();
return response.getItems();
}
}