Skip to content

Commit

Permalink
fix no response for /mc
Browse files Browse the repository at this point in the history
add /rmplugin command
  • Loading branch information
Senderman committed Nov 20, 2022
1 parent b36be5a commit a8998d9
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 44 deletions.
6 changes: 4 additions & 2 deletions src/main/java/com/senderman/telecrafter/InjectionConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.senderman.telecrafter.config.ConfigProvider;
import com.senderman.telecrafter.minecraft.EventListener;
import com.senderman.telecrafter.minecraft.PluginManager;
import com.senderman.telecrafter.minecraft.command.TgCommandExecutor;
import com.senderman.telecrafter.minecraft.provider.MinecraftProvider;
import com.senderman.telecrafter.minecraft.provider.ServerPropertiesProvider;
Expand Down Expand Up @@ -35,7 +36,7 @@ public InjectionConfig(JavaPlugin plugin, ConfigProvider config, File configFile
save(telegram);
save(minecraft);


save(new PluginManager(minecraft));
save(new TgCommandExecutor(telegram));
save(new EventListener(getInstance(TelegramProvider.class)));
save(new ServerPropertiesProvider(plugin));
Expand All @@ -53,7 +54,8 @@ public InjectionConfig(JavaPlugin plugin, ConfigProvider config, File configFile
commandExecutors.add(new ReloadConfigCommand(telegram, config, configFile));
commandExecutors.add(new ListAliasesCommand(telegram, config));
commandExecutors.add(new HealthCommand(telegram));
commandExecutors.add(new InstallPlugin(telegram, minecraft));
commandExecutors.add(new InstallPlugin(telegram, getInstance(PluginManager.class)));
commandExecutors.add(new RemovePlugin(telegram, getInstance(PluginManager.class)));

save(new CommandKeeper(telegram, commandExecutors, config));
save(new AliasExecutor(telegram, minecraft));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.server.BroadcastMessageEvent;
import org.bukkit.event.server.ServerLoadEvent;

import java.util.Optional;
Expand Down Expand Up @@ -87,6 +88,13 @@ void onMobSpawn(CreatureSpawnEvent event) {
}
}

@EventHandler
void onServerMessage(BroadcastMessageEvent event) {
// this cast is safe as BroadcastMessageEvent contains TextComponent
String message = ((TextComponent) event.message()).content();
telegram.sendMessageToMainChat("💬 " + message);
}

@EventHandler
void onServerLoad(ServerLoadEvent event) {
telegram.sendMessageToMainChat("✅ Сервер запущен!");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.senderman.telecrafter.minecraft;

import com.senderman.telecrafter.minecraft.provider.MinecraftProvider;
import org.bukkit.plugin.InvalidDescriptionException;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;

import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class PluginManager {

private final MinecraftProvider minecraft;

public PluginManager(MinecraftProvider minecraft) {
this.minecraft = minecraft;
}

@Nullable
public Plugin getPlugin(String pluginName) {
return minecraft.getPluginManager().getPlugin(pluginName);
}

public PluginDescriptionFile getPluginDescription(File file) throws InvalidDescriptionException {
return minecraft.getPluginLoader().getPluginDescription(file);
}

public void installPluginFile(File pluginFile) throws IOException {
var pluginDir = minecraft.getPluginsDirectory();
var newFile = new File(pluginDir, pluginFile.getName());
Files.move(Path.of(pluginFile.toURI()), Path.of(newFile.toURI()), StandardCopyOption.REPLACE_EXISTING);
}

public void deletePluginFile(JavaPlugin plugin) {
var oldFile = getPluginFile(plugin);
oldFile.delete();
}

private File getPluginFile(JavaPlugin plugin) {
try {
var getFileMethod = JavaPlugin.class.getDeclaredMethod("getFile");
getFileMethod.setAccessible(true);
return (File) getFileMethod.invoke(plugin);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
package com.senderman.telecrafter.telegram.command;

import com.senderman.telecrafter.minecraft.provider.MinecraftProvider;
import com.senderman.telecrafter.minecraft.PluginManager;
import com.senderman.telecrafter.telegram.TelegramProvider;
import com.senderman.telecrafter.telegram.api.entity.Document;
import com.senderman.telecrafter.telegram.api.entity.Message;
import com.senderman.telecrafter.telegram.command.abs.CommandExecutor;
import org.bukkit.plugin.InvalidDescriptionException;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;

import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class InstallPlugin implements CommandExecutor {

private final TelegramProvider telegram;
private final MinecraftProvider minecraft;
private final PluginManager pluginManager;

public InstallPlugin(TelegramProvider telegram, MinecraftProvider minecraft) {
public InstallPlugin(TelegramProvider telegram, PluginManager pluginManager) {
this.telegram = telegram;
this.minecraft = minecraft;
this.pluginManager = pluginManager;
}

@Override
Expand Down Expand Up @@ -59,18 +53,18 @@ public void execute(Message message) {
}
PluginDescriptionFile newDesc;
try {
newDesc = getPluginDescription(pluginFile);
newDesc = pluginManager.getPluginDescription(pluginFile);
} catch (InvalidDescriptionException e) {
telegram.sendMessage(chatId, "Не удалось загрузить описание плагина: " + e);
pluginFile.delete();
return;
}

var oldPlugin = getPlugin(newDesc.getName());
var oldPlugin = pluginManager.getPlugin(newDesc.getName());

if (oldPlugin == null) { // if there's no older version of this plugin
try {
installPluginFile(pluginFile);
pluginManager.installPluginFile(pluginFile);
String updateText = String.format(
"Плагин %s %s успешно установлен! Перезагрузите сервер для применения изменений",
newDesc.getName(), newDesc.getVersion());
Expand All @@ -80,11 +74,9 @@ public void execute(Message message) {
pluginFile.delete();
}
} else {
var oldFile = getPluginFile((JavaPlugin) oldPlugin);
//noinspection ResultOfMethodCallIgnored
oldFile.delete();
pluginManager.deletePluginFile((JavaPlugin) oldPlugin);
try {
installPluginFile(pluginFile);
pluginManager.installPluginFile(pluginFile);
} catch (IOException e) {
telegram.sendMessage(
chatId,
Expand All @@ -101,31 +93,6 @@ public void execute(Message message) {

}

private void installPluginFile(File pluginFile) throws IOException {
var pluginDir = minecraft.getPluginsDirectory();
var newFile = new File(pluginDir, pluginFile.getName());
Files.move(Path.of(pluginFile.toURI()), Path.of(newFile.toURI()), StandardCopyOption.REPLACE_EXISTING);
}

private PluginDescriptionFile getPluginDescription(File file) throws InvalidDescriptionException {
return minecraft.getPluginLoader().getPluginDescription(file);
}

private File getPluginFile(JavaPlugin plugin) {
try {
var getFileMethod = JavaPlugin.class.getDeclaredMethod("getFile");
getFileMethod.setAccessible(true);
return (File) getFileMethod.invoke(plugin);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}

@Nullable
private Plugin getPlugin(String pluginName) {
return minecraft.getPluginManager().getPlugin(pluginName);
}

private boolean validateMessage(Message message) {
return message.isReply() &&
message.getReply().hasDocument() &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.senderman.telecrafter.telegram.command;

import com.senderman.telecrafter.minecraft.PluginManager;
import com.senderman.telecrafter.telegram.TelegramProvider;
import com.senderman.telecrafter.telegram.api.entity.Message;
import com.senderman.telecrafter.telegram.command.abs.CommandExecutor;
import org.bukkit.plugin.java.JavaPlugin;

public class RemovePlugin implements CommandExecutor {

private final TelegramProvider telegram;
private final PluginManager pluginManager;

public RemovePlugin(TelegramProvider telegram, PluginManager pluginManager) {
this.telegram = telegram;
this.pluginManager = pluginManager;
}

@Override
public String getCommand() {
return "/rmplugin";
}

@Override
public String getDescription() {
return "удалить плагин. " + getCommand() + " имя-плагина";
}

@Override
public boolean adminOnly() {
return true;
}

@Override
public void execute(Message message) {
var chatId = message.getChatId();
String[] args = message.getText().split("\\s+", 2);
if (args.length < 2) {
telegram.sendMessage(chatId, "Использование: " + getCommand() + " имя-плагина");
return;
}
var pluginName = args[1];
var plugin = pluginManager.getPlugin(pluginName);
if (plugin == null) {
telegram.sendMessage(chatId, "Такого плагина нет!");
return;
}
pluginManager.deletePluginFile((JavaPlugin) plugin);
telegram.sendMessage(chatId,
"Плагин " + pluginName + " успешно удален! Перезагрузите сервер для применения изменений");
}
}

0 comments on commit a8998d9

Please sign in to comment.