1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-29 14:05:25 +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;
@ -25,73 +26,72 @@ public class bp implements Cmd {
@Override
public boolean perform(Jobs plugin, final CommandSender sender, final String[] args) {
if (!(sender instanceof Player)) {
CMIMessages.sendMessage(sender, LC.info_Ingame);
return false;
}
boolean all = false;
if (args.length > 0 && args[0].equalsIgnoreCase("-a"))
all = true;
if (!(sender instanceof Player)) {
CMIMessages.sendMessage(sender, LC.info_Ingame);
return false;
}
boolean all = false;
if (args.length > 0 && args[0].equalsIgnoreCase("-a"))
all = true;
final Player player = (Player) sender;
final Player player = (Player) sender;
Location loc = player.getLocation();
Location loc = player.getLocation();
final List<Block> changedBlocks = new ArrayList<>();
final List<Block> changedBlocks = new ArrayList<>();
for (int x = -10; x < 10; x++) {
for (int y = -10; y < 10; y++) {
for (int z = -10; z < 10; z++) {
Location l = loc.clone().add(x, y, z);
BlockProtection bp = Jobs.getBpManager().getBp(l);
if (bp != null) {
Long time = bp.getTime();
if (!all) {
if (bp.getAction() == DBAction.DELETE)
continue;
if (time != -1 && time < System.currentTimeMillis()) {
Jobs.getBpManager().remove(l);
continue;
}
}
changedBlocks.add(l.getBlock());
for (int x = -10; x < 10; x++) {
for (int y = -10; y < 10; y++) {
for (int z = -10; z < 10; z++) {
Location l = loc.clone().add(x, y, z);
BlockProtection bp = Jobs.getBpManager().getBp(l);
if (bp != null) {
Long time = bp.getTime();
if (!all) {
if (bp.getAction() == DBAction.DELETE)
continue;
if (time != -1 && time < System.currentTimeMillis()) {
Jobs.getBpManager().remove(l);
continue;
}
}
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());
} else {
if (bp.getAction() == DBAction.DELETE)
player.sendBlockChange(l, CMIMaterial.RED_STAINED_GLASS.getMaterial(), (byte) 14);
else if (time == -1)
player.sendBlockChange(l, CMIMaterial.RED_STAINED_GLASS.getMaterial(), (byte) 15);
else
player.sendBlockChange(l, CMIMaterial.RED_STAINED_GLASS.getMaterial(), (byte) 0);
}
}
}
}
}
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());
} else {
if (bp.getAction() == DBAction.DELETE)
player.sendBlockChange(l, CMIMaterial.RED_STAINED_GLASS.getMaterial(), (byte) 14);
else if (time == -1)
player.sendBlockChange(l, CMIMaterial.RED_STAINED_GLASS.getMaterial(), (byte) 15);
else
player.sendBlockChange(l, CMIMaterial.RED_STAINED_GLASS.getMaterial(), (byte) 0);
}
}
}
}
}
if (changedBlocks.isEmpty())
sender.sendMessage(Jobs.getLanguage().getMessage("command.bp.output.notFound"));
else
sender.sendMessage(Jobs.getLanguage().getMessage("command.bp.output.found", "%amount%", changedBlocks.size()));
if (changedBlocks.isEmpty())
sender.sendMessage(Jobs.getLanguage().getMessage("command.bp.output.notFound"));
else
sender.sendMessage(Jobs.getLanguage().getMessage("command.bp.output.found", "%amount%", changedBlocks.size()));
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@Override
public void run() {
if (Version.isCurrentEqualOrHigher(Version.v1_15_R1))
for (Block one : changedBlocks) {
player.sendBlockChange(one.getLocation(), one.getBlockData());
}
else
for (Block one : changedBlocks) {
player.sendBlockChange(one.getLocation(), one.getType(), one.getData());
}
}
}, 120L);
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@Override
public void run() {
if (Version.isCurrentEqualOrHigher(Version.v1_15_R1))
for (Block one : changedBlocks) {
player.sendBlockChange(one.getLocation(), one.getBlockData());
}
else
for (Block one : changedBlocks) {
player.sendBlockChange(one.getLocation(), one.getType(), one.getData());
}
}
}, 120L);
return true;
return true;
}
}

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 {
@ -23,96 +23,108 @@ public class BlockProtectionManager {
private final ConcurrentHashMap<World, ConcurrentHashMap<String, BlockProtection>> tempCache = new ConcurrentHashMap<>();
public HashMap<World, HashMap<String, HashMap<String, HashMap<String, BlockProtection>>>> getMap() {
return map;
return map;
}
public int getSize() {
int i = 0;
for (HashMap<String, HashMap<String, HashMap<String, BlockProtection>>> worlds : map.values()) {
for (HashMap<String, HashMap<String, BlockProtection>> regions : worlds.values()) {
for (HashMap<String, BlockProtection> chunks : regions.values()) {
i += chunks.size();
}
}
}
return i;
int i = 0;
for (HashMap<String, HashMap<String, HashMap<String, BlockProtection>>> worlds : map.values()) {
for (HashMap<String, HashMap<String, BlockProtection>> regions : worlds.values()) {
for (HashMap<String, BlockProtection> chunks : regions.values()) {
i += chunks.size();
}
}
}
return i;
}
public void add(Block block, Integer cd) {
// Assuming that block is bottom part of flower we will add top part to the record too
CMIMaterial cmat = CMIMaterial.get(block);
switch (cmat) {
case LILAC:
case SUNFLOWER:
case ROSE_BUSH:
case PEONY:
CMIBlock cmb = new CMIBlock(block);
// We are only interested in this being bottom block as this should never trigger for top part of placed block
if (cmb.getBisect().equals(Bisect.BOTTOM))
add(block.getLocation().clone().add(0, 1, 0), cd, true);
break;
}
// Assuming that block is bottom part of flower we will add top part to the record too
CMIMaterial cmat = CMIMaterial.get(block);
switch (cmat) {
case LILAC:
case SUNFLOWER:
case ROSE_BUSH:
case PEONY:
CMIBlock cmb = new CMIBlock(block);
// We are only interested in this being bottom block as this should never trigger for top part of placed block
if (cmb.getBisect().equals(Bisect.BOTTOM))
add(block.getLocation().clone().add(0, 1, 0), cd, true);
break;
}
add(block, cd, true);
add(block, cd, true);
}
public void add(Block block, Integer cd, boolean paid) {
add(block.getLocation(), cd, paid);
add(block.getLocation(), cd, paid);
}
public void add(Location loc, Integer cd) {
add(loc, cd, true);
add(loc, cd, true);
}
public void add(Location loc, Integer cd, boolean paid) {
if (cd == null)
return;
if (cd != -1)
addP(loc, System.currentTimeMillis() + (cd * 1000), paid, true);
else
addP(loc, -1L, paid, true);
if (cd == null)
return;
if (cd != -1)
addP(loc, System.currentTimeMillis() + (cd * 1000), paid, true);
else
addP(loc, -1L, paid, true);
}
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<>());
String v = loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ();
String region = locToRegion(loc);
HashMap<String, HashMap<String, BlockProtection>> chunks = regions.getOrDefault(region, new HashMap<>());
HashMap<String, HashMap<String, HashMap<String, BlockProtection>>> regions = map.getOrDefault(loc.getWorld(), new HashMap<>());
String chunk = locToChunk(loc);
HashMap<String, BlockProtection> Bpm = chunks.getOrDefault(chunk, new HashMap<>());
BlockProtection Bp = Bpm.get(v);
String region = locToRegion(loc);
HashMap<String, HashMap<String, BlockProtection>> chunks = regions.getOrDefault(region, new HashMap<>());
if (Bp == null)
Bp = new BlockProtection(DBAction.INSERT, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
else
Bp.setAction(DBAction.UPDATE);
String chunk = locToChunk(loc);
HashMap<String, BlockProtection> Bpm = chunks.getOrDefault(chunk, new HashMap<>());
BlockProtection Bp = Bpm.get(v);
Bp.setPaid(paid);
Bp.setTime(time);
Bpm.put(v, Bp);
chunks.put(chunk, Bpm);
regions.put(region, chunks);
map.put(loc.getWorld(), regions);
if (cache)
addToCache(loc, Bp);
return Bp;
if (Bp == null)
Bp = new BlockProtection(DBAction.INSERT, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
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);
// 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;
}
private void addToCache(Location loc, BlockProtection Bp) {
if (!Jobs.getGCManager().useBlockProtection)
return;
String v = loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ();
ConcurrentHashMap<String, BlockProtection> locations = tempCache.get(loc.getWorld());
if (locations == null) {
locations = new ConcurrentHashMap<>();
tempCache.put(loc.getWorld(), locations);
}
if (!Jobs.getGCManager().useBlockProtection)
return;
String v = loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ();
ConcurrentHashMap<String, BlockProtection> locations = tempCache.get(loc.getWorld());
if (locations == null) {
locations = new ConcurrentHashMap<>();
tempCache.put(loc.getWorld(), locations);
}
locations.put(v, Bp);
locations.put(v, Bp);
// if (locations.size() > 10) {
// Jobs.getJobsDAO().saveBlockProtection(loc.getWorld().getName(), new HashMap<String, BlockProtection>(locations));
@ -121,91 +133,100 @@ public class BlockProtectionManager {
}
public void saveCache() {
if (!Jobs.getGCManager().useBlockProtection)
return;
for (Entry<World, ConcurrentHashMap<String, BlockProtection>> one : tempCache.entrySet()) {
Jobs.getJobsDAO().saveBlockProtection(one.getKey().getName(), one.getValue());
}
tempCache.clear();
if (!Jobs.getGCManager().useBlockProtection)
return;
for (Entry<World, ConcurrentHashMap<String, BlockProtection>> one : tempCache.entrySet()) {
Jobs.getJobsDAO().saveBlockProtection(one.getKey().getName(), one.getValue());
}
tempCache.clear();
}
public BlockProtection remove(Block block) {
// In case double plant was destroyed we should remove both blocks from records
CMIMaterial cmat = CMIMaterial.get(block);
switch (cmat) {
case LILAC:
case SUNFLOWER:
case ROSE_BUSH:
case PEONY:
CMIBlock cmb = new CMIBlock(block);
if (cmb.getBisect().equals(Bisect.BOTTOM))
remove(block.getLocation().clone().add(0, 1, 0));
else
remove(block.getLocation().clone().add(0, -1, 0));
break;
}
// In case double plant was destroyed we should remove both blocks from records
CMIMaterial cmat = CMIMaterial.get(block);
switch (cmat) {
case LILAC:
case SUNFLOWER:
case ROSE_BUSH:
case PEONY:
CMIBlock cmb = new CMIBlock(block);
if (cmb.getBisect().equals(Bisect.BOTTOM))
remove(block.getLocation().clone().add(0, 1, 0));
else
remove(block.getLocation().clone().add(0, -1, 0));
break;
}
return remove(block.getLocation());
return remove(block.getLocation());
}
public BlockProtection remove(Location loc) {
HashMap<String, HashMap<String, HashMap<String, BlockProtection>>> world = map.get(loc.getWorld());
if (world == null)
return null;
HashMap<String, HashMap<String, BlockProtection>> region = world.get(locToRegion(loc));
if (region == null)
return null;
HashMap<String, BlockProtection> chunk = region.get(locToChunk(loc));
if (chunk == null)
return null;
String v = loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ();
BlockProtection bp = chunk.get(v);
if (bp != null)
bp.setAction(DBAction.DELETE);
return bp;
HashMap<String, HashMap<String, HashMap<String, BlockProtection>>> world = map.get(loc.getWorld());
if (world == null)
return null;
HashMap<String, HashMap<String, BlockProtection>> region = world.get(locToRegion(loc));
if (region == null)
return null;
HashMap<String, BlockProtection> chunk = region.get(locToChunk(loc));
if (chunk == null)
return null;
String v = loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ();
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;
}
public Long getTime(Block block) {
return getTime(block.getLocation());
return getTime(block.getLocation());
}
public Long getTime(Location loc) {
BlockProtection Bp = getBp(loc);
return Bp == null ? null : Bp.getTime();
BlockProtection Bp = getBp(loc);
return Bp == null ? null : Bp.getTime();
}
public BlockProtection getBp(Location loc) {
HashMap<String, HashMap<String, HashMap<String, BlockProtection>>> world = map.get(loc.getWorld());
if (world == null)
return null;
HashMap<String, HashMap<String, BlockProtection>> region = world.get(locToRegion(loc));
if (region == null)
return null;
HashMap<String, BlockProtection> chunk = region.get(locToChunk(loc));
if (chunk == null)
return null;
return chunk.get(loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ());
HashMap<String, HashMap<String, HashMap<String, BlockProtection>>> world = map.get(loc.getWorld());
if (world == null)
return null;
HashMap<String, HashMap<String, BlockProtection>> region = world.get(locToRegion(loc));
if (region == null)
return null;
HashMap<String, BlockProtection> chunk = region.get(locToChunk(loc));
if (chunk == null)
return null;
return chunk.get(loc.getBlockX() + ":" + loc.getBlockY() + ":" + loc.getBlockZ());
}
private static String locToChunk(Location loc) {
return (int) Math.floor(loc.getBlockX() / 16D) + ":" + (int) Math.floor(loc.getBlockZ() / 16D);
return (int) Math.floor(loc.getBlockX() / 16D) + ":" + (int) Math.floor(loc.getBlockZ() / 16D);
}
private static String locToRegion(Location loc) {
int x = (int) Math.floor(loc.getBlockX() / 16D);
int z = (int) Math.floor(loc.getBlockZ() / 16D);
return (int) Math.floor(x / 32D) + ":" + (int) Math.floor(z / 32D);
int x = (int) Math.floor(loc.getBlockX() / 16D);
int z = (int) Math.floor(loc.getBlockZ() / 16D);
return (int) Math.floor(x / 32D) + ":" + (int) Math.floor(z / 32D);
}
public Integer getBlockDelayTime(Block block) {
Integer time = Jobs.getRestrictedBlockManager().restrictedBlocksTimer.get(CMIMaterial.get(block));
if (time == null && Jobs.getGCManager().useGlobalTimer) {
time = Jobs.getGCManager().globalblocktimer;
}
return time;
Integer time = Jobs.getRestrictedBlockManager().restrictedBlocksTimer.get(CMIMaterial.get(block));
if (time == null && Jobs.getGCManager().useGlobalTimer) {
time = Jobs.getGCManager().globalblocktimer;
}
return time;
}
public boolean isInBp(Block block) {
return Jobs.getRestrictedBlockManager().restrictedBlocksTimer.containsKey(CMIMaterial.get(block));
return Jobs.getRestrictedBlockManager().restrictedBlocksTimer.containsKey(CMIMaterial.get(block));
}
}

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;
}
}