HostnameValidator: allow multiple addresses

This commit is contained in:
Bixilon 2020-10-10 23:42:18 +02:00
parent c66dc4e8b9
commit 6e27430994
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 20 additions and 12 deletions

View File

@ -149,7 +149,7 @@ public class ChatEvent extends EventListener {
@EventHandler(priority = Priorities.HIGHEST)
public void onChatMessageReceiving(ChatMessageReceivingEvent event) {
if (event.getMessage().getMessage().contains("Bixilon")) {
MinosoftExampleMod.getInstance().getLogger().game("Bixilon is awful, suppressing this potential bad chat message!");
MinosoftExampleMod.getInstance().getLogger().info("Bixilon wrote a potential bad chat message. Suppressing it!");
event.setCancelled(true);
}
}
@ -163,5 +163,5 @@ public class ChatEvent extends EventListener {
}
}
```
The following code would suppress messages containing the word "Bixilon" and if you write "jeb_" into the chat, the message's text will be "jeb_ is awesome".
The following code would suppress messages containing the word "Bixilon" and if you write "jeb_ is stupid" into the chat, the message's text will be "jeb_ is awesome".
To see a list of all events look into `de.bixilon.minosoft.modding.event.events`. There is also a javadoc.

View File

@ -55,7 +55,7 @@ public class EventManager {
public void registerConnectionListener(EventListener listener, ServerAddressValidator... addresses) {
if (addresses.length == 0) {
throw new RuntimeException("You must provide at least one server address or use global events!");
throw new RuntimeException("You must provide at least one address validator or use global events!");
}
HashSet<ServerAddressValidator> serverAddresses = new HashSet<>(Arrays.asList(addresses));
specificEventListeners.put(serverAddresses, getEventMethods(listener));

View File

@ -15,29 +15,37 @@ package de.bixilon.minosoft.modding.event.address;
import de.bixilon.minosoft.util.ServerAddress;
public class HostnameValidator implements ServerAddressValidator {
private final String hostname;
import java.util.Arrays;
import java.util.HashSet;
public HostnameValidator(String hostname) {
this.hostname = hostname;
public class HostnameValidator implements ServerAddressValidator {
private final HashSet<String> hostnames;
public HostnameValidator(String... hostnames) {
this(new HashSet<>(Arrays.asList(hostnames)));
}
public HostnameValidator(HashSet<String> hostnames) {
this.hostnames = new HashSet<>();
hostnames.forEach((hostname) -> this.hostnames.add(hostname.toLowerCase()));
}
@Override
public boolean check(ServerAddress address) {
return this.hostname.equalsIgnoreCase(address.getHostname());
return this.hostnames.contains(address.getHostname().toLowerCase());
}
public String getHostname() {
return hostname;
public HashSet<String> getHostnames() {
return hostnames;
}
@Override
public int hashCode() {
return hostname.hashCode();
return hostnames.hashCode();
}
@Override
public boolean equals(Object obj) {
return hostname.equals(obj);
return hostnames.equals(obj);
}
}