Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
remove disable_seconds from rule
Browse files Browse the repository at this point in the history
  • Loading branch information
cyilin committed Mar 28, 2019
1 parent 523737a commit e09f1ac
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 28 deletions.
23 changes: 19 additions & 4 deletions src/main/java/cat/nyaa/yasui/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ public void commandChunkEvents(CommandSender sender, Arguments args) {
});
stat.entrySet().stream().sorted(Comparator.comparing(e -> -e.getValue().getRedstone())).limit(plugin.config.profiler_event_chunk_count).forEach(
e -> new Message("")
.append(e.getKey().getComponent())
.append(I18n.format("user.chunk.total", e.getValue().getPhysics() + e.getValue().getRedstone()))
.append(I18n.format("user.chunk.events", e.getValue().getRedstone(), e.getValue().getPhysics()))
.send(sender)
.append(e.getKey().getComponent())
.append(I18n.format("user.chunk.total", e.getValue().getPhysics() + e.getValue().getRedstone()))
.append(I18n.format("user.chunk.events", e.getValue().getRedstone(), e.getValue().getPhysics()))
.send(sender)
);
});
}
Expand Down Expand Up @@ -129,6 +129,21 @@ public void commandChunkEntities(CommandSender sender, Arguments args) {
});
}

@SubCommand(value = "clearredstonehistory", permission = "yasui.admin")
public void clearHistory(CommandSender sender, Arguments args) {
int i = 0;
for (ChunkCoordinate id : Utils.getChunks(asPlayer(sender).getLocation().getChunk(), args.nextInt())) {
i++;
plugin.redstoneListener.history.remove(id);
plugin.redstoneListener.disabledChunks.remove(id);
if (plugin.redstoneListener.redstoneMonitorTasks.containsKey(id)) {
plugin.redstoneListener.redstoneMonitorTasks.get(id).cancel();
plugin.redstoneListener.redstoneMonitorTasks.remove(id);
}
}
msg(sender, "user.redstone.clearhistory", i);
}

