-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,19 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.vanillage.validusername</groupId> | ||
<artifactId>validusername</artifactId> | ||
<version>1.0</version> | ||
<name>ValidUsername</name> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.vanillage.minecraftalphaserver</groupId> | ||
<artifactId>minecraftalphaserver</artifactId> | ||
<version>1.0</version> | ||
<scope>system</scope> | ||
<systemPath>${project.basedir}/lib/minecraft_server_unobfuscated.jar</systemPath> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<finalName>ValidUsername</finalName> | ||
</build> | ||
</project> |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/vanillage/validusername/ValidUsername.java
This file contains 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 @@ | ||
package com.vanillage.validusername; | ||
|
||
import com.vanillage.minecraftalphaserver.event.PlayerLoggedInListener; | ||
import com.vanillage.minecraftalphaserver.plugin.Plugin; | ||
|
||
public final class ValidUsername extends Plugin { | ||
@Override | ||
public void onEnable() { | ||
getMinecraftServer().getPluginManager().getEventManager().registerListener((PlayerLoggedInListener) new ValidUsernamePlayerLoggedInListener(this)); | ||
getMinecraftServer().log(getName() + " enabled"); | ||
} | ||
|
||
@Override | ||
public void onDisable() { | ||
getMinecraftServer().log(getName() + " disabled"); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
src/main/java/com/vanillage/validusername/ValidUsernamePlayerLoggedInListener.java
This file contains 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,93 @@ | ||
package com.vanillage.validusername; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
import com.vanillage.minecraftalphaserver.event.PlayerLoggedInEvent; | ||
import com.vanillage.minecraftalphaserver.event.PlayerLoggedInListener; | ||
|
||
public final class ValidUsernamePlayerLoggedInListener extends PlayerLoggedInListener { | ||
private final ValidUsername plugin; | ||
private final int priority; | ||
private Pattern userNamePattern; | ||
private String kickReason; | ||
|
||
public ValidUsernamePlayerLoggedInListener(ValidUsername plugin) { | ||
this(plugin, -10, null, null); | ||
} | ||
|
||
public ValidUsernamePlayerLoggedInListener(ValidUsername plugin, int priority) { | ||
this(plugin, priority, null, null); | ||
} | ||
|
||
public ValidUsernamePlayerLoggedInListener(ValidUsername plugin, Pattern userNamePattern) { | ||
this(plugin, -10, userNamePattern, null); | ||
} | ||
|
||
public ValidUsernamePlayerLoggedInListener(ValidUsername plugin, String kickReason) { | ||
this(plugin, -10, null, kickReason); | ||
} | ||
|
||
public ValidUsernamePlayerLoggedInListener(ValidUsername plugin, int priority, Pattern userNamePattern) { | ||
this(plugin, priority, userNamePattern, null); | ||
} | ||
|
||
public ValidUsernamePlayerLoggedInListener(ValidUsername plugin, int priority, String kickReason) { | ||
this(plugin, priority, null, kickReason); | ||
} | ||
|
||
public ValidUsernamePlayerLoggedInListener(ValidUsername plugin, Pattern userNamePattern, String kickReason) { | ||
this(plugin, -10, userNamePattern, kickReason); | ||
} | ||
|
||
public ValidUsernamePlayerLoggedInListener(ValidUsername plugin, int priority, Pattern userNamePattern, String kickReason) { | ||
if (plugin == null) { | ||
throw new IllegalArgumentException("plugin cannot be null"); | ||
} | ||
|
||
this.plugin = plugin; | ||
this.priority = priority; | ||
this.userNamePattern = userNamePattern == null ? Pattern.compile("[A-Za-z0-9_]{2,16}") : userNamePattern; | ||
this.kickReason = kickReason == null ? "Invalid username <username>, try: " + this.userNamePattern.pattern() : kickReason; | ||
} | ||
|
||
public ValidUsername getPlugin() { | ||
return plugin; | ||
} | ||
|
||
public Pattern getUserNamePattern() { | ||
return userNamePattern; | ||
} | ||
|
||
public void setUserNamePattern(Pattern userNamePattern) { | ||
if (userNamePattern == null) { | ||
throw new IllegalArgumentException("userNamePattern cannot be null"); | ||
} | ||
|
||
this.userNamePattern = userNamePattern; | ||
} | ||
|
||
public String getKickReason() { | ||
return kickReason; | ||
} | ||
|
||
public void setKickReason(String kickReason) { | ||
if (kickReason == null) { | ||
throw new IllegalArgumentException("kickReason cannot be null"); | ||
} | ||
|
||
this.kickReason = kickReason; | ||
} | ||
|
||
@Override | ||
public void onEvent(PlayerLoggedInEvent event) { | ||
if (!userNamePattern.matcher(event.getPacket().username).matches()) { | ||
event.setKickReason(kickReason.replace("<username>", event.getPacket().username)); | ||
event.deny(); | ||
} | ||
} | ||
|
||
@Override | ||
public int getPriority() { | ||
return priority; | ||
} | ||
} |