Skip to content

Commit

Permalink
Add the project
Browse files Browse the repository at this point in the history
  • Loading branch information
stonar96 committed Sep 20, 2018
1 parent e46f5e7 commit 3640df8
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ buildNumber.properties

# Avoid ignoring Maven wrapper jar file (.jar files are usually ignored)
!/.mvn/wrapper/maven-wrapper.jar

# IDE
/.settings/
/lib/
/.classpath
/.project
19 changes: 19 additions & 0 deletions pom.xml
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 src/main/java/com/vanillage/validusername/ValidUsername.java
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");
}
}
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;
}
}

0 comments on commit 3640df8

Please sign in to comment.