This repository has been archived by the owner on Mar 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
308 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package cat.nyaa.yasui; | ||
|
||
import net.md_5.bungee.api.chat.BaseComponent; | ||
import net.md_5.bungee.api.chat.ClickEvent; | ||
import net.md_5.bungee.api.chat.HoverEvent; | ||
import net.md_5.bungee.api.chat.TextComponent; | ||
import org.bukkit.block.Block; | ||
import org.bukkit.entity.Entity; | ||
|
||
class ChunkCoordinate { | ||
|
||
private final int x; | ||
|
||
private final int z; | ||
|
||
private ChunkCoordinate(int x, int z) { | ||
this.x = x; | ||
this.z = z; | ||
} | ||
|
||
public static ChunkCoordinate of(int x, int z) { | ||
return new ChunkCoordinate(x, z); | ||
} | ||
|
||
public static ChunkCoordinate of(Block b) { | ||
return new ChunkCoordinate(b.getX() >> 4, b.getZ() >> 4); | ||
} | ||
|
||
public static ChunkCoordinate of(Entity b) { | ||
return new ChunkCoordinate(b.getLocation().getBlockX() >> 4, b.getLocation().getBlockZ() >> 4); | ||
} | ||
|
||
public ChunkCoordinate add(ChunkCoordinate another) { | ||
return new ChunkCoordinate(x + another.x, z + another.z); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return (x << 16 + x >> 16) ^ z; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o instanceof ChunkCoordinate) { | ||
ChunkCoordinate pair = (ChunkCoordinate) o; | ||
return (pair.x == x) && (pair.z == z); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Chunk " + x + ", " + z; | ||
} | ||
|
||
public BaseComponent getComponent() { | ||
TextComponent component = new TextComponent(I18n.format("user.chunk.coord", x, z)); | ||
component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[]{new TextComponent(I18n.format("user.chunk.hover", x << 4, (x << 4) + 16, z << 4, (z << 4) + 16))})); | ||
component.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, I18n.format("user.chunk.tp", (x << 4) + 8, (z << 4) + 8))); | ||
return component; | ||
} | ||
|
||
public int getX() { | ||
return x; | ||
} | ||
|
||
public int getZ() { | ||
return z; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package cat.nyaa.yasui; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.Chunk; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.EventPriority; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.block.BlockEvent; | ||
import org.bukkit.event.block.BlockPhysicsEvent; | ||
import org.bukkit.event.block.BlockRedstoneEvent; | ||
|
||
import java.util.Map; | ||
|
||
public class ProfilerListener implements Listener { | ||
final private Yasui plugin; | ||
|
||
public ProfilerListener(Yasui pl) { | ||
plugin = pl; | ||
Bukkit.getPluginManager().registerEvents(this, plugin); | ||
} | ||
|
||
@EventHandler(priority = EventPriority.MONITOR) | ||
public void onBlockPhysicsEvent(BlockPhysicsEvent event) { | ||
ProfilerStatsMonitor.ChunkStat stat = getStat(event); | ||
if (stat == null) return; | ||
stat.incPhysics(); | ||
} | ||
|
||
@EventHandler(priority = EventPriority.MONITOR) | ||
public void onBlockRedstoneEvent(BlockRedstoneEvent event) { | ||
ProfilerStatsMonitor.ChunkStat stat = getStat(event); | ||
if (stat == null) return; | ||
stat.incRedstone(); | ||
} | ||
|
||
private ProfilerStatsMonitor.ChunkStat getStat(BlockEvent event) { | ||
Chunk chunk = event.getBlock().getChunk(); | ||
if (chunk == null) return null; | ||
Map<ChunkCoordinate, ProfilerStatsMonitor.ChunkStat> currentRedstoneStats = plugin.profilerStatsMonitor.currentRedstoneStats(chunk.getWorld()); | ||
return currentRedstoneStats.computeIfAbsent(ChunkCoordinate.of(chunk.getX(), chunk.getZ()), (k) -> new ProfilerStatsMonitor.ChunkStat()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package cat.nyaa.yasui; | ||
|
||
import cat.nyaa.nyaacore.Pair; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.World; | ||
import org.bukkit.scheduler.BukkitRunnable; | ||
|
||
import java.util.*; | ||
|
||
public class ProfilerStatsMonitor extends BukkitRunnable { | ||
private final Yasui plugin; | ||
private final long startTickMillis; | ||
private final long startTickNano; | ||
private long currentTickMillis; | ||
private Map<World, Deque<Pair<Long, Map<ChunkCoordinate, ChunkStat>>>> stats = new HashMap<>(600); | ||
|
||
public ProfilerStatsMonitor(Yasui pl) { | ||
plugin = pl; | ||
plugin.profilerStatsMonitor = this; | ||
startTickMillis = System.currentTimeMillis(); | ||
startTickNano = System.nanoTime(); | ||
runTaskTimer(plugin, 0, 0); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
currentTickMillis = startTickMillis + (System.nanoTime() - startTickNano) / 1000000; | ||
List<World> worlds = Bukkit.getWorlds(); | ||
for (World world : worlds) { | ||
Deque<Pair<Long, Map<ChunkCoordinate, ChunkStat>>> worldStats = stats.computeIfAbsent(world, ignored -> new ArrayDeque<>()); | ||
if (worldStats.size() == 600) { | ||
worldStats.poll(); | ||
} | ||
Pair<Long, Map<ChunkCoordinate, ChunkStat>> rsCounterNode = Pair.of(currentTickMillis, new LinkedHashMap<>()); | ||
worldStats.add(rsCounterNode); | ||
} | ||
for (World world : stats.keySet()) { | ||
if (!worlds.contains(world)) { | ||
stats.remove(world); | ||
} | ||
} | ||
} | ||
|
||
public Map<ChunkCoordinate, ChunkStat> currentRedstoneStats(World world) { | ||
return getRedstoneStats(world).getLast().getValue(); | ||
} | ||
|
||
public Deque<Pair<Long, Map<ChunkCoordinate, ChunkStat>>> getRedstoneStats(World world) { | ||
return stats.get(world); | ||
} | ||
|
||
public long getCurrentTickMillis() { | ||
return currentTickMillis; | ||
} | ||
|
||
static class ChunkStat { | ||
private int redstone = 0; | ||
private int physics = 0; | ||
|
||
public int incRedstone() { | ||
return ++redstone; | ||
} | ||
|
||
public int incPhysics() { | ||
return ++physics; | ||
} | ||
|
||
public void add(ChunkStat value) { | ||
redstone += value.redstone; | ||
physics += value.physics; | ||
} | ||
|
||
public int getPhysics() { | ||
return physics; | ||
} | ||
|
||
public int getRedstone() { | ||
return redstone; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,6 @@ commands: | |
yasui: | ||
permissions: | ||
yasui.admin: | ||
default: op | ||
yasui.profiler: | ||
default: op |