SteamWar/BauSystem2.0
Archiviert
12
0

Improve Loadtimer Listener Performance

Signed-off-by: Chaoscaot <chaoscaot444@gmail.com>
Dieser Commit ist enthalten in:
Chaoscaot 2021-05-30 22:34:49 +02:00
Ursprung 8dbb9cb940
Commit 4d8cf8a125
2 geänderte Dateien mit 106 neuen und 44 gelöschten Zeilen

Datei anzeigen

@ -25,20 +25,18 @@ import de.steamwar.bausystem.features.tpslimit.TPSUtils;
import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.utils.RegionExtensionType; import de.steamwar.bausystem.region.utils.RegionExtensionType;
import de.steamwar.bausystem.region.utils.RegionType; import de.steamwar.bausystem.region.utils.RegionType;
import lombok.Getter;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.boss.BarColor; import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle; import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar; import org.bukkit.boss.BossBar;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
@ -49,13 +47,14 @@ import java.util.concurrent.atomic.AtomicInteger;
public class Loadtimer implements Listener { public class Loadtimer implements Listener {
@Getter
private static final Map<Region, Loadtimer> timers = new HashMap<>(); private static final Map<Region, Loadtimer> timers = new HashMap<>();
private final Region region; private final Region region;
private final Map<Player, Integer> tntPlaced = new HashMap<>(); private final Map<Player, Integer> tntPlaced = new HashMap<>();
private final BukkitTask task; private final BukkitTask task;
private final Map<Player, BossBar> bars = new HashMap(); private final Map<Player, BossBar> bars = new HashMap<>();
private Stage stage; private Stage stage;
private boolean finishOnActive = true; private final boolean finishOnActive;
private long start = -1; private long start = -1;
private long activate = -1; private long activate = -1;
@ -68,11 +67,6 @@ public class Loadtimer implements Listener {
this.stage = Stage.WAITING; this.stage = Stage.WAITING;
Bukkit.getPluginManager().registerEvents(this, BauSystem.getInstance()); Bukkit.getPluginManager().registerEvents(this, BauSystem.getInstance());
task = Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> { task = Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), () -> {
Bukkit.getOnlinePlayers().forEach(player -> {
if (!Region.getRegion(player.getLocation()).equals(region) && bars.containsKey(player)) {
bars.remove(player).removeAll();
}
});
if (stage == Stage.COUNTING) { if (stage == Stage.COUNTING) {
long timeSinceStart = TPSUtils.currentTick.get() - start; long timeSinceStart = TPSUtils.currentTick.get() - start;
long timeSinceHalf = timeSinceStart / 2; long timeSinceHalf = timeSinceStart / 2;
@ -97,6 +91,8 @@ public class Loadtimer implements Listener {
bar.setTitle(BauSystem.MESSAGE.parse(message, player, objects)); bar.setTitle(BauSystem.MESSAGE.parse(message, player, objects));
bar.setColor(color); bar.setColor(color);
bar.setProgress(progress); bar.setProgress(progress);
} else if (bars.containsKey(player)) {
bars.remove(player).removeAll();
} }
}); });
} }
@ -121,40 +117,32 @@ public class Loadtimer implements Listener {
}); });
} }
@EventHandler public void onTntPlace(BlockPlaceEvent event) {
public void onBlockPlace(BlockPlaceEvent event) { if (stage == Stage.WAITING) {
if (Region.getRegion(event.getBlock().getLocation()).equals(region) && event.getBlockPlaced().getType() == Material.TNT) { this.stage = Stage.COUNTING;
if (stage == Stage.WAITING) { this.start = TPSUtils.currentTick.get();
this.stage = Stage.COUNTING; }
this.start = TPSUtils.currentTick.get();
}
if (stage == Stage.COUNTING) { if (stage == Stage.COUNTING) {
tntPlaced.put(event.getPlayer(), tntPlaced.getOrDefault(event.getPlayer(), 0) + 1); tntPlaced.put(event.getPlayer(), tntPlaced.getOrDefault(event.getPlayer(), 0) + 1);
}
} }
} }
@EventHandler public void onInteract(PlayerInteractEvent event) {
public void onPlayerInteract(PlayerInteractEvent event) { if (stage == Stage.COUNTING) {
if (Region.getRegion(event.getPlayer().getLocation()).equals(region)) { if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
if (stage == Stage.COUNTING) { Material type = event.getClickedBlock().getType();
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { if (type.name().contains("_BUTTON") || type == Material.LEVER || type.name().contains("_TRAPDOOR") || type == Material.NOTE_BLOCK) {
Material type = event.getClickedBlock().getType();
if (type.name().contains("_BUTTON") || type == Material.LEVER || type.name().contains("_TRAPDOOR") || type == Material.NOTE_BLOCK) {
setActivate();
}
} else if (event.getAction() == Action.PHYSICAL) {
setActivate(); setActivate();
} }
} else if (event.getAction() == Action.PHYSICAL) {
setActivate();
} }
} }
} }
@EventHandler public void onTntSpawn() {
public void onEntitySpawn(EntitySpawnEvent event) { if ((stage == Stage.COUNTING || stage == Stage.ACTIVATED)) {
if (event.getEntityType() == EntityType.PRIMED_TNT && Region.getRegion(event.getLocation()).equals(region) &&
(stage == Stage.COUNTING || stage == Stage.ACTIVATED)) {
stage = Stage.IGNITION; stage = Stage.IGNITION;
ignite = TPSUtils.currentTick.get(); ignite = TPSUtils.currentTick.get();
if (activate == -1) if (activate == -1)
@ -167,16 +155,12 @@ public class Loadtimer implements Listener {
} }
} }
@EventHandler public void onTntExplode(EntityExplodeEvent event) {
public void onEntityExplode(EntityExplodeEvent event) { if (region.inRegion(event.getLocation(), RegionType.BUILD, RegionExtensionType.EXTENSION) && stage == Stage.IGNITION) {
if (event.getEntityType() == EntityType.PRIMED_TNT) { stage = Stage.END;
Region r = Region.getRegion(event.getLocation()); explode = TPSUtils.currentTick.get();
if (r.equals(region) && r.inRegion(event.getLocation(), RegionType.BUILD, RegionExtensionType.EXTENSION) && stage == Stage.IGNITION) { print();
stage = Stage.END; delete();
explode = TPSUtils.currentTick.get();
print();
delete();
}
} }
} }

Datei anzeigen

@ -0,0 +1,78 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.loadtimer;
import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.Linked;
import de.steamwar.bausystem.region.Region;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import static de.steamwar.bausystem.features.loadtimer.Loadtimer.*;
@Linked(LinkageType.LISTENER)
public class LoadtimerListener implements Listener {
@EventHandler
public void onBlockPlace(BlockPlaceEvent event) {
if (!getTimers().isEmpty() && event.getBlockPlaced().getType() == Material.TNT) {
Region r = Region.getRegion(event.getBlock().getLocation());
if (hasTimer(r)) {
getTimer(r).onTntPlace(event);
}
}
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (!getTimers().isEmpty()) {
Region r = Region.getRegion(event.getPlayer().getLocation());
if (hasTimer(r)) {
getTimer(r).onInteract(event);
}
}
}
@EventHandler
public void onEntitySpawn(EntitySpawnEvent event) {
if (!getTimers().isEmpty() && event.getEntityType() == EntityType.PRIMED_TNT) {
Region r = Region.getRegion(event.getLocation());
if (hasTimer(r)) {
getTimer(r).onTntSpawn();
}
}
}
@EventHandler
public void onEntityExplode(EntityExplodeEvent event) {
if (!getTimers().isEmpty() && event.getEntityType() == EntityType.PRIMED_TNT) {
Region r = Region.getRegion(event.getLocation());
if (hasTimer(r)) {
getTimer(r).onTntExplode(event);
}
}
}
}