Skip to content

Commit 77a596f

Browse files
committed
Added Velocity 3.1.X support
1 parent f220748 commit 77a596f

File tree

7 files changed

+337
-0
lines changed

7 files changed

+337
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<module>bungee</module>
1919
<module>bukkit</module>
2020
<module>bukkit-1.17</module>
21+
<module>velocity</module>
2122
</modules>
2223

2324
<licenses>

velocity/pom.xml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
<parent>
6+
<artifactId>badlionclientmodapi</artifactId>
7+
<groupId>net.badlion</groupId>
8+
<version>2.0.0</version>
9+
</parent>
10+
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>mod-api-velocity</artifactId>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
</properties>
18+
19+
<build>
20+
<defaultGoal>clean install</defaultGoal>
21+
<finalName>Badlion-ModApi-Velocity</finalName>
22+
<sourceDirectory>src/main/java</sourceDirectory>
23+
24+
<resources>
25+
<resource>
26+
<targetPath>.</targetPath>
27+
<filtering>true</filtering>
28+
<directory>src/main/resources/</directory>
29+
30+
<includes>
31+
<include>*.yml</include>
32+
</includes>
33+
</resource>
34+
</resources>
35+
36+
<plugins>
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-compiler-plugin</artifactId>
40+
<version>3.8.1</version>
41+
42+
<configuration>
43+
<source>8</source>
44+
<target>8</target>
45+
</configuration>
46+
</plugin>
47+
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-shade-plugin</artifactId>
51+
<version>3.1.0</version>
52+
53+
<executions>
54+
<execution>
55+
<phase>package</phase>
56+
<goals>
57+
<goal>shade</goal>
58+
</goals>
59+
</execution>
60+
</executions>
61+
</plugin>
62+
</plugins>
63+
</build>
64+
65+
<dependencies>
66+
<dependency>
67+
<groupId>net.badlion</groupId>
68+
<artifactId>modapi-common</artifactId>
69+
<version>2.0.0</version>
70+
<scope>compile</scope>
71+
</dependency>
72+
73+
<dependency>
74+
<groupId>com.velocitypowered</groupId>
75+
<artifactId>velocity-api</artifactId>
76+
<version>3.1.0</version>
77+
<scope>provided</scope>
78+
</dependency>
79+
</dependencies>
80+
81+
<repositories>
82+
<repository>
83+
<id>velocity</id>
84+
<url>https://repo.velocitypowered.com/snapshots/</url>
85+
</repository>
86+
</repositories>
87+
</project>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package net.badlion.velocityapi;
2+
3+
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonParser;
5+
import net.badlion.modapicommon.AbstractBadlionApi;
6+
import net.badlion.modapicommon.AbstractPluginMessageSender;
7+
import net.badlion.modapicommon.Config;
8+
9+
import java.io.File;
10+
import java.io.FileNotFoundException;
11+
import java.io.FileReader;
12+
import java.io.FileWriter;
13+
import java.io.IOException;
14+
import java.io.Reader;
15+
import java.nio.file.Files;
16+
import java.nio.file.Path;
17+
import java.nio.file.Paths;
18+
19+
public class VelocityBadlionApi extends AbstractBadlionApi {
20+
private final VelocityBadlionPlugin plugin;
21+
private Config config;
22+
23+
public VelocityBadlionApi(VelocityBadlionPlugin plugin) {
24+
this.plugin = plugin;
25+
}
26+
27+
@Override
28+
public void loadConfig(File file) throws IOException {
29+
try (Reader reader = new FileReader(file)) {
30+
this.config = AbstractBadlionApi.GSON_NON_PRETTY.fromJson(reader, Config.class);
31+
32+
} catch (FileNotFoundException ex) {
33+
this.plugin.getLogger().info("No Config Found: Saving default...");
34+
35+
final Config config = new Config();
36+
this.saveConfig(config, new File(this.plugin.getDataFolder(), "config.json"));
37+
38+
this.config = config;
39+
}
40+
41+
try {
42+
this.findOtherConfigs();
43+
44+
} catch (Exception ex) {
45+
this.plugin.getLogger().warn("Problem occurred trying to read old config: " + ex.getMessage(), ex);
46+
}
47+
}
48+
49+
private void findOtherConfigs() throws IOException {
50+
final Path cpsConfig = Paths.get("plugins/BadlionClientCPSAPI/config.json");
51+
52+
if (Files.exists(cpsConfig)) {
53+
this.plugin.getLogger().info("Found config file from old Badlion Client CPS API, attempting to import config...");
54+
55+
JsonObject oldConfig = new JsonParser().parse(new String(Files.readAllBytes(cpsConfig))).getAsJsonObject();
56+
57+
this.config.setClicksPerSecondLimitRight(oldConfig.get("clicksPerSecondLimitRight").getAsInt());
58+
this.config.setClicksPerSecondLimit(oldConfig.get("clicksPerSecondLimit").getAsInt());
59+
60+
this.saveConfig(this.config, new File(this.plugin.getDataFolder(), "config.json"));
61+
62+
this.plugin.getLogger().warn("Imported config from cps api to new config, deleting old config...");
63+
this.plugin.getLogger().warn("Since this version of the Badlion Mod API, the CPS API plugin is no longer necessary! Please delete it.");
64+
65+
Files.deleteIfExists(cpsConfig);
66+
}
67+
}
68+
69+
@Override
70+
public void saveConfig(Config config, File file) {
71+
try (FileWriter writer = new FileWriter(file)) {
72+
AbstractBadlionApi.GSON_PRETTY.toJson(config, writer);
73+
74+
} catch (Exception ex) {
75+
this.plugin.getLogger().warn("Failed to save config: " + ex.getMessage(), ex);
76+
}
77+
}
78+
79+
@Override
80+
public Config getBadlionConfig() {
81+
return this.config;
82+
}
83+
84+
@Override
85+
public AbstractPluginMessageSender getPluginMessageSender() {
86+
return this.plugin.getMessageSender();
87+
}
88+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package net.badlion.velocityapi;
2+
3+
import com.google.inject.Inject;
4+
import com.velocitypowered.api.event.Subscribe;
5+
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
6+
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;
7+
import com.velocitypowered.api.plugin.Plugin;
8+
import com.velocitypowered.api.proxy.ProxyServer;
9+
import net.badlion.velocityapi.listener.PlayerListener;
10+
import net.badlion.velocityapi.waypoints.UnsupportedWaypointManager;
11+
import org.slf4j.Logger;
12+
13+
import java.io.File;
14+
import java.io.IOException;
15+
16+
@Plugin(
17+
id = "badlion-modapi",
18+
name = "Badlion Client Mod Api",
19+
version = "2.0",
20+
authors = {"Badlion"}
21+
)
22+
public class VelocityBadlionPlugin {
23+
private final ProxyServer proxyServer;
24+
private final Logger logger;
25+
private final VelocityBadlionApi badlionApi;
26+
private final VelocityPluginMessageSender messageSender;
27+
private final File dataFolder;
28+
29+
@Inject
30+
public VelocityBadlionPlugin(ProxyServer proxyServer, Logger logger) {
31+
this.proxyServer = proxyServer;
32+
this.logger = logger;
33+
this.badlionApi = new VelocityBadlionApi(this);
34+
this.messageSender = new VelocityPluginMessageSender(this);
35+
this.dataFolder = new File("plugins", "BadlionClientModAPI");
36+
this.badlionApi.setWaypointManager(new UnsupportedWaypointManager());
37+
}
38+
39+
@Subscribe
40+
public void onProxyInitialization(ProxyInitializeEvent event) {
41+
if (!this.getDataFolder().exists()) {
42+
if (!this.getDataFolder().mkdir()) {
43+
this.getLogger().error("Failed to create plugin directory.");
44+
}
45+
}
46+
47+
try {
48+
this.badlionApi.loadConfig(new File(this.getDataFolder(), "config.json"));
49+
50+
// Only register the listener if the config loads successfully
51+
this.getProxy().getEventManager().register(this, new PlayerListener(this));
52+
53+
this.getLogger().info("Successfully setup BadlionClientModAPI plugin.");
54+
} catch (IOException ex) {
55+
this.getLogger().error("Error with config for BadlionClientModAPI plugin: " + ex.getMessage(), ex);
56+
}
57+
}
58+
59+
@Subscribe
60+
public void onProxyShutdown(ProxyShutdownEvent event) {
61+
this.badlionApi.saveConfig(this.badlionApi.getBadlionConfig(), new File(this.getDataFolder(), "config.json"));
62+
}
63+
64+
public ProxyServer getProxy() {
65+
return this.proxyServer;
66+
}
67+
68+
public Logger getLogger() {
69+
return this.logger;
70+
}
71+
72+
public VelocityBadlionApi getBadlionApi() {
73+
return this.badlionApi;
74+
}
75+
76+
public VelocityPluginMessageSender getMessageSender() {
77+
return this.messageSender;
78+
}
79+
80+
public File getDataFolder() {
81+
return this.dataFolder;
82+
}
83+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package net.badlion.velocityapi;
2+
3+
import com.velocitypowered.api.proxy.Player;
4+
import com.velocitypowered.api.proxy.messages.LegacyChannelIdentifier;
5+
import net.badlion.modapicommon.AbstractPluginMessageSender;
6+
7+
import java.util.Optional;
8+
import java.util.UUID;
9+
10+
public class VelocityPluginMessageSender extends AbstractPluginMessageSender {
11+
12+
private final VelocityBadlionPlugin plugin;
13+
14+
public VelocityPluginMessageSender(VelocityBadlionPlugin plugin) {
15+
this.plugin = plugin;
16+
}
17+
18+
@Override
19+
public void sendPluginMessage(byte[] data) {
20+
for (Player player : this.plugin.getProxy().getAllPlayers()) {
21+
this.sendPluginMessagePacket(player, "badlion:modapi", data);
22+
}
23+
}
24+
25+
@Override
26+
public void sendPluginMessage(UUID player, byte[] data) {
27+
Optional<Player> proxiedPlayer = this.plugin.getProxy().getPlayer(player);
28+
29+
proxiedPlayer.ifPresent(value -> this.sendPluginMessagePacket(value, "badlion:modapi", data));
30+
}
31+
32+
public void sendPluginMessagePacket(Player player, String channel, byte[] data) {
33+
player.sendPluginMessage(new LegacyChannelIdentifier(channel), data);
34+
}
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package net.badlion.velocityapi.listener;
2+
3+
import com.velocitypowered.api.event.Subscribe;
4+
import com.velocitypowered.api.event.connection.PostLoginEvent;
5+
import com.velocitypowered.api.proxy.Player;
6+
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
7+
import net.badlion.velocityapi.VelocityBadlionPlugin;
8+
import net.badlion.modapicommon.AbstractBadlionApi;
9+
10+
public class PlayerListener {
11+
12+
private final VelocityBadlionPlugin plugin;
13+
14+
public PlayerListener(VelocityBadlionPlugin plugin) {
15+
this.plugin = plugin;
16+
}
17+
18+
@Subscribe
19+
public void onLogin(PostLoginEvent event) {
20+
// Send the disallowed mods to players when they login to the proxy. A notification will appear on the Badlion Client so they know the mod was disabled
21+
Player player = event.getPlayer();
22+
player.sendPluginMessage(MinecraftChannelIdentifier.from("badlion:modapi"), AbstractBadlionApi.GSON_NON_PRETTY.toJson(this.plugin.getBadlionApi().getBadlionConfig()).getBytes());
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package net.badlion.velocityapi.waypoints;
2+
3+
import net.badlion.modapicommon.utility.AbstractWaypoint;
4+
import net.badlion.modapicommon.utility.AbstractWaypointManager;
5+
6+
import java.util.UUID;
7+
8+
public class UnsupportedWaypointManager extends AbstractWaypointManager {
9+
10+
@Override
11+
public void addPlayerWaypoint(UUID player, AbstractWaypoint waypoint) {
12+
throw new UnsupportedOperationException("Waypoint mod hooks are not supported on velocity!");
13+
}
14+
15+
@Override
16+
public void removePlayerWaypoint(UUID player, int id) {
17+
throw new UnsupportedOperationException("Waypoint mod hooks are not supported on velocity!");
18+
}
19+
}

0 commit comments

Comments
 (0)