Skip to content

Commit

Permalink
Performance improvements (#36)
Browse files Browse the repository at this point in the history
* Switch from nanoTime to millis

* Allow /repairreport from console
  • Loading branch information
TylerS1066 authored Jun 9, 2024
1 parent f15cb63 commit d5b75eb
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private static void loadConfig(FileConfiguration config) {
Config.Locale = config.getString("Locale", "en");
I18nSupport.init();
Config.RepairTicksPerBlock = config.getInt("RepairTicksPerBlock", 0);
Config.RepairMaxTickTime = config.getLong("RepairMaxTickTime", 5000000);
Config.RepairMaxTickTime = config.getLong("RepairMaxTickTime", 5000000) / 1000000;
Config.RepairMaxBlocksPerTick = config.getInt("RepairMaxBlocksPerTick", 2);
Config.RepairMoneyPerBlock = config.getDouble("RepairMoneyPerBlock", 0.0);
Config.RepairMaxPercent = config.getDouble("RepairMaxPercent", 50);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public class RepairManager extends BukkitRunnable {

@Override
public void run() {
long start = System.nanoTime();
long start = System.currentTimeMillis();

Set<Repair> completed = new HashSet<>();
Set<Repair> executed = new HashSet<>();
while (System.nanoTime() - start < Config.RepairMaxTickTime) {
while (System.currentTimeMillis() - start < Config.RepairMaxTickTime) {
Repair repair = repairs.peek();
if (repair == null)
break; // No repairs, jump out
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,40 @@ public class RepairReportCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s,
@NotNull String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage(
MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Repair - Must Be Player"));
return true;
}
Player player = (Player) sender;

if (!player.hasPermission("movecraft.repair.repairreport")) {
player.sendMessage(MOVECRAFT_COMMAND_PREFIX + net.countercraft.movecraft.localisation.I18nSupport
if (!sender.hasPermission("movecraft.repair.repairreport")) {
sender.sendMessage(MOVECRAFT_COMMAND_PREFIX + net.countercraft.movecraft.localisation.I18nSupport
.getInternationalisedString("Insufficient Permissions"));
return true;
}

TopicPaginator pageinator = new TopicPaginator(
TopicPaginator paginator = new TopicPaginator(
I18nSupport.getInternationalisedString("Repair - Ongoing Repairs"));

int page = 1; // Default page
if (args.length > 0) {
try {
page = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
player.sendMessage(MOVECRAFT_COMMAND_PREFIX + net.countercraft.movecraft.localisation.I18nSupport
sender.sendMessage(MOVECRAFT_COMMAND_PREFIX + net.countercraft.movecraft.localisation.I18nSupport
.getInternationalisedString("Paginator - Invalid Page") + " \"" + args[0] + "\"");
return true;
}
}

for (Repair repair : MovecraftRepair.getInstance().getRepairManager().get()) {
Player p = Bukkit.getPlayer(repair.getPlayerUUID());
pageinator.addLine(repair.getName() + " " + (p == null ? "None" : p.getName()) + " @ " + repair.remaining()
paginator.addLine(repair.getName() + " " + (p == null ? "None" : p.getName()) + " @ " + repair.remaining()
+ ", " + (repair.size() - repair.remaining()) + " / " + repair.size());
}

if (pageinator.isEmpty()) {
player.sendMessage(
if (paginator.isEmpty()) {
sender.sendMessage(
MOVECRAFT_COMMAND_PREFIX + I18nSupport.getInternationalisedString("Repair - No Repairs"));
return true;
}

if (!pageinator.isInBounds(page)) {
player.sendMessage(MOVECRAFT_COMMAND_PREFIX +
if (!paginator.isInBounds(page)) {
sender.sendMessage(MOVECRAFT_COMMAND_PREFIX +
net.countercraft.movecraft.localisation.I18nSupport
.getInternationalisedString("Paginator - Page Number")
+ " " + page + " " +
Expand All @@ -67,8 +60,8 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
return true;
}

for (String line : pageinator.getPage(page)) {
player.sendMessage(line);
for (String line : paginator.getPage(page)) {
sender.sendMessage(line);
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Config {
public static String Locale = "en";

public static int RepairTicksPerBlock = 0;
public static long RepairMaxTickTime = 5000000;
public static long RepairMaxTickTime = 5;
public static int RepairMaxBlocksPerTick = 2;
public static double RepairMoneyPerBlock = 0.0;
public static double RepairMaxPercent = 50.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ProtoRepair(UUID playerUUID, String name, RepairQueue queue, RepairCounte
this.materials = materials;
this.origin = origin;
this.damagedBlockCount = damagedBlockCount;
calculationTime = System.nanoTime();
calculationTime = System.currentTimeMillis();
valid = true;
}

Expand All @@ -71,7 +71,7 @@ public MovecraftLocation getOrigin() {
}

public boolean isInvalid() {
return (System.nanoTime() - calculationTime > 5000000000L) || !valid; // 5 seconds
return (System.currentTimeMillis() - calculationTime > 5000) || !valid; // 5 seconds
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Repair(UUID playerUUID, String name, double cost, RepairQueue queue) {
this.cost = cost;
this.queue = queue;
this.size = queue.size();
lastExecution = System.nanoTime();
lastExecution = System.currentTimeMillis();
start = System.currentTimeMillis();
}

Expand Down Expand Up @@ -53,7 +53,7 @@ public int remaining() {
}

public boolean run() {
double elapsedTicks = (System.nanoTime() - lastExecution) * 20.0 / 1000000000;
double elapsedTicks = (System.currentTimeMillis() - lastExecution) * 20.0 / 1000;
int placedBlocks = 0;

while (elapsedTicks > Config.RepairTicksPerBlock && placedBlocks <= Config.RepairMaxBlocksPerTick) {
Expand All @@ -67,7 +67,7 @@ public boolean run() {
}

if (placedBlocks > 0) {
lastExecution = System.nanoTime();
lastExecution = System.currentTimeMillis();
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public class WarfareUtils extends BukkitRunnable {

@Override
public void run() {
long start = System.nanoTime();
long start = System.currentTimeMillis();

while (System.nanoTime() - start < Config.RepairMaxTickTime && !tasks.isEmpty()) {
while (System.currentTimeMillis() - start < Config.RepairMaxTickTime && !tasks.isEmpty()) {
tasks.poll().execute();
}
}
Expand Down

0 comments on commit d5b75eb

Please sign in to comment.