Code cleanup and Optimizations

This commit is contained in:
MattBDev
2016-03-29 00:56:44 -04:00
parent 49d18b9229
commit 32ba55baf5
34 changed files with 456 additions and 540 deletions

View File

@ -143,7 +143,7 @@ public class SpongeMain implements IPlotMain {
if (!Settings.CONSOLE_COLOR) {
message = message.replaceAll('\u00a7' + "[a-z|0-9]", "");
}
if (this.server == null || this.server.getConsole() == null) {
if (this.server == null) {
this.logger.info(message);
return;
}
@ -306,7 +306,7 @@ public class SpongeMain implements IPlotMain {
@Override
public void startMetrics() {
try {
Metrics metrics = new Metrics(this.game, plugin);
Metrics metrics = new Metrics(this.game, this.plugin);
metrics.start();
log(C.PREFIX.s() + "&6Metrics enabled.");
} catch (IOException e) {

View File

@ -1,16 +1,15 @@
package com.plotsquared.sponge.events;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotId;
import org.spongepowered.api.event.Cancellable;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.event.impl.AbstractEvent;
import com.intellectualcrafters.plot.object.PlotId;
public class PlotClearEvent extends AbstractEvent implements Cancellable {
private final Plot plot;
private boolean cancelled;
private Plot plot;
/**
* PlotDeleteEvent: Called when a plot is cleared
@ -23,31 +22,31 @@ public class PlotClearEvent extends AbstractEvent implements Cancellable {
}
/**
* Get the PlotId
* Get the PlotId.
*
* @return PlotId
*/
public PlotId getPlotId() {
return plot.getId();
return this.plot.getId();
}
/**
* Get the world name
* Get the world name.
*
* @return String
*/
public String getWorld() {
return plot.getArea().worldname;
return this.plot.getArea().worldname;
}
@Override
public boolean isCancelled() {
return cancelled;
return this.cancelled;
}
@Override
public void setCancelled(final boolean cancel) {
cancelled = cancel;
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
@Override

View File

@ -24,14 +24,6 @@ import com.plotsquared.listener.PlotListener;
import com.plotsquared.sponge.SpongeMain;
import com.plotsquared.sponge.object.SpongePlayer;
import com.plotsquared.sponge.util.SpongeUtil;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Predicate;
import org.spongepowered.api.block.BlockSnapshot;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.data.Transaction;
@ -60,6 +52,15 @@ import org.spongepowered.api.event.world.ExplosionEvent.Detonate;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.world.World;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Predicate;
public class MainListener {
/*
@ -159,7 +160,7 @@ public class MainListener {
}
((SpongePlayer) user).player.sendMessage(Text.join(components));
}
event.setMessage(null);
//event.setMessage(null);
}
@Listener
@ -185,7 +186,7 @@ public class MainListener {
@Listener
public void onSpawnEntity(SpawnEntityEvent event) throws Exception {
World world = event.getTargetWorld();
event.filterEntities((Predicate<Entity>) entity -> {
event.filterEntities(entity -> {
if (entity instanceof Player) {
return true;
}
@ -396,7 +397,7 @@ public class MainListener {
return;
}
}
if (currentPlot == null || !FlagManager.isPlotFlagTrue(currentPlot, "explosion")) {
if (!FlagManager.isPlotFlagTrue(currentPlot, "explosion")) {
event.filterAll();
return;
}

View File

@ -16,12 +16,13 @@ public class FastChunk extends PlotChunk<Chunk> {
public short[] relight;
public int[][] biomes;
public Chunk chunk;
public FastChunk(final ChunkWrapper chunk) {
public FastChunk(ChunkWrapper chunk) {
super(chunk);
ids = new char[16][];
count = new short[16];
air = new short[16];
relight = new short[16];
this.ids = new char[16][];
this.count = new short[16];
this.air = new short[16];
this.relight = new short[16];
}
@Override
@ -32,89 +33,89 @@ public class FastChunk extends PlotChunk<Chunk> {
@Override
public Chunk getChunk() {
if (chunk == null) {
final ChunkWrapper cl = getChunkWrapper();
chunk = SpongeUtil.getWorld(cl.world).getChunk(cl.x, 0, cl.z).get();
if (this.chunk == null) {
ChunkWrapper cl = getChunkWrapper();
this.chunk = SpongeUtil.getWorld(cl.world).getChunk(cl.x, 0, cl.z).get();
}
return chunk;
return this.chunk;
}
@Override
public void setChunkWrapper(final ChunkWrapper loc) {
public void setChunkWrapper(ChunkWrapper loc) {
super.setChunkWrapper(loc);
chunk = null;
this.chunk = null;
}
/**
* Get the number of block changes in a specified section
* Get the number of block changes in a specified section.
* @param i
* @return
*/
public int getCount(final int i) {
return count[i];
public int getCount(int i) {
return this.count[i];
}
public int getAir(final int i) {
return air[i];
public int getAir(int i) {
return this.air[i];
}
public void setCount(int i, short value) {
count[i] = value;
this.count[i] = value;
}
/**
* Get the number of block changes in a specified section
* Get the number of block changes in a specified section.
* @param i
* @return
*/
public int getRelight(final int i) {
return relight[i];
public int getRelight(int i) {
return this.relight[i];
}
public int getTotalCount() {
int total = 0;
for (int i = 0; i < 16; i++) {
total += count[i];
total += this.count[i];
}
return total;
}
public int getTotalRelight() {
if (getTotalCount() == 0) {
Arrays.fill(count, (short) 1);
Arrays.fill(relight, Short.MAX_VALUE);
Arrays.fill(this.count, (short) 1);
Arrays.fill(this.relight, Short.MAX_VALUE);
return Short.MAX_VALUE;
}
int total = 0;
for (int i = 0; i < 16; i++) {
total += relight[i];
total += this.relight[i];
}
return total;
}
/**
* Get the raw data for a section
* Get the raw data for a section.
* @param i
* @return
*/
public char[] getIdArray(final int i) {
return ids[i];
public char[] getIdArray(int i) {
return this.ids[i];
}
@Override
public void setBlock(final int x, final int y, final int z, final int id, byte data) {
final int i = MainUtil.CACHE_I[y][x][z];
final int j = MainUtil.CACHE_J[y][x][z];
char[] vs = ids[i];
public void setBlock(int x, int y, int z, int id, byte data) {
int i = MainUtil.CACHE_I[y][x][z];
int j = MainUtil.CACHE_J[y][x][z];
char[] vs = this.ids[i];
if (vs == null) {
vs = ids[i] = new char[4096];
count[i]++;
vs = this.ids[i] = new char[4096];
this.count[i]++;
} else if (vs[j] == 0) {
count[i]++;
this.count[i]++;
}
switch (id) {
case 0:
air[i]++;
this.air[i]++;
vs[j] = (char) 1;
return;
case 10:
@ -128,7 +129,7 @@ public class FastChunk extends PlotChunk<Chunk> {
case 124:
case 138:
case 169:
relight[i]++;
this.relight[i]++;
case 2:
case 4:
case 13:
@ -192,7 +193,7 @@ public class FastChunk extends PlotChunk<Chunk> {
case 130:
case 76:
case 62:
relight[i]++;
this.relight[i]++;
case 54:
case 146:
case 61:
@ -211,12 +212,12 @@ public class FastChunk extends PlotChunk<Chunk> {
@Override
public PlotChunk clone() {
FastChunk toReturn = new FastChunk(getChunkWrapper());
toReturn.air = air.clone();
toReturn.count = count.clone();
toReturn.relight = relight.clone();
toReturn.ids = new char[ids.length][];
for (int i = 0; i < ids.length; i++) {
char[] matrix = ids[i];
toReturn.air = this.air.clone();
toReturn.count = this.count.clone();
toReturn.relight = this.relight.clone();
toReturn.ids = new char[this.ids.length][];
for (int i = 0; i < this.ids.length; i++) {
char[] matrix = this.ids[i];
if (matrix != null) {
toReturn.ids[i] = new char[matrix.length];
System.arraycopy(matrix, 0, toReturn.ids[i], 0, matrix.length);
@ -228,18 +229,18 @@ public class FastChunk extends PlotChunk<Chunk> {
@Override
public PlotChunk shallowClone() {
FastChunk toReturn = new FastChunk(getChunkWrapper());
toReturn.air = air;
toReturn.count = count;
toReturn.relight = relight;
toReturn.ids = ids;
toReturn.air = this.air;
toReturn.count = this.count;
toReturn.relight = this.relight;
toReturn.ids = this.ids;
return toReturn;
}
@Override
public void setBiome(int x, int z, int biome) {
if (biomes == null) {
biomes = new int[16][16];
if (this.biomes == null) {
this.biomes = new int[16][16];
}
biomes[x][z] = biome;
this.biomes[x][z] = biome;
}
}

View File

@ -76,14 +76,14 @@ public class FastQueue extends SlowQueue {
/**
* This should be overridden by any specialized queues.
* @param pc
* @param plotChunk
*/
@Override
public void execute(PlotChunk<Chunk> pc) {
FastChunk fs = (FastChunk) pc;
Chunk spongeChunk = pc.getChunk();
public void execute(PlotChunk<Chunk> plotChunk) {
FastChunk fs = (FastChunk) plotChunk;
Chunk spongeChunk = plotChunk.getChunk();
net.minecraft.world.World nmsWorld = (net.minecraft.world.World) spongeChunk.getWorld();
ChunkWrapper wrapper = pc.getChunkWrapper();
ChunkWrapper wrapper = plotChunk.getChunkWrapper();
if (!this.toUpdate.containsKey(wrapper)) {
this.toUpdate.put(wrapper, spongeChunk);
}

View File

@ -1,12 +1,11 @@
package com.plotsquared.sponge.util.block;
import org.spongepowered.api.world.Chunk;
import com.intellectualcrafters.plot.object.PlotBlock;
import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.PlotChunk;
import com.intellectualcrafters.plot.util.SetQueue.ChunkWrapper;
import com.plotsquared.sponge.util.SpongeUtil;
import org.spongepowered.api.world.Chunk;
public class SlowChunk extends PlotChunk<Chunk> {
@ -26,29 +25,29 @@ public class SlowChunk extends PlotChunk<Chunk> {
@Override
public void setBiome(int x, int z, int biome) {
if (biomes == null) {
biomes = new int[16][16];
if (this.biomes == null) {
this.biomes = new int[16][16];
}
biomes[x][z] = biome;
this.biomes[x][z] = biome;
}
@Override
public void setBlock(int x, int y, int z, int id, byte data) {
if (result[y >> 4] == null) {
result[y >> 4] = new PlotBlock[4096];
if (this.result[y >> 4] == null) {
this.result[y >> 4] = new PlotBlock[4096];
}
if (id == lastBlock.id && data == lastBlock.data) {
result[MainUtil.CACHE_I[x][y][z]][MainUtil.CACHE_J[x][y][z]] = lastBlock;
if (id == this.lastBlock.id && data == this.lastBlock.data) {
this.result[MainUtil.CACHE_I[x][y][z]][MainUtil.CACHE_J[x][y][z]] = this.lastBlock;
} else {
result[MainUtil.CACHE_I[x][y][z]][MainUtil.CACHE_J[x][y][z]] = new PlotBlock((short) id, data);
this.result[MainUtil.CACHE_I[x][y][z]][MainUtil.CACHE_J[x][y][z]] = new PlotBlock((short) id, data);
}
}
@Override
public PlotChunk clone() {
SlowChunk toReturn = new SlowChunk(getChunkWrapper());
for (int i = 0; i < result.length; i++) {
PlotBlock[] matrix = result[i];
for (int i = 0; i < this.result.length; i++) {
PlotBlock[] matrix = this.result[i];
if (matrix != null) {
toReturn.result[i] = new PlotBlock[matrix.length];
System.arraycopy(matrix, 0, toReturn.result[i], 0, matrix.length);
@ -60,7 +59,7 @@ public class SlowChunk extends PlotChunk<Chunk> {
@Override
public PlotChunk shallowClone() {
SlowChunk toReturn = new SlowChunk(getChunkWrapper());
toReturn.result = result;
toReturn.result = this.result;
return toReturn;
}
}

View File

@ -1,13 +1,5 @@
package com.plotsquared.sponge.util.block;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.world.Chunk;
import com.flowpowered.math.vector.Vector3i;
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.object.ChunkLoc;
@ -18,6 +10,13 @@ import com.intellectualcrafters.plot.util.PlotQueue;
import com.intellectualcrafters.plot.util.SetQueue;
import com.intellectualcrafters.plot.util.SetQueue.ChunkWrapper;
import com.plotsquared.sponge.util.SpongeUtil;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.world.Chunk;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
public class SlowQueue implements PlotQueue<Chunk> {
@ -28,18 +27,18 @@ public class SlowQueue implements PlotQueue<Chunk> {
if (y > 255 || y < 0) {
return false;
}
final ChunkWrapper wrap = SetQueue.IMP.new ChunkWrapper(world, x >> 4, z >> 4);
ChunkWrapper wrap = SetQueue.IMP.new ChunkWrapper(world, x >> 4, z >> 4);
x = x & 15;
z = z & 15;
PlotChunk<Chunk> result = blocks.get(wrap);
PlotChunk<Chunk> result = this.blocks.get(wrap);
if (result == null) {
result = getChunk(wrap);
result.setBlock(x, y, z, id, data);
final PlotChunk<Chunk> previous = blocks.put(wrap, result);
PlotChunk<Chunk> previous = this.blocks.put(wrap, result);
if (previous == null) {
return true;
}
blocks.put(wrap, previous);
this.blocks.put(wrap, previous);
result = previous;
}
result.setBlock(x, y, z, id, data);
@ -48,7 +47,7 @@ public class SlowQueue implements PlotQueue<Chunk> {
@Override
public void setChunk(PlotChunk<Chunk> chunk) {
blocks.put(chunk.getChunkWrapper(), chunk);
this.blocks.put(chunk.getChunkWrapper(), chunk);
}
@Override
@ -57,11 +56,11 @@ public class SlowQueue implements PlotQueue<Chunk> {
throw new IllegalStateException("Must be called from main thread!");
}
try {
if (blocks.isEmpty()) {
if (this.blocks.isEmpty()) {
return null;
}
final Iterator<Entry<ChunkWrapper, PlotChunk<Chunk>>> iter = blocks.entrySet().iterator();
final PlotChunk<Chunk> toReturn = iter.next().getValue();
Iterator<Entry<ChunkWrapper, PlotChunk<Chunk>>> iter = this.blocks.entrySet().iterator();
PlotChunk<Chunk> toReturn = iter.next().getValue();
if (SetQueue.IMP.isWaiting()) {
return null;
}
@ -69,7 +68,7 @@ public class SlowQueue implements PlotQueue<Chunk> {
execute(toReturn);
fixLighting(toReturn, true);
return toReturn;
} catch (final Throwable e) {
} catch (Throwable e) {
e.printStackTrace();
return null;
}
@ -81,17 +80,17 @@ public class SlowQueue implements PlotQueue<Chunk> {
throw new IllegalStateException("Must be called from main thread!");
}
try {
if (blocks.isEmpty()) {
if (this.blocks.isEmpty()) {
return null;
}
final PlotChunk<Chunk> toReturn = blocks.remove(wrap);
PlotChunk<Chunk> toReturn = this.blocks.remove(wrap);
if (toReturn == null) {
return null;
}
execute(toReturn);
fixLighting(toReturn, fixLighting);
return toReturn;
} catch (final Throwable e) {
} catch (Throwable e) {
e.printStackTrace();
return null;
}
@ -99,16 +98,16 @@ public class SlowQueue implements PlotQueue<Chunk> {
@Override
public void clear() {
blocks.clear();
this.blocks.clear();
}
/**
* This should be overriden by any specialized queues
* @param pc
* This should be overriden by any specialized queues.
* @param plotChunk
*/
public void execute(PlotChunk<Chunk> pc) {
SlowChunk sc = (SlowChunk) pc;
Chunk chunk = pc.getChunk();
public void execute(PlotChunk<Chunk> plotChunk) {
SlowChunk sc = (SlowChunk) plotChunk;
Chunk chunk = plotChunk.getChunk();
chunk.loadChunk(true);
Vector3i min = chunk.getBlockMin();
int bx = min.getX();
@ -119,9 +118,9 @@ public class SlowQueue implements PlotQueue<Chunk> {
continue;
}
for (int j = 0; j < 4096; j++) {
final int x = MainUtil.x_loc[i][j];
final int y = MainUtil.y_loc[i][j];
final int z = MainUtil.z_loc[i][j];
int x = MainUtil.x_loc[i][j];
int y = MainUtil.y_loc[i][j];
int z = MainUtil.z_loc[i][j];
PlotBlock newBlock = result2[j];
BlockState state = SpongeUtil.getBlockState(newBlock.id, newBlock.data);
chunk.setBlock(bx + x, y, bz + z, state, false);
@ -146,7 +145,7 @@ public class SlowQueue implements PlotQueue<Chunk> {
}
/**
* This should be overriden by any specialized queues
* This should be overriden by any specialized queues.
* @param wrap
*/
@Override
@ -155,7 +154,7 @@ public class SlowQueue implements PlotQueue<Chunk> {
}
/**
* This should be overriden by any specialized queues
* This should be overriden by any specialized queues.
* @param fixAll
*/
@Override
@ -165,7 +164,7 @@ public class SlowQueue implements PlotQueue<Chunk> {
}
/**
* This should be overriden by any specialized queues
* This should be overriden by any specialized queues.
* @param locs
*/
@Override