Skip to content

Commit 23de534

Browse files
committed
added code
barely tested it, will finish later
0 parents  commit 23de534

6 files changed

Lines changed: 485 additions & 0 deletions

File tree

.gitignore

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# User-specific stuff
2+
.idea/
3+
4+
*.iml
5+
*.ipr
6+
*.iws
7+
8+
# IntelliJ
9+
out/
10+
11+
# Compiled class file
12+
*.class
13+
14+
# Log file
15+
*.log
16+
17+
# BlueJ files
18+
*.ctxt
19+
20+
# Package Files #
21+
*.jar
22+
*.war
23+
*.nar
24+
*.ear
25+
*.zip
26+
*.tar.gz
27+
*.rar
28+
29+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
30+
hs_err_pid*
31+
32+
*~
33+
34+
# temporary files which can be created if a process still has a handle open of a deleted file
35+
.fuse_hidden*
36+
37+
# KDE directory preferences
38+
.directory
39+
40+
# Linux trash folder which might appear on any partition or disk
41+
.Trash-*
42+
43+
# .nfs files are created when an open file is removed but is still being accessed
44+
.nfs*
45+
46+
# General
47+
.DS_Store
48+
.AppleDouble
49+
.LSOverride
50+
51+
# Icon must end with two \r
52+
Icon
53+
54+
# Thumbnails
55+
._*
56+
57+
# Files that might appear in the root of a volume
58+
.DocumentRevisions-V100
59+
.fseventsd
60+
.Spotlight-V100
61+
.TemporaryItems
62+
.Trashes
63+
.VolumeIcon.icns
64+
.com.apple.timemachine.donotpresent
65+
66+
# Directories potentially created on remote AFP share
67+
.AppleDB
68+
.AppleDesktop
69+
Network Trash Folder
70+
Temporary Items
71+
.apdisk
72+
73+
# Windows thumbnail cache files
74+
Thumbs.db
75+
Thumbs.db:encryptable
76+
ehthumbs.db
77+
ehthumbs_vista.db
78+
79+
# Dump file
80+
*.stackdump
81+
82+
# Folder config file
83+
[Dd]esktop.ini
84+
85+
# Recycle Bin used on file shares
86+
$RECYCLE.BIN/
87+
88+
# Windows Installer files
89+
*.cab
90+
*.msi
91+
*.msix
92+
*.msm
93+
*.msp
94+
95+
# Windows shortcuts
96+
*.lnk
97+
98+
target/
99+
100+
pom.xml.tag
101+
pom.xml.releaseBackup
102+
pom.xml.versionsBackup
103+
pom.xml.next
104+
105+
release.properties
106+
dependency-reduced-pom.xml
107+
buildNumber.properties
108+
.mvn/timing.properties
109+
.mvn/wrapper/maven-wrapper.jar
110+
.flattened-pom.xml
111+
112+
# Common working directory
113+
run/

