Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .run/build_snapshot.run.xml → .run/[Snapshot] Build.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<ExternalSystemSettings>
<option name="env">
<map>
<entry key="TEST_PLUGIN_BUILD" value="1" />
<entry key="SERVER_PATH" value="C:\DEV\TestServer" />

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а зачем этот файлик тут

<entry key="TEST_PLUGIN_BUILD" value="true" />
</map>
</option>
<option name="executionName" />
Expand All @@ -26,4 +27,4 @@
<RunAsTest>false</RunAsTest>
<method v="2" />
</configuration>
</component>
</component>
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Plugin properties
repo=https://github.com/JouTak/TemplatePlugin-Java
repo=https://github.com/Slippers32/TemplatePlugin-Java
group=ru.joutak
name=Template-Java
name=ModCheckerPlugin
version=1.0.0

# Gradle properties
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ lombok = "1.18.32"
junit = "5.13.4"

[libraries]

paper = { module = "io.papermc.paper:paper-api", version.ref = "paper" }
lombok = { module = "org.projectlombok:lombok", version.ref = "lombok" }
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit"}
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/ru/joutak/plugin/ModCheckerPlugin.java
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 src/main/java/ru/joutak/plugin/listeners/JoinListener.java
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());
}
}
44 changes: 44 additions & 0 deletions src/main/java/ru/joutak/plugin/logic/Punisher.java
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);
});
}
}
53 changes: 53 additions & 0 deletions src/main/java/ru/joutak/plugin/logic/Validator.java
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 {
Comment thread
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 src/main/java/ru/joutak/plugin/logic/VerificationStorage.java
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 src/main/java/ru/joutak/plugin/network/PluginMessageHandler.java
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) {
Comment thread
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) {}
}
23 changes: 0 additions & 23 deletions src/main/java/ru/joutak/template/EmptyPlugin.java

This file was deleted.

17 changes: 17 additions & 0 deletions src/main/resources/config.yml
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}"
5 changes: 2 additions & 3 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
name: ${NAME}
version: ${VERSION}
api-version: ${MINECRAFT_VERSION}
main: ru.joutak.template.EmptyPlugin
main: ru.joutak.plugin.ModCheckerPlugin
description: 'A template plugin'
authors: ['EnderDissa', 'Stunnerer', 's4dnex']
contributors: ['technosimp']
authors: [ 'Slippers_friend']
website: ${WEBSITE}

depend: []
Expand Down
Loading
Loading