Skip to content

Commit

Permalink
Massive refactors and fixes
Browse files Browse the repository at this point in the history
* Fix sign text corrupts. Do not pass `org.bukkit.block.Sign` around
  since there is no guaranty that it is valid after `org.bukkit.block.Sign#update()`.
* Fix thread-safety issue with `onPlayerChat`.
* Fix gun fires when repairing barrier via sign.
* Fix player was able to change text on sign.
* Fix zombie spawnpoint selection logic.
* Add barrier break and repair progress display.
* Use barrier block count - 1 as max break/fix level.
* Replace RNG with the more modern one.
* And many other low-level fixes, refactors and improvements.

Resolve TheTurkeyDev#97.
  • Loading branch information
skbeh committed Jan 15, 2024
1 parent 7823ce1 commit e39f1b4
Show file tree
Hide file tree
Showing 74 changed files with 1,178 additions and 1,055 deletions.
1 change: 1 addition & 0 deletions API/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ repositories {
}

dependencies {
implementation 'org.jetbrains:annotations:24.1.0'
implementation group: 'org.spigotmc', name: 'spigot-api', version: '1.16.4-R0.1-SNAPSHOT'
}
13 changes: 5 additions & 8 deletions Core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ archivesBaseName = "comzombies-Universal"

repositories {
mavenCentral()
maven { url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
maven { url 'https://oss.sonatype.org/content/repositories/central' }
maven { url 'https://repo.papermc.io/repository/maven-public/' }
maven { url 'https://repo.extendedclip.com/content/repositories/placeholderapi/' }
maven { url 'https://jitpack.io' }
}
Expand All @@ -31,11 +29,10 @@ dependencies {
implementation project(':NMS:1_16_R1')
implementation project(':NMS:1_15_R1')
implementation project(':NMS:1_14_R1')
implementation 'org.jetbrains:annotations:24.1.0'
implementation 'io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT'
compileOnly 'com.github.MilkBowl:VaultAPI:1.7'
implementation group: 'org.spigotmc', name: 'spigot-api', version: '1.16.4-R0.1-SNAPSHOT'
implementation group: 'org.spigotmc', name: 'spigot', version: '1.16.4-R0.1-SNAPSHOT'
implementation 'me.clip:placeholderapi:2.11.1'

implementation 'me.clip:placeholderapi:2.11.5'
}

jar {
Expand Down Expand Up @@ -64,4 +61,4 @@ processResources {
]
duplicatesStrategy = 'INCLUDE'
}
}
}
62 changes: 52 additions & 10 deletions Core/src/main/java/com/theprogrammingturkey/comz/COMZombies.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.block.Sign;
import org.bukkit.command.CommandMap;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.Random;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.random.RandomGenerator;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -45,23 +48,26 @@
*/
public class COMZombies extends JavaPlugin
{
public static final Random rand = new Random();
/**
* Not thread-safe.
*/
public static final RandomGenerator rand;
/**
* Default plugin logger.
*/
public static final Logger log = Logger.getLogger("COM:Z");
/**
* Players currently performing some sort of action or maintenance
*/
public HashMap<Player, BaseAction> activeActions = new HashMap<>();
public final ConcurrentHashMap<Player, BaseAction> activeActions = new ConcurrentHashMap<>();


/**
* Players who are contained in this hash map are in sign edit for a given
* sign, the value that corresponds to the player is the sign that the
* player is editing.
*/
public HashMap<Player, Sign> isEditingASign = new HashMap<>();
public final ConcurrentHashMap<Player, Sign> isEditingASign = new ConcurrentHashMap<>();

/**
* Called when the plugin is reloading to cancel every remove spawn, create
Expand All @@ -73,12 +79,29 @@ public void clearAllSetup()
}

public static final String CONSOLE_PREFIX = "[COM_Zombies] ";
public static final String PREFIX = ChatColor.RED + "[ " + ChatColor.GOLD + ChatColor.ITALIC + "CoM: Zombies" + ChatColor.RED + " ]" + ChatColor.GRAY + " ";
public static final String PREFIX = ChatColor.RED + "[" + ChatColor.GOLD + ChatColor.ITALIC + "CoM: Zombies" + ChatColor.RED + "]" + ChatColor.GRAY + " ";

public static INMSUtil nmsUtil;

public Vault vault;

static {
RandomGenerator rng;
try {
rng = RandomGenerator.of("L64X1024MixRandom");
} catch (IllegalArgumentException e) {
try {
rng = (RandomGenerator) Class.forName("jdk.random.L64X1024MixRandom")
.getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
NoSuchMethodException | ClassNotFoundException ignored) {
e.printStackTrace();
rng = RandomGenerator.of("SplittableRandom");
}
}
rand = rng;
}

public void onEnable()
{
loadVersionSpecificCode();
Expand All @@ -97,11 +120,29 @@ public void onEnable()

registerEvents();

getCommand("zombies").setExecutor(CommandManager.INSTANCE);

log.info(COMZombies.CONSOLE_PREFIX + "has been enabled!");
CommandMap commandMap;
try {
commandMap = Bukkit.getServer().getCommandMap();
} catch (NoSuchMethodError e) {
final Field commandMapField;
try {
commandMapField = Bukkit.getServer().getClass().getDeclaredField("commandMap");
} catch (NoSuchFieldException ex) {
throw new RuntimeException(ex);
}
commandMapField.setAccessible(true);
try {
commandMap = (CommandMap) commandMapField.get(Bukkit.getServer());
} catch (IllegalAccessException ex) {
throw new RuntimeException(ex);
}
}
commandMap.register("zombies", CommandManager.INSTANCE);

GameManager.INSTANCE.loadAllGames();
scheduleTask(() -> {
GameManager.INSTANCE.loadAllGames();
log.info(COMZombies.CONSOLE_PREFIX + "has been enabled!");
});
}

private void loadVersionSpecificCode()
Expand Down Expand Up @@ -196,6 +237,7 @@ public void registerEvents()
m.registerEvents(new PlayerListener(), this);
m.registerEvents(new OnInventoryChangeEvent(), this);
m.registerEvents(new ScopeListener(), this);
m.registerEvents(new BarrierRepairListener(), this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.theprogrammingturkey.comz.game.GameManager;
import com.theprogrammingturkey.comz.util.COMZPermission;
import com.theprogrammingturkey.comz.util.CommandUtil;
import java.util.Locale;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

Expand All @@ -20,7 +21,8 @@ public boolean onCommand(Player player, String[] args)

CommandUtil.sendMessageToPlayer(player, ChatColor.RED + "" + ChatColor.STRIKETHROUGH + "---------------" + ChatColor.DARK_RED + "Arenas" + ChatColor.RED + "" + ChatColor.STRIKETHROUGH + "---------------");
for(Game game : GameManager.INSTANCE.getGames())
CommandUtil.sendMessageToPlayer(player, ChatColor.RED + game.getName() + ": " + ChatColor.GREEN + "Players: " + game.getPlayersInGame().size() + ", Status: " + game.getMode().toString().toLowerCase());
CommandUtil.sendMessageToPlayer(player, ChatColor.RED + game.getName() + ": " + ChatColor.GREEN + "Players: " + game.getPlayersInGame().size() + ", Status: " + game.getMode().toString().toLowerCase(
Locale.ROOT));

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@
import com.theprogrammingturkey.comz.game.Game;
import com.theprogrammingturkey.comz.game.GameManager;
import com.theprogrammingturkey.comz.util.CommandUtil;
import java.util.List;
import java.util.Locale;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import javax.annotation.Nonnull;
import java.util.HashMap;

public class CommandManager implements CommandExecutor
{
public class CommandManager extends Command {

public static final CommandManager INSTANCE = new CommandManager();
private final HashMap<String, SubCommand> commandList = new HashMap<>();
private final HashMap<Player, ZombiesHelpCommand> helpCommand = new HashMap<>();

public CommandManager()
{
public CommandManager() {
super("zombies", "Type /zombies help for more info!", "", List.of("z", "zo", "zom"));
load();
}

Expand Down Expand Up @@ -211,73 +212,59 @@ public void onRemoteCommand(Player player, String[] args)
this.commandList.get(args[0]).onCommand(player, args);
}

@Override
public boolean onCommand(@Nonnull CommandSender sender, Command cmd, @Nonnull String label, @Nonnull String[] args)
{
Player player;
String command = cmd.getName();
if(command.equalsIgnoreCase("zombies"))
{
if(sender instanceof Player)
{
player = (Player) sender;
}
else
{
COMZombies.log.info("You must be in game to issue this command!");
return true;
}
if(args.length <= 0)
{
CommandUtil.sendMessageToPlayer(player, ChatColor.RED + "" + ChatColor.BOLD + "Call of Minecraft: Zombies, By : " + ChatColor.GOLD + "IModZombies4Fun, turkey2349 and smeths!");
CommandUtil.sendMessageToPlayer(player, ChatColor.RED + "" + ChatColor.BOLD + "Call of Minecraft: Zombies, By : " + ChatColor.GOLD + "Turkey2349, IModZombies4Fun and Smeths!");
CommandUtil.sendMessageToPlayer(player, ChatColor.RED + "" + ChatColor.BOLD + " Type /zombies help for a list of commands!");
public boolean execute(@Nonnull CommandSender sender, @Nonnull String label,
@Nonnull String[] args) {
final Player player;
if (sender instanceof Player) {
player = (Player) sender;
} else {
COMZombies.log.info("You must be in game to issue this command!");
return true;
}
if (args.length == 0) {
CommandUtil.sendMessageToPlayer(player,
ChatColor.RED + "" + ChatColor.BOLD + "Call of Minecraft: Zombies, By : " + ChatColor.GOLD
+ "Turkey2349, IModZombies4Fun and Smeths!");
CommandUtil.sendMessageToPlayer(player,
ChatColor.RED + "" + ChatColor.BOLD + " Type /zombies help for a list of commands!");
return true;
} else if (args[0].equalsIgnoreCase("setround")) {
if (!player.isOp()) {
return true;
}
else if(args[0].equalsIgnoreCase("setround"))
{
if(!player.isOp())
return true;
Game arena = GameManager.INSTANCE.getGame(args[1]);
if(arena == null)
return true;
else
{
for(int i = 0; i < Integer.parseInt(args[2]); i++)
arena.nextWave();
CommandUtil.sendMessageToPlayer(player, ChatColor.RED + "Setting wave to: " + ChatColor.GOLD + args[2]);
}
}
else if(args[0].equalsIgnoreCase("version"))
{
CommandUtil.sendMessageToPlayer(player, COMZombies.getPlugin().getDescription().getVersion());
Game arena = GameManager.INSTANCE.getGame(args[1]);
if (arena == null) {
return true;
}
if(args[0].equalsIgnoreCase("help") || args[0].equalsIgnoreCase("h"))
{
ZombiesHelpCommand help;
if(helpCommand.get(player) == null)
{
help = new ZombiesHelpCommand(this, player);
helpCommand.put(player, help);
help.commandIssued(args);
} else {
for (int i = 0; i < Integer.parseInt(args[2]); i++) {
arena.nextWave();
}
else
{
help = helpCommand.get(player);
help.commandIssued(args);
}
return true;
CommandUtil.sendMessageToPlayer(player,
ChatColor.RED + "Setting wave to: " + ChatColor.GOLD + args[2]);
}
if(commandList.containsKey(args[0].toLowerCase()))
{
this.commandList.get(args[0].toLowerCase()).onCommand(player, args);
}
else
{
CommandUtil.sendMessageToPlayer(player, ChatColor.RED + "No such command! Type /zombies help for a list of commands!");
} else if (args[0].equalsIgnoreCase("version")) {
CommandUtil.sendMessageToPlayer(player, COMZombies.getPlugin().getDescription().getVersion());
return true;
}
if (args[0].equalsIgnoreCase("help") || args[0].equalsIgnoreCase("h")) {
ZombiesHelpCommand help;
if (helpCommand.get(player) == null) {
help = new ZombiesHelpCommand(this, player);
helpCommand.put(player, help);
help.commandIssued(args);
} else {
help = helpCommand.get(player);
help.commandIssued(args);
}
return true;
}
return false;
if (commandList.containsKey(args[0].toLowerCase(Locale.ROOT))) {
this.commandList.get(args[0].toLowerCase(Locale.ROOT)).onCommand(player, args);
} else {
CommandUtil.sendMessageToPlayer(player,
ChatColor.RED + "No such command! Type /zombies help for a list of commands!");
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public boolean onCommand(Player player, String[] args)
action.cancelAction();

game.setEnabled();
game.signManager.updateGame();
CommandUtil.sendMessageToPlayer(player, ChatColor.GREEN + "Arena " + game.getName() + " has been enabled!");
return true;
}
Expand Down
Loading

0 comments on commit e39f1b4

Please sign in to comment.