pom.xml

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>lol.hyper</groupId>
8+
<artifactId>velocityblockversion</artifactId>
9+
<version>1.0</version>
10+
<packaging>jar</packaging>
11+
12+
<name>VelocityBlockVersion</name>
13+
14+
<properties>
15+
<java.version>1.8</java.version>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
</properties>
18+
19+
<build>
20+
<resources>
21+
<resource>
22+
<directory>src/main/resources</directory>
23+
<filtering>true</filtering>
24+
</resource>
25+
</resources>
26+
<plugins>
27+
<plugin>
28+
<groupId>org.apache.maven.plugins</groupId>
29+
<artifactId>maven-compiler-plugin</artifactId>
30+
<version>3.8.1</version>
31+
<configuration>
32+
<source>${java.version}</source>
33+
<target>${java.version}</target>
34+
</configuration>
35+
</plugin>
36+
<plugin>
37+
<groupId>org.codehaus.mojo</groupId>
38+
<artifactId>templating-maven-plugin</artifactId>
39+
<version>1.0.0</version>
40+
<executions>
41+
<execution>
42+
<id>filter-src</id>
43+
<goals>
44+
<goal>filter-sources</goal>
45+
</goals>
46+
</execution>
47+
</executions>
48+
</plugin>
49+
<plugin>
50+
<groupId>org.apache.maven.plugins</groupId>
51+
<artifactId>maven-shade-plugin</artifactId>
52+
<version>3.2.4</version>
53+
<configuration>
54+
<relocations>
55+
<relocation>
56+
<pattern>org.bstats</pattern>
57+
<!-- Replace this with your package! -->
58+
<shadedPattern>lol.hyper.velocityblockversion.bstats</shadedPattern>
59+
</relocation>
60+
</relocations>
61+
</configuration>
62+
<executions>
63+
<execution>
64+
<phase>package</phase>
65+
<goals>
66+
<goal>shade</goal>
67+
</goals>
68+
</execution>
69+
</executions>
70+
</plugin>
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-site-plugin</artifactId>
74+
<version>3.9.1</version>
75+
<dependencies>
76+
<dependency>
77+
<groupId>net.trajano.wagon</groupId>
78+
<artifactId>wagon-git</artifactId>
79+
<version>2.0.4</version>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.apache.maven.doxia</groupId>
83+
<artifactId>doxia-module-markdown</artifactId>
84+
<version>1.9.1</version>
85+
</dependency>
86+
</dependencies>
87+
</plugin>
88+
<plugin>
89+
<groupId>org.apache.maven.plugins</groupId>
90+
<artifactId>maven-release-plugin</artifactId>
91+
<version>3.0.0-M1</version>
92+
<configuration>
93+
<autoVersionSubmodules>true</autoVersionSubmodules>
94+
<tagNameFormat>@{project.version}</tagNameFormat>
95+
<scmCommentPrefix xml:space="preserve">[RELEASE] </scmCommentPrefix>
96+
<goals>install deploy site-deploy
97+
</goals> <!-- install is here to fix javadoc generation in multi-module projects -->
98+
<releaseProfiles>release</releaseProfiles>
99+
</configuration>
100+
</plugin>
101+
</plugins>
102+
</build>
103+
104+
<repositories>
105+
<repository>
106+
<id>velocitypowered-repo</id>
107+
<url>https://nexus.velocitypowered.com/repository/maven-public/</url>
108+
</repository>
109+
</repositories>
110+
111+
<dependencies>
112+
<dependency>
113+
<groupId>com.velocitypowered</groupId>
114+
<artifactId>velocity-api</artifactId>
115+
<version>3.0.0</version>
116+
<scope>provided</scope>
117+
</dependency>
118+
<dependency>
119+
<groupId>net.kyori</groupId>
120+
<artifactId>adventure-text-minimessage</artifactId>
121+
<version>4.1.0-SNAPSHOT</version>
122+
</dependency>
123+
<dependency>
124+
<groupId>org.bstats</groupId>
125+
<artifactId>bstats-velocity</artifactId>
126+
<version>2.2.1</version>
127+
<scope>compile</scope>
128+
</dependency>
129+
</dependencies>
130+
</project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package lol.hyper.velocityblockversion;
2+
3+
import com.google.inject.Inject;
4+
import com.velocitypowered.api.event.PostOrder;
5+
import com.velocitypowered.api.event.connection.PreLoginEvent;
6+
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
7+
import com.velocitypowered.api.event.Subscribe;
8+
import com.velocitypowered.api.plugin.Plugin;
9+
import lol.hyper.velocityblockversion.tools.ConfigHandler;
10+
import lol.hyper.velocityblockversion.tools.VersionToStrings;
11+
import net.kyori.adventure.text.Component;
12+
import net.kyori.adventure.text.minimessage.MiniMessage;
13+
import org.bstats.velocity.Metrics;
14+
import org.slf4j.Logger;
15+
16+
@Plugin(
17+
id = "velocityblockversion",
18+
name = "VelocityBlockVersion",
19+
version = "1.0",
20+
authors = {"hyperdefined"},
21+
description = "Block certain Minecraft versions from connecting to your network."
22+
)
23+
public class VelocityBlockVersion {
24+
25+
public ConfigHandler configHandler;
26+
27+
public final Logger logger;
28+
private final Metrics.Factory metricsFactory;
29+
30+
@Inject
31+
public VelocityBlockVersion(Logger logger, Metrics.Factory metricsFactory) {
32+
this.logger = logger;
33+
this.metricsFactory = metricsFactory;
34+
}
35+
36+
@Subscribe
37+
public void onProxyInitialization(ProxyInitializeEvent event) {
38+
VersionToStrings.init();
39+
configHandler = new ConfigHandler(this);
40+
configHandler.loadConfig();
41+
Metrics metrics = metricsFactory.make(this, 13308);
42+
}
43+
44+
@Subscribe(order = PostOrder.FIRST)
45+
public void onPlayerLogin(PreLoginEvent event) {
46+
int version = event.getConnection().getProtocolVersion().getProtocol();
47+
if (ConfigHandler.versions.contains(version)) {
48+
String allowedVersions = VersionToStrings.allowedVersions(ConfigHandler.versions);
49+
String blockedMessage = configHandler.config.getString("disconnect_message");
50+
if (allowedVersions == null) {
51+
blockedMessage = "<red>All versions are currently blocked from playing.</red>";
52+
}
53+
if (blockedMessage.contains("{VERSIONS}")) {
54+
blockedMessage = blockedMessage.replace("{VERSIONS}", allowedVersions);
55+
}
56+
Component message = MiniMessage.get().parse(blockedMessage);
57+
event.setResult(PreLoginEvent.PreLoginComponentResult.denied(message));
58+
logger.info("Blocking player " + event.getUsername() + " because they are playing on version "
59+
+ VersionToStrings.versionStrings.get(version) + " which is blocked!");
60+
}
61+
}
62+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package lol.hyper.velocityblockversion.tools;
2+
3+
import com.moandjiezana.toml.Toml;
4+
import lol.hyper.velocityblockversion.VelocityBlockVersion;
5+
6+
import java.io.*;
7+
import java.nio.file.Files;
8+
import java.util.ArrayList;
9+
import java.util.Iterator;
10+
import java.util.List;
11+
12+
public class ConfigHandler {
13+
14+
public Toml config;
15+
private final VelocityBlockVersion velocityBlockVersion;
16+
public static final List<Integer> versions = new ArrayList<>();
17+
public static final double CONFIG_VERSION = 2;
18+
19+
public ConfigHandler(VelocityBlockVersion velocityBlockVersion) {
20+
this.velocityBlockVersion = velocityBlockVersion;
21+
}
22+
23+
public void loadConfig() {
24+
File configFile = new File("plugins" + File.separator + "VelocityBlockVersion", "config.toml");
25+
if (!configFile.exists()) {
26+
InputStream is = velocityBlockVersion.getClass().getResourceAsStream( "/config.toml");
27+
File path = new File("plugins" + File.separator + "VelocityBlockVersion");
28+
try {
29+
if (path.mkdir()) {
30+
Files.copy(is, configFile.toPath());
31+
velocityBlockVersion.logger.info("Copying default config...");
32+
} else {
33+
velocityBlockVersion.logger.error("Unable to create config folder!");
34+
}
35+
} catch (IOException e) {
36+
velocityBlockVersion.logger.error("Unable to copy default config!", e);
37+
}
38+
}
39+
InputStream inputStream;
40+
try {
41+
inputStream = new FileInputStream(configFile);
42+
} catch (FileNotFoundException e) {
43+
velocityBlockVersion.logger.error("Unable to find config!", e);
44+
return;
45+
}
46+
config = new Toml().read(inputStream);
47+
48+
if (config.getLong("config_version") != CONFIG_VERSION) {
49+
velocityBlockVersion.logger.warn(
50+
"Your config is outdated. We will attempt to load your current config. However, things might not work!");
51+
velocityBlockVersion.logger.warn(
52+
"To fix this, delete your current config and let the server remake it.");
53+
}
54+
// for some reason, the config loads the versions as longs
55+
// we have to convert them this ugly way
56+
for (Object obj : config.getList("versions")) {
57+
long t = (long) obj;
58+
versions.add((int) t);
59+
}
60+
if (versions.size() == 0) {
61+
velocityBlockVersion.logger.warn("There are no versions listed in the config!");
62+
} else {
63+
velocityBlockVersion.logger.info("Loaded " + versions.size() + " versions!");
64+
}
65+
// use an iterator here so we can remove stuff
66+
Iterator<Integer> iter = versions.iterator();
67+
while (iter.hasNext()) {
68+
int version = iter.next();
69+
if (!VersionToStrings.versionStrings.containsKey(version)) {
70+
velocityBlockVersion.logger.warn(
71+
"Version " + version + " is NOT a valid version number! Ignoring this version.");
72+
iter.remove();
73+
}
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)