-
Notifications
You must be signed in to change notification settings - Fork 3
ModCheckerPlugin #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
ModCheckerPlugin #10
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package ru.joutak.plugin; | ||
|
|
||
| import lombok.Getter; | ||
| import org.bukkit.plugin.java.JavaPlugin; | ||
| import ru.joutak.plugin.listeners.JoinListener; | ||
| import ru.joutak.plugin.logic.Punisher; | ||
| import ru.joutak.plugin.logic.Validator; | ||
| import ru.joutak.plugin.logic.VerificationStorage; | ||
| import ru.joutak.plugin.network.PluginMessageHandler; | ||
|
|
||
| public final class ModCheckerPlugin extends JavaPlugin { | ||
| public static final String CHANNEL = "mod_checker:main"; | ||
| @Getter | ||
| private static ModCheckerPlugin instance; | ||
|
|
||
| @Override | ||
| public void onEnable() { | ||
| instance = this; | ||
|
|
||
| Punisher punisher = new Punisher(this); | ||
| Validator validator = new Validator(this, punisher); | ||
| VerificationStorage storage = new VerificationStorage(); | ||
|
|
||
| saveDefaultConfig(); | ||
|
|
||
| getServer().getMessenger().registerOutgoingPluginChannel(this, CHANNEL); | ||
| getServer().getMessenger().registerIncomingPluginChannel(this, CHANNEL, new PluginMessageHandler(this,validator,storage)); | ||
|
|
||
| getServer().getPluginManager().registerEvents(new JoinListener(this, storage), this); | ||
|
|
||
| getLogger().info(String.format("Плагин %s версии %s включен!", getPluginMeta().getName(), getPluginMeta().getVersion())); | ||
| } | ||
|
|
||
| @Override | ||
| public void onDisable() { | ||
| getServer().getMessenger().unregisterOutgoingPluginChannel(this); | ||
| getServer().getMessenger().unregisterIncomingPluginChannel(this); | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
src/main/java/ru/joutak/plugin/listeners/JoinListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package ru.joutak.plugin.listeners; | ||
|
|
||
| import org.bukkit.Bukkit; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.event.EventHandler; | ||
| import org.bukkit.event.Listener; | ||
| import org.bukkit.event.player.PlayerJoinEvent; | ||
| import org.bukkit.event.player.PlayerQuitEvent; | ||
| import ru.joutak.plugin.ModCheckerPlugin; | ||
| import ru.joutak.plugin.logic.VerificationStorage; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public class JoinListener implements Listener { | ||
|
|
||
| private final ModCheckerPlugin plugin; | ||
| private final VerificationStorage storage; | ||
|
|
||
| public JoinListener(ModCheckerPlugin plugin, VerificationStorage storage) { | ||
| this.plugin = plugin; | ||
| this.storage = storage; | ||
| } | ||
|
|
||
| @EventHandler | ||
| public void onJoin(PlayerJoinEvent event) { | ||
| Player player = event.getPlayer(); | ||
| UUID uuid = player.getUniqueId(); | ||
|
|
||
| Bukkit.getScheduler().runTaskLater(plugin, () -> { | ||
| if (!player.isOnline()) return; | ||
|
|
||
| player.sendPluginMessage(plugin, ModCheckerPlugin.CHANNEL, new byte[]{0}); | ||
| plugin.getLogger().info("Отправлен запрос модов от игрока " + player.getName()); | ||
|
|
||
| Bukkit.getScheduler().runTaskLater(plugin, () -> { | ||
| if (player.isOnline() && !storage.isVerified(uuid)) { | ||
| player.kickPlayer("§7У вас не установлен обязательный мод ModChecker!"); | ||
| plugin.getLogger().warning(player.getName() + " кикнут: мод не ответил вовремя."); | ||
| } | ||
| }, 200L); | ||
|
|
||
| }, 40L); | ||
| } | ||
|
|
||
| @EventHandler | ||
| public void onQuit(PlayerQuitEvent event) { | ||
| storage.remove(event.getPlayer().getUniqueId()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package ru.joutak.plugin.logic; | ||
|
|
||
| import org.bukkit.Bukkit; | ||
| import org.bukkit.BanList; | ||
| import org.bukkit.entity.Player; | ||
| import ru.joutak.plugin.ModCheckerPlugin; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.function.BiConsumer; | ||
|
|
||
| public class Punisher { | ||
| private final ModCheckerPlugin plugin; | ||
| private final Map<String, BiConsumer<Player, String>> actions = new HashMap<>(); | ||
|
|
||
| public Punisher(ModCheckerPlugin plugin) { | ||
| this.plugin = plugin; | ||
| setupActions(); | ||
| } | ||
|
|
||
| private void setupActions() { | ||
| actions.put("BAN", (player, reason) -> { | ||
| String msg = plugin.getConfig().getString("messages.ban", "Banned").replace("{reason}", reason); | ||
| Bukkit.getBanList(BanList.Type.NAME).addBan(player.getName(), msg, null, "ModChecker"); | ||
| player.kickPlayer(msg); | ||
| }); | ||
|
|
||
| actions.put("KICK", (player, reason) -> { | ||
| String msg = plugin.getConfig().getString("messages.kick", "Kicked").replace("{reason}", reason); | ||
| player.kickPlayer(msg); | ||
| }); | ||
|
|
||
| actions.put("LOG", (player, reason) -> | ||
| plugin.getLogger().warning(player.getName() + ": " + reason) | ||
| ); | ||
| } | ||
|
|
||
| public void execute(Player player, String actionName, String reason) { | ||
| Bukkit.getScheduler().runTask(plugin, () -> { | ||
| actions.getOrDefault(actionName.toUpperCase(), actions.get("LOG")) | ||
| .accept(player, reason); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package ru.joutak.plugin.logic; | ||
|
|
||
| import org.bukkit.Bukkit; | ||
| import org.bukkit.BanList; | ||
| import org.bukkit.configuration.file.FileConfiguration; | ||
| import org.bukkit.entity.Player; | ||
| import ru.joutak.plugin.ModCheckerPlugin; | ||
| import ru.joutak.plugin.network.PluginMessageHandler.ModData; | ||
| import ru.joutak.plugin.logic.Punisher; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class Validator { | ||
|
Slippers32 marked this conversation as resolved.
|
||
|
|
||
| private final ModCheckerPlugin plugin; | ||
| private final Punisher punisher; | ||
|
|
||
| public Validator(ModCheckerPlugin plugin, Punisher punisher) { | ||
| this.plugin = plugin; | ||
| this.punisher = punisher; | ||
| } | ||
|
|
||
| public void process(Player player, List<ModData> playerMods) { | ||
| FileConfiguration config = plugin.getConfig(); | ||
|
|
||
| for (String entry : config.getStringList("required-mods")) { | ||
| String targetId = entry.contains(":") ? entry.split(":", 2)[0] : entry; | ||
| String targetVer = entry.contains(":") ? entry.split(":", 2)[1] : null; | ||
|
|
||
| ModData found = playerMods.stream().filter(m -> m.id().equals(targetId)).findFirst().orElse(null); | ||
|
|
||
| if (found == null) { | ||
| punish(player, "on-missing-required", "Скачай мод: " + targetId); | ||
| return; | ||
| } | ||
| if (targetVer != null && !found.version().equals(targetVer)) { | ||
| punish(player, "on-missing-required", "Версия " + targetId + " должна быть : " + targetVer); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| for (ModData mod : playerMods) { | ||
| if (config.getStringList("blacklisted-mods").contains(mod.id())) { | ||
| punish(player, "on-found-blacklisted", "Запрещеный мод (" + mod.id()+")"); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| private void punish(Player player, String configPath, String reason) { | ||
| String actionType = plugin.getConfig().getString(configPath, "LOG"); | ||
| punisher.execute(player, actionType, reason); | ||
| } | ||
| } | ||
21 changes: 21 additions & 0 deletions
21
src/main/java/ru/joutak/plugin/logic/VerificationStorage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package ru.joutak.plugin.logic; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import java.util.UUID; | ||
|
|
||
| public class VerificationStorage { | ||
| private final Set<UUID> verifiedPlayers = new HashSet<>(); | ||
|
|
||
| public void setVerified(UUID uuid) { | ||
| verifiedPlayers.add(uuid); | ||
| } | ||
|
|
||
| public boolean isVerified(UUID uuid) { | ||
| return verifiedPlayers.contains(uuid); | ||
| } | ||
|
|
||
| public void remove(UUID uuid) { | ||
| verifiedPlayers.remove(uuid); | ||
| } | ||
| } |
75 changes: 75 additions & 0 deletions
75
src/main/java/ru/joutak/plugin/network/PluginMessageHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package ru.joutak.plugin.network; | ||
|
|
||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.plugin.messaging.PluginMessageListener; | ||
| import ru.joutak.plugin.ModCheckerPlugin; | ||
| import ru.joutak.plugin.logic.Punisher; | ||
| import ru.joutak.plugin.logic.Validator; | ||
| import ru.joutak.plugin.logic.VerificationStorage; | ||
|
|
||
| import java.io.*; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class PluginMessageHandler implements PluginMessageListener { | ||
|
|
||
| private final ModCheckerPlugin plugin; | ||
| private final Validator validator; | ||
| private final VerificationStorage storage; | ||
|
|
||
| public PluginMessageHandler(ModCheckerPlugin plugin, Validator validator, VerificationStorage storage) { | ||
| this.plugin = plugin; | ||
| this.validator = validator; | ||
| this.storage = storage; | ||
| } | ||
|
|
||
| @Override | ||
| public void onPluginMessageReceived(String channel, Player player, byte[] message) { | ||
| if (!channel.equals(ModCheckerPlugin.CHANNEL)) return; | ||
|
|
||
| storage.setVerified(player.getUniqueId()); | ||
|
|
||
| List<ModData> playerMods = new ArrayList<>(); | ||
|
|
||
| try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(message))) { | ||
| int count = readVarInt(in); | ||
| for (int i = 0; i < count; i++) { | ||
| String id = readString(in); | ||
| String ver = readString(in); | ||
| playerMods.add(new ModData(id, ver)); | ||
| } | ||
| } catch (IOException e) { | ||
| plugin.getLogger().warning("Failed to read packet from " + player.getName() + ": " + e.getMessage()); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| validator.process(player, playerMods); | ||
| } catch (Exception e) { | ||
|
Slippers32 marked this conversation as resolved.
|
||
| plugin.getLogger().severe("Error while processing mods for " + player.getName() + ": " + e.getMessage()); | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| private String readString(DataInputStream in) throws IOException { | ||
| int len = readVarInt(in); | ||
| byte[] b = new byte[len]; | ||
| in.readFully(b); | ||
| return new String(b, StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| private int readVarInt(DataInputStream in) throws IOException { | ||
| int result = 0, numRead = 0; | ||
| byte read; | ||
| do { | ||
| read = in.readByte(); | ||
| result |= (read & 0x7F) << (7 * numRead++); | ||
| if (numRead > 5) throw new RuntimeException("VarInt too big"); | ||
| } while ((read & 0x80) != 0); | ||
| return result; | ||
| } | ||
|
|
||
| public record ModData(String id, String version) {} | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Действия BAN KICK LOG | ||
| on-missing-required: KICK | ||
| on-found-blacklisted: BAN | ||
|
|
||
| # Формат: "id" или "id:version" | ||
| required-mods: | ||
| - "fabric-api" | ||
| - "mod_checker:1.0.0" | ||
|
|
||
| blacklisted-mods: | ||
| - "xray" | ||
| - "meteor-client" | ||
| - "freecam" | ||
|
|
||
| messages: | ||
| kick: "Вы не можете зайти\n§8Причина: §f{reason}" | ||
| ban: "Вы забанены!\n§8Причина: §f{reason}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а зачем этот файлик тут