Skip to content

Commit

Permalink
Merge pull request #11 from Realizedd/dev
Browse files Browse the repository at this point in the history
Release v1.3.0 - Memory Optimization
  • Loading branch information
Realizedd authored Feb 25, 2023
2 parents 36fc694 + 7d9d545 commit ddeed33
Show file tree
Hide file tree
Showing 28 changed files with 330 additions and 243 deletions.
2 changes: 2 additions & 0 deletions arenaregen/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies {
implementation project(':v1_17_R1')
implementation project(':v1_18_R1')
implementation project(':v1_18_R2')
implementation project(':v1_19_R1')
}

shadowJar {
Expand All @@ -47,6 +48,7 @@ shadowJar {
include(dependency(':v1_17_R1'))
include(dependency(':v1_18_R1'))
include(dependency(':v1_18_R2'))
include(dependency(':v1_19_R1'))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public class ArenaRegen extends DuelsExtension {
@Override
public void onEnable() {
this.configuration = new Config(this);

if (configuration.isTrackBlockChanges() && configuration.isAllowArenaBlockBreak()) {
warn("The config options 'track-block-changes' and 'allow-arena-block-break' are incompatible with each other.");
}

this.lang = new Lang(this);
this.handler = findHandler();

Expand Down Expand Up @@ -82,6 +87,10 @@ public void info(final String s) {
api.info("[" + getName() + " Extension] " + s);
}

public void warn(final String s) {
api.warn("[" + getName() + " Extension] " + s);
}

public void error(final String s) {
api.error("[" + getName() + " Extension] " + s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public void execute(final CommandSender sender, final String label, final String
}

lang.sendMessage(sender, "COMMAND.arenaregen.reset.start", "name", name);
zone.reset(() -> {
lang.sendMessage(sender, "COMMAND.arenaregen.reset.end", "name", name);
}, true);
zone.reset(() -> lang.sendMessage(sender, "COMMAND.arenaregen.reset.end", "name", name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import me.realized.de.arenaregen.util.ReflectionUtil;
import org.bukkit.Chunk;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;

Expand All @@ -21,7 +22,8 @@ public NMSHandler() {
public void sendChunkUpdate(final Player player, final Chunk chunk) {}

@Override
public void setBlockFast(final Block block, final Material material, final int data) {
public void setBlockFast(final World world, final int x, final int y, final int z, final int data, final Material material) {
final Block block = world.getBlockAt(x, y, z);
block.setType(material);

if (SET_DATA != null) {
Expand All @@ -32,5 +34,5 @@ public void setBlockFast(final Block block, final Material material, final int d
}

@Override
public void updateLighting(Block bukkitBlock) {}
public void updateLighting(final World world, final int x, final int y, final int z) {}
}
157 changes: 82 additions & 75 deletions arenaregen/src/main/java/me/realized/de/arenaregen/zone/Zone.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,11 @@
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Entity;

import lombok.Getter;
import me.realized.de.arenaregen.ArenaRegen;
import me.realized.de.arenaregen.config.Config;
Expand All @@ -35,13 +19,21 @@
import me.realized.de.arenaregen.util.BlockUtil;
import me.realized.de.arenaregen.util.Callback;
import me.realized.de.arenaregen.util.ChunkLoc;
import me.realized.de.arenaregen.util.Pair;
import me.realized.de.arenaregen.util.Position;
import me.realized.de.arenaregen.zone.task.Task;
import me.realized.de.arenaregen.zone.task.tasks.ResetBlocksTask;
import me.realized.de.arenaregen.zone.task.tasks.FilterBlocksTask;
import me.realized.de.arenaregen.zone.task.tasks.ScanBlocksTask;
import me.realized.duels.api.Duels;
import me.realized.duels.api.arena.Arena;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

public class Zone {

Expand All @@ -57,22 +49,22 @@ public class Zone {
@Getter
private Location min, max;

@Getter
private File file;

@Getter
private Task task;

@Getter
private final Map<Position, BlockInfo> blocks = new HashMap<>();
private volatile Map<Position, BlockInfo> blocks = new HashMap<>();

@Getter
private final Set<ChunkLoc> chunks = new HashSet<>();

@Getter
private final List<Entity> spawnedEntities = new ArrayList<>();
// @Getter
// private final List<Entity> spawnedEntities = new ArrayList<>();

private Set<Block> changedBlocks = new HashSet<>();
private Queue<Pair<Block, BlockInfo>> changes = new LinkedList<>();
private Set<Position> changedBlocks = new HashSet<>();

Zone(final ArenaRegen extension, final Duels api, final Arena arena, final File folder, final Location first, final Location second) {
this.api = api;
Expand All @@ -94,6 +86,9 @@ public class Zone {
Math.max(first.getBlockZ(), second.getBlockZ())
);


final Map<Position, BlockInfo> blocks = new HashMap<>();

BlockUtil.runForCuboid(min, max, block -> {
// Only store non-air blocks
if (block.getType() == Material.AIR) {
Expand All @@ -102,7 +97,11 @@ public class Zone {

blocks.put(new Position(block), new BlockInfo(block.getState()));
});


if (!config.isTrackBlockChanges()) {
this.blocks = blocks;
}

try (final BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(min.getWorld().getName());
writer.newLine();
Expand Down Expand Up @@ -176,7 +175,7 @@ public class Zone {

this.file = file = newFile;
}

try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String worldName = reader.readLine();
final World world;
Expand All @@ -187,6 +186,23 @@ public class Zone {

this.min = new Location(world, Integer.parseInt(reader.readLine()), Integer.parseInt(reader.readLine()), Integer.parseInt(reader.readLine()));
this.max = new Location(world, Integer.parseInt(reader.readLine()), Integer.parseInt(reader.readLine()), Integer.parseInt(reader.readLine()));
}

if (!config.isTrackBlockChanges()) {
this.blocks = loadBlocks();
}

loadChunks();
}

private Map<Position, BlockInfo> loadBlocks() throws IOException {
final Map<Position, BlockInfo> blocks = new HashMap<>();

try (final BufferedReader reader = new BufferedReader(new FileReader(file))) {
// Skip 7 lines to get to blocks section
for (int i = 0; i < 7; i++) {
reader.readLine();
}

String block;

Expand All @@ -196,11 +212,11 @@ public class Zone {
final Position pos = new Position(Integer.parseInt(posData[0]), Integer.parseInt(posData[1]), Integer.parseInt(posData[2]));
final String[] blockData = data[1].split(";");
final BlockInfo info = new BlockInfo(Material.getMaterial(blockData[0]), Byte.parseByte(blockData[1]));
this.blocks.put(pos, info);
blocks.put(pos, info);
}
}

loadChunks();
return blocks;
}

private void loadChunks() {
Expand All @@ -215,12 +231,21 @@ public String getName() {
return arena.getName();
}

private int calculateSize() {
return (max.getBlockX() - min.getBlockX() + 1)
+ (max.getBlockY() - min.getBlockY() + 1)
+ (max.getBlockZ() - min.getBlockZ() + 1);
}

public int getTotalBlocks() {
return blocks.size();
return config.isTrackBlockChanges() ? calculateSize() : blocks.size();
}

public World getWorld() {
return min.getWorld();
}

void delete() {
blocks.clear();
file.delete();
}

Expand All @@ -229,53 +254,54 @@ public boolean isResetting() {
}

// Called before reset zones are saved to files.
public void resetInstant() {
public void resetInstant() throws IOException {
final Map<Position, BlockInfo> blocks = loadBlocks();

BlockUtil.runForCuboid(min, max, block -> {
final Position position = new Position(block);
final BlockInfo info = blocks.get(position);
final Position pos = new Position(block);
final BlockInfo info = blocks.get(pos);

if (info == null) {
if (block.getType() != Material.AIR) {
handler.setBlockFast(block, Material.AIR, 0);
handler.setBlockFast(getWorld(), pos.getX(), pos.getY(), pos.getZ(), 0, Material.AIR);
}

return;
} else if (info.matches(block)) {
return;
}

handler.setBlockFast(block, info.getType(), info.getData());
handler.updateLighting(block);
handler.setBlockFast(getWorld(), pos.getX(), pos.getY(), pos.getZ(), info.getData(), info.getType());
handler.updateLighting(getWorld(), pos.getX(), pos.getY(), pos.getZ());
});
}

public void startTask(final Task task) {
public void startSyncTaskTimer(final Task task) {
this.task = task;

if (task != null) {
task.runTaskTimer(api, 1L, 1L);
}
}

public void reset(final Callback onDone, final boolean hard) {
arena.setDisabled(true);

if (hard) {
startTask(new ScanBlocksTask(extension, this, onDone));
return;
}
public void startAsyncTask(final Task task) {
this.task = task;

if (config.isTrackBlockChanges()) {
startTask(new ResetBlocksTask(extension, this, onDone, this.changes));
this.changedBlocks = new HashSet<>();
this.changes = new LinkedList<>();
} else {
startTask(new ScanBlocksTask(extension, this, onDone));
if (task != null) {
task.runTaskAsynchronously(api);
}
}

public void reset(final Callback onDone) {
reset(onDone, false);
arena.setDisabled(true);

if (!config.isTrackBlockChanges()) {
startSyncTaskTimer(new ScanBlocksTask(extension, this, onDone));
return;
}

startAsyncTask(new FilterBlocksTask(extension, this, onDone, this.changedBlocks));
this.changedBlocks = new HashSet<>();
}

public void reset() {
Expand All @@ -293,38 +319,19 @@ public boolean contains(final Block block) {
return contains(block.getLocation());
}

boolean isCached(final Block block) {
return contains(block) && blocks.containsKey(new Position(block));
public boolean contains(final Chunk chunk) {
return chunks.contains(new ChunkLoc(chunk));
}

boolean isCached(final Chunk chunk) {
return chunks.contains(new ChunkLoc(chunk));
public boolean isCached(final Block block) {
return contains(block) && blocks.containsKey(new Position(block));
}

public void track(final Block block) {
if (changedBlocks.contains(block)) {
return;
}

final Position position = new Position(block);
final BlockInfo info = blocks.get(position);

if (info == null) {
if (block.getType() != Material.AIR) {
changes.add(new Pair<>(block, new BlockInfo()));
changedBlocks.add(block);
}

return;
} else if (info.matches(block)) {
return;
}

changes.add(new Pair<>(block, info));
changedBlocks.add(block);
changedBlocks.add(new Position(block));
}

public void track(final Collection<Block> blocks) {
blocks.forEach(block -> track(block));
blocks.forEach(this::track);
}
}
Loading

0 comments on commit ddeed33

Please sign in to comment.