private World getWorld(CommandSender sender, Arguments args) {
World world;
if (args.top() == null) {
Expand Down
37 changes: 22 additions & 15 deletions src/main/java/cat/nyaa/yasui/RedstoneListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
public class RedstoneListener extends BukkitRunnable implements Listener {
private final Yasui plugin;
public Integer redstoneLimitMaxChange = -1;
public Integer redstoneLimitDisableSeconds = 0;
public Integer redstoneLimitDisableRadius = 0;
public Map<ChunkCoordinate, LoadingCache<Integer, Integer>> history;
public Map<ChunkCoordinate, Integer> disabledChunks;
Expand All @@ -41,8 +40,8 @@ public RedstoneListener(Yasui pl) {
public void onClickButton(PlayerInteractEvent event) {
if (event.hasBlock() && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
Block b = event.getClickedBlock();
if (b != null && disabledChunks.getOrDefault(ChunkCoordinate.of(b), 0) > time && (b.getType() == Material.LEVER || Tag.BUTTONS.isTagged(b.getType()))) {
event.getPlayer().sendMessage(I18n.format("user.redstone.disabled_here", disabledChunks.get(ChunkCoordinate.of(b)) - time));
if (b != null && disabledChunks.containsKey(ChunkCoordinate.of(b)) && (b.getType() == Material.LEVER || Tag.BUTTONS.isTagged(b.getType()))) {
event.getPlayer().sendMessage(I18n.format("user.redstone.disabled_here", disabledChunks.get(ChunkCoordinate.of(b)), redstoneLimitMaxChange));
}
}
}
Expand All @@ -51,7 +50,7 @@ public void onClickButton(PlayerInteractEvent event) {
public void redstoneChange(BlockRedstoneEvent event) {
if (event.getOldCurrent() < event.getNewCurrent()) {
onPistonMove(event.getBlock(), 1);
if (disabledChunks.getOrDefault(ChunkCoordinate.of(event.getBlock()), 0) >= time) {
if (disabledChunks.containsKey(ChunkCoordinate.of(event.getBlock()))) {
event.setNewCurrent(event.getOldCurrent());
}
}
Expand All @@ -60,7 +59,7 @@ public void redstoneChange(BlockRedstoneEvent event) {
@EventHandler(priority = EventPriority.LOWEST)
public void onBlockPistonExtend(BlockPistonExtendEvent event) {
onPistonMove(event.getBlock(), event.getBlocks().size());
if (disabledChunks.getOrDefault(ChunkCoordinate.of(event.getBlock()), 0) >= time) {
if (disabledChunks.containsKey(ChunkCoordinate.of(event.getBlock()))) {
event.setCancelled(true);
}
}
Expand All @@ -74,9 +73,7 @@ private void onPistonMove(Block block, int blocks) {
if (worlds.contains(block.getWorld().getName())) {
ChunkCoordinate id = ChunkCoordinate.of(block);
if (!redstoneMonitorTasks.containsKey(id)) {
RedstoneMonitor task = new RedstoneMonitor(id);
task.runTaskTimer(plugin, 20, plugin.config.redstone_limit_check_interval_tick);
redstoneMonitorTasks.put(id, task);
addTask(id);
}
if (!history.containsKey(id)) {
history.put(id, this.newCache());
Expand All @@ -86,6 +83,14 @@ private void onPistonMove(Block block, int blocks) {
}
}

private void addTask(ChunkCoordinate id) {
if (!redstoneMonitorTasks.containsKey(id)) {
RedstoneMonitor task = new RedstoneMonitor(id);
task.runTaskTimer(plugin, 20, plugin.config.redstone_limit_check_interval_tick);
redstoneMonitorTasks.put(id, task);
}
}

private LoadingCache<Integer, Integer> newCache() {
return CacheBuilder.newBuilder()
.expireAfterWrite(plugin.config.redstone_limit_time_range + 5, TimeUnit.SECONDS)
Expand All @@ -102,23 +107,24 @@ public void run() {
time = (int) (System.currentTimeMillis() / 1000);
}

public void disableRedstone(Chunk chunk, int radius, int seconds) {
public void disableRedstone(Chunk chunk, int radius, int redstoneActivity) {
List<ChunkCoordinate> list = Utils.getChunks(chunk, radius);
for (ChunkCoordinate id : list) {
disabledChunks.put(id, time + seconds);
disabledChunks.put(id, redstoneActivity);
addTask(id);
}
if (plugin.config.redstone_limit_log) {
plugin.getLogger().info(I18n.format("log.redstone", chunk.getWorld().getName(), chunk.getX(), chunk.getZ(), radius));
}
Collection<Entity> players = chunk.getWorld().getNearbyEntities(chunk.getBlock(8, 128, 8).getLocation(), (radius + 4) * 16, 128, (radius + 4) * 16, entity -> entity instanceof Player);
for (Entity p : players) {
if (!p.isDead()) {
p.sendMessage(I18n.format("user.redstone.msg", chunk.getX(), chunk.getZ(), radius, seconds));
p.sendMessage(I18n.format("user.redstone.msg", chunk.getX(), chunk.getZ(), radius, redstoneActivity));
}
}
}

public long getHistory(ChunkCoordinate id, int seconds) {
public int getHistory(ChunkCoordinate id, int seconds) {
int total = 0;
LoadingCache<Integer, Integer> ch = history.get(id);
if (ch != null) {
Expand All @@ -140,10 +146,11 @@ public RedstoneMonitor(ChunkCoordinate id) {
public void run() {
if (!disabledChunks.containsKey(id)) {
if (redstoneLimitMaxChange >= 0) {
if (getHistory(id, plugin.config.redstone_limit_time_range) > redstoneLimitMaxChange) {
int total = getHistory(id, plugin.config.redstone_limit_time_range);
if (total > redstoneLimitMaxChange) {
World w = Bukkit.getWorld(id.getWorld());
if (w != null) {
disableRedstone(w.getChunkAt(id.getX(), id.getZ()), redstoneLimitDisableRadius, redstoneLimitDisableSeconds);
disableRedstone(w.getChunkAt(id.getX(), id.getZ()), redstoneLimitDisableRadius, total);
return;
}
}
Expand All @@ -154,7 +161,7 @@ public void run() {
redstoneMonitorTasks.remove(id);
}
} else {
if (disabledChunks.get(id) < time) {
if (disabledChunks.get(id) <= redstoneLimitMaxChange) {
disabledChunks.remove(id);
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/cat/nyaa/yasui/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public class Rule implements ISerializable {
public String entity_limit_disable;
@Serializable(name = "redstone_limit.disable_radius")
public String redstone_limit_disable_radius;
@Serializable(name = "redstone_limit.disable_seconds")
public String redstone_limit_disable_seconds;
@Serializable(name = "redstone_limit.max_change")
public String redstone_limit_max_change;
@Serializable
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/cat/nyaa/yasui/TPSMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public void runRule(Rule rule, World world) {
if (redstone_max_change.intValue() >= 0) {
plugin.redstoneListener.worlds.add(world.getName());
plugin.redstoneListener.redstoneLimitMaxChange = redstone_max_change.intValue();
plugin.redstoneListener.redstoneLimitDisableSeconds = eval(rule.redstone_limit_disable_seconds, world).intValue();
plugin.redstoneListener.redstoneLimitDisableRadius = eval(rule.redstone_limit_disable_radius, world).intValue();
} else {
plugin.redstoneListener.worlds.remove(world.getName());
Expand All @@ -108,8 +107,7 @@ public void runRule(Rule rule, World world) {
}
if (redstone_max_change != null) {
msg = msg.replaceAll("\\{redstone_max_change}", String.valueOf(redstone_max_change.intValue()))
.replaceAll("\\{redstone_disable_radius}", String.valueOf(plugin.redstoneListener.redstoneLimitDisableRadius))
.replaceAll("\\{redstone_disable_seconds}", String.valueOf(plugin.redstoneListener.redstoneLimitDisableSeconds));
.replaceAll("\\{redstone_disable_radius}", String.valueOf(plugin.redstoneListener.redstoneLimitDisableRadius));
}
new Message(ChatColor.translateAlternateColorCodes('&', msg)).broadcast(rule.messageType, p -> (p.getWorld().equals(world)));
}
Expand All @@ -136,8 +134,7 @@ public BigDecimal eval(List<BigDecimal> parameters) {
.with("world_players", new BigDecimal(world.getPlayers().size()))
.with("world_living_entities", new BigDecimal(world.getLivingEntities().size()))
.with("redstone_max_change", new BigDecimal(plugin.redstoneListener.redstoneLimitMaxChange))
.with("redstone_disable_radius", new BigDecimal(plugin.redstoneListener.redstoneLimitDisableRadius))
.with("redstone_disable_seconds", new BigDecimal(plugin.redstoneListener.redstoneLimitDisableSeconds));
.with("redstone_disable_radius", new BigDecimal(plugin.redstoneListener.redstoneLimitDisableRadius));
return exp.eval();
}
return null;
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/lang/en_US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ user:
error:
bad_world: Unknown world %s
redstone:
disabled_here: "redstone has been disabled at this chunk (remaining time: %ds)"
msg: "redstone disabled at chunk X: %d Y: %d radius %d for %ds"
disabled_here: "redstone has been disabled at this chunk (redstone activity of this chunk is %d, current allowed is %d)"
msg: "redstone disabled at chunk X: %d Y: %d radius %d (redstone activity: %d)"
clearhistory: "success cleared history of %d chunks"
log:
redstone: "redstone disabled at [%s, %d, %d] radius: %d "
internal:
Expand Down

0 comments on commit e09f1ac

Please sign in to comment.