Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored code, works on latest version (1.20.2) #5

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.victorgamer15</groupId>
<artifactId>BrandBlocker</artifactId>
<version>1.5</version>
<version>1.6</version>
<packaging>jar</packaging>

<name>BrandBlocker</name>
Expand Down
77 changes: 38 additions & 39 deletions src/main/java/me/victorgamer15/brandblocker/BrandBlocker.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,33 @@
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.messaging.Messenger;
import org.bukkit.plugin.messaging.PluginMessageListener;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Objects;

public final class BrandBlocker extends JavaPlugin implements PluginMessageListener , Listener {
public class BrandBlocker extends JavaPlugin implements PluginMessageListener, Listener {

String prefix;
public String prefix;
public final String version = Bukkit.getBukkitVersion().split("-")[0].split("\\.")[1];
public HashMap<String, String> player_brands = new HashMap<>();

@Override
public void onEnable() {
String[] div = Bukkit.getBukkitVersion().split("-")[0].split("\\.");
String version = div[1];
saveDefaultConfig();
prefix = ChatColor.translateAlternateColorCodes('&', getConfig().getString("prefix"));
getLogger().info("Server running version 1."+version);

if (Integer.parseInt(version) < 13) {
Messenger messenger = Bukkit.getMessenger();
messenger.registerIncomingPluginChannel(this, "MC|Brand", this);
Bukkit.getMessenger().registerIncomingPluginChannel(this, "MC|Brand", this);
getLogger().info("Registered 1.12- listener");
} else {
Messenger messenger = Bukkit.getMessenger();
messenger.registerIncomingPluginChannel(this, "minecraft:brand", this);
Bukkit.getMessenger().registerIncomingPluginChannel(this, "minecraft:brand", this);
getLogger().info("Registered 1.13+ listener");
}
prefix = ChatColor.translateAlternateColorCodes('&', getConfig().getString("prefix"));
saveDefaultConfig();

Bukkit.getServer().getPluginManager().registerEvents(this, this);
}

Expand Down Expand Up @@ -75,6 +74,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
} else if (args[0].equalsIgnoreCase("reload")) {
if (sender.hasPermission("brandblocker.usage")) {
reloadConfig();
prefix = ChatColor.translateAlternateColorCodes('&', getConfig().getString("prefix"));
sender.sendMessage(prefix+ChatColor.translateAlternateColorCodes('&', getConfig().getString("config-reload")));
} else {
sender.sendMessage(prefix+ChatColor.translateAlternateColorCodes('&', getConfig().getString("no-permission")));
Expand All @@ -86,40 +86,39 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
return false;
}

HashMap<String, String> player_brands = new HashMap<>();

@Override
public void onPluginMessageReceived(String channel, Player p, byte[] msg) {
String brand = new String(msg, StandardCharsets.UTF_8).substring(1);
final String brand = new String(msg, StandardCharsets.UTF_8).substring(1);
final Iterator<String> iterator = getConfig().getStringList("blocked-brands").iterator();

player_brands.put(p.getName(), brand);
if (!getConfig().getBoolean("enable")) return;
if (getConfig().getBoolean("geyser-support") && p.getName().contains(Objects.requireNonNull(getConfig().getString("geyser-prefix")))) return;
if (getConfig().getString("mode", "blacklist").equals("blacklist")) {
Iterator<String> iterator = getConfig().getStringList("blocked-brands").iterator();
while (iterator.hasNext()) {
String str = iterator.next();
if (brand.toLowerCase().contains(str.toLowerCase())) {
if(p.hasPermission("brandblocker.bypass")) return;
String kickMsg = getConfig().getString("kick-message");
assert kickMsg != null;
p.kickPlayer(ChatColor.translateAlternateColorCodes('&', kickMsg));
getLogger().info(getConfig().getString("console-log").replace("%player%", p.getName()).replace("%brand%", brand));
return;

switch (getConfig().getString("mode")) {
case "blacklist":
while (iterator.hasNext()) {
String str = iterator.next();
if (str.equalsIgnoreCase(brand)) {
if(p.hasPermission("brandblocker.bypass")) return;
String kickMsg = getConfig().getString("kick-message");
p.kickPlayer(ChatColor.translateAlternateColorCodes('&', kickMsg));
getLogger().info(getConfig().getString("console-log").replace("%player%", p.getName()).replace("%brand%", brand));
return;
}
}
}
} else if (getConfig().getString("mode", "whitelist").equals("whitelist")) {
Iterator<String> iterator = getConfig().getStringList("blocked-brands").iterator();
while (iterator.hasNext()) {
String str = iterator.next();
if (brand.toLowerCase().contains(str.toLowerCase()))
return;
}
if(p.hasPermission("brandblocker.bypass")) return;
String kickMsg = getConfig().getString("kick-message");
assert kickMsg != null;
p.kickPlayer(ChatColor.translateAlternateColorCodes('&', kickMsg));
getLogger().info(getConfig().getString("console-log").replace("%player%", p.getName()).replace("%brand%", brand));
return;
break;
case "whitelist":
while (iterator.hasNext()) {
String str = iterator.next();
if (str.equalsIgnoreCase(brand))
return;
}
if(p.hasPermission("brandblocker.bypass")) return;
String kickMsg = getConfig().getString("kick-message");
p.kickPlayer(ChatColor.translateAlternateColorCodes('&', kickMsg));
getLogger().info(getConfig().getString("console-log").replace("%player%", p.getName()).replace("%brand%", brand));
break;
}
}

Expand Down