Improve Loadtimer Listener Performance
Signed-off-by: Chaoscaot <chaoscaot444@gmail.com>
Dieser Commit ist enthalten in:
Ursprung
8dbb9cb940
Commit
4d8cf8a125
@ -25,20 +25,18 @@ import de.steamwar.bausystem.features.tpslimit.TPSUtils;
|
||||
import de.steamwar.bausystem.region.Region;
|
||||
import de.steamwar.bausystem.region.utils.RegionExtensionType;
|
||||
import de.steamwar.bausystem.region.utils.RegionType;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.boss.BarColor;
|
||||
import org.bukkit.boss.BarStyle;
|
||||
import org.bukkit.boss.BossBar;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
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 org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
@ -49,13 +47,14 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class Loadtimer implements Listener {
|
||||
|
||||
@Getter
|
||||
private static final Map<Region, Loadtimer> timers = new HashMap<>();
|
||||
private final Region region;
|
||||
private final Map<Player, Integer> tntPlaced = new HashMap<>();
|
||||
private final BukkitTask task;
|
||||
private final Map<Player, BossBar> bars = new HashMap();
|
||||
private final Map<Player, BossBar> bars = new HashMap<>();
|
||||
private Stage stage;
|
||||
private boolean finishOnActive = true;
|
||||
private final boolean finishOnActive;
|
||||
|
||||
private long start = -1;
|
||||
private long activate = -1;
|
||||
@ -68,11 +67,6 @@ public class Loadtimer implements Listener {
|
||||
this.stage = Stage.WAITING;
|
||||
Bukkit.getPluginManager().registerEvents(this, 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) {
|
||||
long timeSinceStart = TPSUtils.currentTick.get() - start;
|
||||
long timeSinceHalf = timeSinceStart / 2;
|
||||
@ -97,6 +91,8 @@ public class Loadtimer implements Listener {
|
||||
bar.setTitle(BauSystem.MESSAGE.parse(message, player, objects));
|
||||
bar.setColor(color);
|
||||
bar.setProgress(progress);
|
||||
} else if (bars.containsKey(player)) {
|
||||
bars.remove(player).removeAll();
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -121,40 +117,32 @@ public class Loadtimer implements Listener {
|
||||
});
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onBlockPlace(BlockPlaceEvent event) {
|
||||
if (Region.getRegion(event.getBlock().getLocation()).equals(region) && event.getBlockPlaced().getType() == Material.TNT) {
|
||||
if (stage == Stage.WAITING) {
|
||||
this.stage = Stage.COUNTING;
|
||||
this.start = TPSUtils.currentTick.get();
|
||||
}
|
||||
public void onTntPlace(BlockPlaceEvent event) {
|
||||
if (stage == Stage.WAITING) {
|
||||
this.stage = Stage.COUNTING;
|
||||
this.start = TPSUtils.currentTick.get();
|
||||
}
|
||||
|
||||
if (stage == Stage.COUNTING) {
|
||||
tntPlaced.put(event.getPlayer(), tntPlaced.getOrDefault(event.getPlayer(), 0) + 1);
|
||||
}
|
||||
if (stage == Stage.COUNTING) {
|
||||
tntPlaced.put(event.getPlayer(), tntPlaced.getOrDefault(event.getPlayer(), 0) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerInteract(PlayerInteractEvent event) {
|
||||
if (Region.getRegion(event.getPlayer().getLocation()).equals(region)) {
|
||||
if (stage == Stage.COUNTING) {
|
||||
if (event.getAction() == Action.RIGHT_CLICK_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) {
|
||||
public void onInteract(PlayerInteractEvent event) {
|
||||
if (stage == Stage.COUNTING) {
|
||||
if (event.getAction() == Action.RIGHT_CLICK_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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onEntitySpawn(EntitySpawnEvent event) {
|
||||
if (event.getEntityType() == EntityType.PRIMED_TNT && Region.getRegion(event.getLocation()).equals(region) &&
|
||||
(stage == Stage.COUNTING || stage == Stage.ACTIVATED)) {
|
||||
public void onTntSpawn() {
|
||||
if ((stage == Stage.COUNTING || stage == Stage.ACTIVATED)) {
|
||||
stage = Stage.IGNITION;
|
||||
ignite = TPSUtils.currentTick.get();
|
||||
if (activate == -1)
|
||||
@ -167,16 +155,12 @@ public class Loadtimer implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onEntityExplode(EntityExplodeEvent event) {
|
||||
if (event.getEntityType() == EntityType.PRIMED_TNT) {
|
||||
Region r = Region.getRegion(event.getLocation());
|
||||
if (r.equals(region) && r.inRegion(event.getLocation(), RegionType.BUILD, RegionExtensionType.EXTENSION) && stage == Stage.IGNITION) {
|
||||
stage = Stage.END;
|
||||
explode = TPSUtils.currentTick.get();
|
||||
print();
|
||||
delete();
|
||||
}
|
||||
public void onTntExplode(EntityExplodeEvent event) {
|
||||
if (region.inRegion(event.getLocation(), RegionType.BUILD, RegionExtensionType.EXTENSION) && stage == Stage.IGNITION) {
|
||||
stage = Stage.END;
|
||||
explode = TPSUtils.currentTick.get();
|
||||
print();
|
||||
delete();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
In neuem Issue referenzieren
Einen Benutzer sperren