1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-12-01 15:03:36 +01:00

Avoiding memory leaks

This commit is contained in:
Zrips 2022-11-14 14:38:38 +02:00
parent 2bd257b563
commit b79b8a1e9d
3 changed files with 211 additions and 181 deletions

View File

@ -16,6 +16,7 @@ import com.gamingmesh.jobs.container.DBAction;
import net.Zrips.CMILib.Items.CMIMaterial;
import net.Zrips.CMILib.Locale.LC;
import net.Zrips.CMILib.Logs.CMIDebug;
import net.Zrips.CMILib.Messages.CMIMessages;
import net.Zrips.CMILib.Version.Version;
@ -57,9 +58,8 @@ public class bp implements Cmd {
changedBlocks.add(l.getBlock());
if (Version.isCurrentEqualOrHigher(Version.v1_15_R1)) {
player.sendBlockChange(l, (bp.getAction() == DBAction.DELETE ?
CMIMaterial.RED_STAINED_GLASS :
time == -1 ? CMIMaterial.BLACK_STAINED_GLASS : CMIMaterial.WHITE_STAINED_GLASS).getMaterial().createBlockData());
player.sendBlockChange(l, (bp.getAction() == DBAction.DELETE ? CMIMaterial.RED_STAINED_GLASS : time == -1 ? CMIMaterial.BLACK_STAINED_GLASS : CMIMaterial.WHITE_STAINED_GLASS)
.getMaterial().createBlockData());
} else {
if (bp.getAction() == DBAction.DELETE)
player.sendBlockChange(l, CMIMaterial.RED_STAINED_GLASS.getMaterial(), (byte) 14);

View File

@ -4,6 +4,7 @@ import java.util.HashMap;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
@ -15,7 +16,6 @@ import com.gamingmesh.jobs.container.DBAction;
import net.Zrips.CMILib.Container.CMIBlock;
import net.Zrips.CMILib.Container.CMIBlock.Bisect;
import net.Zrips.CMILib.Items.CMIMaterial;
import net.Zrips.CMILib.Logs.CMIDebug;
public class BlockProtectionManager {
@ -75,6 +75,7 @@ public class BlockProtectionManager {
}
public BlockProtection addP(Location loc, Long time, boolean paid, boolean cache) {
String v = loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ();
HashMap<String, HashMap<String, HashMap<String, BlockProtection>>> regions = map.getOrDefault(loc.getWorld(), new HashMap<>());
@ -88,16 +89,27 @@ public class BlockProtectionManager {
if (Bp == null)
Bp = new BlockProtection(DBAction.INSERT, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
else
else {
Bp.setAction(DBAction.UPDATE);
if (Bp.getSchedId() > -1)
Bukkit.getServer().getScheduler().cancelTask(Bp.getSchedId());
}
Bp.setPaid(paid);
Bp.setTime(time);
// If timer is under 2 hours, we can run scheduler to remove it when time comes
if ((time - System.currentTimeMillis()) / 1000 < 60 * 60 * 2)
Bp.setSchedId(Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Jobs.getInstance(), () -> {
remove(loc);
}, (time - System.currentTimeMillis()) / 50));
Bpm.put(v, Bp);
chunks.put(chunk, Bpm);
regions.put(region, chunks);
map.put(loc.getWorld(), regions);
if (cache)
// Only saving into save cache if timer is higher than 5 minutes
if (cache && (time - System.currentTimeMillis()) / 1000 > 60 * 5)
addToCache(loc, Bp);
return Bp;
}
@ -162,6 +174,15 @@ public class BlockProtectionManager {
BlockProtection bp = chunk.get(v);
if (bp != null)
bp.setAction(DBAction.DELETE);
if (bp != null && bp.getId() < 0) {
chunk.remove(v);
}
if (chunk.isEmpty())
region.remove(locToChunk(loc));
if (region.isEmpty())
world.remove(locToRegion(loc));
return bp;
}

View File

@ -6,7 +6,8 @@ public class BlockProtection {
private static long pre = (int) (System.currentTimeMillis() / 10000000000L) * 10000000000L;
private int id;
private int id = -1;
private int schedId = - 1;
private int time = -1;
private int recorded = -1;
private DBAction action;
@ -113,4 +114,12 @@ public class BlockProtection {
public int getZ() {
return z;
}
public int getSchedId() {
return schedId;
}
public void setSchedId(int schedId) {
this.schedId = schedId;
}
}