PlotSquared/src/main/java/com/plotsquared/sponge/util/SpongeBlockManager.java

243 lines
8.5 KiB
Java
Raw Normal View History

2015-07-30 16:25:16 +02:00
package com.plotsquared.sponge.util;
import java.util.ArrayList;
2015-07-30 16:25:16 +02:00
import java.util.List;
2015-10-07 08:33:33 +02:00
import java.util.Optional;
2015-07-30 16:25:16 +02:00
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.block.BlockTypes;
import org.spongepowered.api.block.tileentity.Sign;
import org.spongepowered.api.block.tileentity.TileEntity;
import org.spongepowered.api.data.manipulator.mutable.tileentity.SignData;
import org.spongepowered.api.data.value.mutable.ListValue;
2015-07-30 16:25:16 +02:00
import org.spongepowered.api.text.Text;
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.biome.BiomeType;
import org.spongepowered.api.world.biome.BiomeTypes;
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.PlotBlock;
import com.intellectualcrafters.plot.object.schematic.PlotItem;
import com.intellectualcrafters.plot.util.BlockManager;
import com.intellectualcrafters.plot.util.MathMan;
import com.intellectualcrafters.plot.util.StringComparison;
import com.plotsquared.sponge.SpongeMain;
2015-09-13 06:04:31 +02:00
public class SpongeBlockManager extends BlockManager {
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public boolean isBlockSolid(final PlotBlock block) {
2015-09-11 12:09:22 +02:00
final BlockState state = SpongeMain.THIS.getBlockState(block);
final BlockType type = state.getType();
2015-10-07 08:33:33 +02:00
// TODO check if solid;
return true;
2015-07-30 16:25:16 +02:00
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public StringComparison<PlotBlock>.ComparisonResult getClosestBlock(String name) {
try {
2015-12-19 20:30:06 +01:00
2015-07-30 16:25:16 +02:00
double match;
short id;
byte data;
2015-09-11 12:09:22 +02:00
final String[] split = name.split(":");
2015-09-13 06:04:31 +02:00
if (split.length == 2) {
2015-07-30 16:25:16 +02:00
data = Byte.parseByte(split[1]);
name = split[0];
2015-09-13 06:04:31 +02:00
} else {
2015-07-30 16:25:16 +02:00
data = 0;
}
2015-09-13 06:04:31 +02:00
if (MathMan.isInteger(split[0])) {
2015-07-30 16:25:16 +02:00
id = Short.parseShort(split[0]);
match = 0;
2015-09-13 06:04:31 +02:00
} else {
final StringComparison<BlockState>.ComparisonResult comparison = new StringComparison<BlockState>(name, SpongeMain.THIS.getAllStates()) {
2015-09-11 12:09:22 +02:00
@Override
2015-09-13 06:04:31 +02:00
public String getString(final BlockState o) {
2015-07-30 16:25:16 +02:00
return o.getType().getId();
};
}.getBestMatchAdvanced();
match = comparison.match;
id = SpongeMain.THIS.getPlotBlock(comparison.best).id;
}
2015-09-11 12:09:22 +02:00
final PlotBlock block = new PlotBlock(id, data);
final StringComparison<PlotBlock> outer = new StringComparison<PlotBlock>();
2015-07-30 16:25:16 +02:00
return outer.new ComparisonResult(match, block);
2015-09-13 06:04:31 +02:00
} catch (final Exception e) {}
2015-07-30 16:25:16 +02:00
return null;
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public String getClosestMatchingName(final PlotBlock block) {
2015-07-30 16:25:16 +02:00
// TODO Auto-generated method stub
return null;
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public String[] getBiomeList() {
2015-07-30 16:25:16 +02:00
// TODO Auto-generated method stub
return null;
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public boolean addItems(final String world, final PlotItem items) {
2015-07-30 16:25:16 +02:00
// TODO Auto-generated method stub
return false;
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public int getBiomeFromString(final String biome) {
2015-07-30 16:25:16 +02:00
// TODO Auto-generated method stub
return 0;
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public PlotBlock getPlotBlockFromString(final String block) {
2015-12-19 20:30:06 +01:00
dsa
2015-07-30 16:25:16 +02:00
// TODO Auto-generated method stub
return null;
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public int getHeighestBlock(final String worldname, final int x, final int z) {
2015-09-11 12:09:22 +02:00
final World world = SpongeUtil.getWorld(worldname);
2015-09-13 06:04:31 +02:00
if (world == null) {
return 64;
}
for (int y = 255; y > 0; y--) {
2015-09-11 12:09:22 +02:00
final BlockState block = world.getBlock(x, y, z);
2015-09-13 06:04:31 +02:00
if ((block != null) && (block.getType() != BlockTypes.AIR)) {
return y + 1;
}
2015-07-30 16:25:16 +02:00
}
return 64;
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public String getBiome(final String world, final int x, final int z) {
2015-07-30 16:25:16 +02:00
return SpongeUtil.getWorld(world).getBiome(x, z).getName().toUpperCase();
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public PlotBlock getBlock(final Location loc) {
2015-09-11 12:09:22 +02:00
final BlockState state = SpongeUtil.getWorld(loc.getWorld()).getBlock(loc.getX(), loc.getY(), loc.getZ());
2015-07-30 16:25:16 +02:00
PlotBlock block = SpongeMain.THIS.getPlotBlock(state);
2015-09-13 06:04:31 +02:00
if (block == null) {
2015-07-30 16:25:16 +02:00
block = SpongeMain.THIS.registerBlock(state);
}
return block;
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public Location getSpawn(final String world) {
2015-09-11 12:09:22 +02:00
final World worldObj = SpongeUtil.getWorld(world);
worldObj.getSpawnLocation();
final Location result = SpongeUtil.getLocation(world, SpongeUtil.getWorld(world).getSpawnLocation());
2015-08-11 14:05:44 +02:00
result.setY(getHeighestBlock(world, result.getX(), result.getZ()));
return result;
2015-07-30 16:25:16 +02:00
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public String[] getSign(final Location loc) {
2015-09-11 12:09:22 +02:00
final World world = SpongeUtil.getWorld(loc.getWorld());
final Optional<TileEntity> block = world.getTileEntity(loc.getX(), loc.getY(), loc.getZ());
2015-09-13 06:04:31 +02:00
if (!block.isPresent()) {
return null;
}
2015-09-11 12:09:22 +02:00
final TileEntity tile = block.get();
2015-09-13 06:04:31 +02:00
if (!(tile instanceof Sign)) {
return null;
}
2015-09-11 12:09:22 +02:00
final Sign sign = (Sign) tile;
final Optional<SignData> optional = sign.getOrCreate(SignData.class);
2015-09-13 06:04:31 +02:00
if (!optional.isPresent()) {
return null;
}
2015-09-11 12:09:22 +02:00
final String[] result = new String[4];
final ListValue<Text> lines = optional.get().lines();
2015-09-13 06:04:31 +02:00
for (int i = 0; i < 4; i++) {
2015-07-30 16:25:16 +02:00
result[i] = lines.get(i).toString();
}
return result;
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public boolean isWorld(final String world) {
2015-07-30 16:25:16 +02:00
return SpongeUtil.getWorld(world) != null;
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void functionSetBlocks(final String worldname, final int[] xv, final int[] yv, final int[] zv, final int[] id, final byte[] data) {
for (int i = 0; i < xv.length; i++) {
2015-07-30 16:25:16 +02:00
functionSetBlock(worldname, xv[i], yv[i], zv[i], id[i], data[i]);
}
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void functionSetSign(final String worldname, final int x, final int y, final int z, final String[] lines) {
2015-09-11 12:09:22 +02:00
final World world = SpongeUtil.getWorld(worldname);
2015-07-30 16:25:16 +02:00
world.setBlock(x, y, z, BlockTypes.WALL_SIGN.getDefaultState());
2015-09-11 12:09:22 +02:00
final Optional<TileEntity> block = world.getTileEntity(x, y, z);
2015-09-13 06:04:31 +02:00
if (!block.isPresent()) {
return;
}
2015-09-11 12:09:22 +02:00
final TileEntity tile = block.get();
2015-09-13 06:04:31 +02:00
if (!(tile instanceof Sign)) {
return;
}
2015-09-11 12:09:22 +02:00
final Sign sign = (Sign) tile;
final List<Text> text = new ArrayList<>(4);
2015-09-13 06:04:31 +02:00
for (int i = 0; i < 4; i++) {
text.add(SpongeMain.THIS.getText(lines[i]));
2015-07-30 16:25:16 +02:00
}
2015-09-13 06:04:31 +02:00
try {
2015-09-11 12:09:22 +02:00
final Optional<SignData> optional = sign.getOrCreate(SignData.class);
2015-09-13 06:04:31 +02:00
if (optional.isPresent()) {
2015-09-11 12:09:22 +02:00
final SignData offering = optional.get();
2015-08-14 00:52:31 +02:00
offering.lines().set(0, SpongeMain.THIS.getText(lines[0]));
offering.lines().set(1, SpongeMain.THIS.getText(lines[1]));
offering.lines().set(2, SpongeMain.THIS.getText(lines[2]));
offering.lines().set(3, SpongeMain.THIS.getText(lines[3]));
sign.offer(offering);
}
2015-09-13 06:04:31 +02:00
} catch (final NullPointerException e) {
e.printStackTrace();
}
2015-07-30 16:25:16 +02:00
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void functionSetBlock(final String worldname, final int x, final int y, final int z, final int id, final byte data) {
2015-07-30 16:25:16 +02:00
BlockState state;
2015-09-13 06:04:31 +02:00
if (data == 0) {
2015-07-30 16:25:16 +02:00
state = SpongeMain.THIS.getBlockState(id);
2015-09-13 06:04:31 +02:00
} else {
2015-07-30 16:25:16 +02:00
state = SpongeMain.THIS.getBlockState(new PlotBlock((short) id, data));
}
2015-09-13 06:04:31 +02:00
if (state == null) {
return;
}
2015-09-11 12:09:22 +02:00
final World world = SpongeUtil.getWorld(worldname);
final BlockState block = world.getBlock(x, y, z);
2015-09-13 06:04:31 +02:00
if (block != state) {
2015-08-03 20:20:04 +02:00
world.setBlock(x, y, z, state);
}
2015-07-30 16:25:16 +02:00
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void functionSetBiomes(final String worldname, final int[] xv, final int[] zv, final String biomeName) {
2015-07-30 16:25:16 +02:00
BiomeType biome;
2015-09-13 06:04:31 +02:00
try {
2015-07-30 16:25:16 +02:00
biome = (BiomeType) BiomeTypes.class.getField(biomeName.toUpperCase()).get(null);
2015-09-13 06:04:31 +02:00
} catch (final Exception e) {
2015-07-30 16:25:16 +02:00
e.printStackTrace();
biome = BiomeTypes.FOREST;
}
2015-09-13 06:04:31 +02:00
for (int i = 0; i < xv.length; i++) {
2015-07-30 16:25:16 +02:00
SpongeUtil.getWorld(worldname).setBiome(xv[i], zv[i], biome);
}
}
2015-09-13 06:04:31 +02:00
2015-07-30 16:25:16 +02:00
}