use of org.bukkit.util.BlockVector in project Denizen-For-Bukkit by DenizenScript.
the class CuboidBlockSet method fromMCEditStream.
public static CuboidBlockSet fromMCEditStream(InputStream is) {
CuboidBlockSet cbs = new CuboidBlockSet();
try {
NBTInputStream nbtStream = new NBTInputStream(new GZIPInputStream(is));
NamedTag rootTag = nbtStream.readNamedTag();
nbtStream.close();
if (!rootTag.getName().equals("Schematic")) {
throw new Exception("Tag 'Schematic' does not exist or is not first!");
}
CompoundTag schematicTag = (CompoundTag) rootTag.getTag();
Map<String, Tag> schematic = schematicTag.getValue();
short width = (getChildTag(schematic, "Width", ShortTag.class).getValue());
short length = (getChildTag(schematic, "Length", ShortTag.class).getValue());
short height = (getChildTag(schematic, "Height", ShortTag.class).getValue());
int originX = 0;
int originY = 0;
int originZ = 0;
try {
originX = getChildTag(schematic, "DenizenOriginX", IntTag.class).getValue();
originY = getChildTag(schematic, "DenizenOriginY", IntTag.class).getValue();
originZ = getChildTag(schematic, "DenizenOriginZ", IntTag.class).getValue();
} catch (Exception e) {
// Default origin, why not
}
cbs.x_width = width;
cbs.z_height = length;
cbs.y_length = height;
cbs.center_x = originX;
cbs.center_y = originY;
cbs.center_z = originZ;
// Disregard Offset
String materials = getChildTag(schematic, "Materials", StringTag.class).getValue();
if (!materials.equals("Alpha")) {
throw new Exception("Schematic file is not an Alpha schematic!");
}
byte[] blockId = getChildTag(schematic, "Blocks", ByteArrayTag.class).getValue();
byte[] blockData = getChildTag(schematic, "Data", ByteArrayTag.class).getValue();
byte[] addId = new byte[0];
short[] blocks = new short[blockId.length];
if (schematic.containsKey("AddBlocks")) {
addId = getChildTag(schematic, "AddBlocks", ByteArrayTag.class).getValue();
}
for (int index = 0; index < blockId.length; index++) {
if ((index >> 1) >= addId.length) {
blocks[index] = (short) (blockId[index] & 0xFF);
} else {
if ((index & 1) == 0) {
blocks[index] = (short) (((addId[index >> 1] & 0x0F) << 8) + (blockId[index] & 0xFF));
} else {
blocks[index] = (short) (((addId[index >> 1] & 0xF0) << 4) + (blockId[index] & 0xFF));
}
}
}
List<Tag> tileEntities = getChildTag(schematic, "TileEntities", ListTag.class).getValue();
Map<BlockVector, Map<String, Tag>> tileEntitiesMap = new HashMap<BlockVector, Map<String, Tag>>();
for (Tag tag : tileEntities) {
if (!(tag instanceof CompoundTag)) {
continue;
}
CompoundTag t = (CompoundTag) tag;
int x = 0;
int y = 0;
int z = 0;
Map<String, Tag> values = new HashMap<String, Tag>();
for (Map.Entry<String, Tag> entry : t.getValue().entrySet()) {
if (entry.getKey().equals("x")) {
if (entry.getValue() instanceof IntTag) {
x = ((IntTag) entry.getValue()).getValue();
}
} else if (entry.getKey().equals("y")) {
if (entry.getValue() instanceof IntTag) {
y = ((IntTag) entry.getValue()).getValue();
}
} else if (entry.getKey().equals("z")) {
if (entry.getValue() instanceof IntTag) {
z = ((IntTag) entry.getValue()).getValue();
}
}
values.put(entry.getKey(), entry.getValue());
}
BlockVector vec = new BlockVector(x, y, z);
tileEntitiesMap.put(vec, values);
}
org.bukkit.util.Vector vec = new Vector(width, height, length);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
for (int z = 0; z < length; z++) {
int index = y * width * length + z * width + x;
BlockVector pt = new BlockVector(x, y, z);
BlockData block = NMSHandler.getInstance().getBlockHelper().getBlockData(blocks[index], blockData[index]);
if (tileEntitiesMap.containsKey(pt)) {
CompoundTag otag = NMSHandler.getInstance().createCompoundTag(tileEntitiesMap.get(pt));
block.setCompoundTag(otag);
}
cbs.blocks.add(block);
}
}
}
} catch (Exception e) {
dB.echoError(e);
}
return cbs;
}
use of org.bukkit.util.BlockVector in project Glowstone by GlowstoneMC.
the class GlowPlayer method processBlockChanges.
/**
* Process and send pending BlockChangeMessages.
*/
private void processBlockChanges() {
synchronized (blockChanges) {
List<BlockChangeMessage> messages = new ArrayList<>(blockChanges);
blockChanges.clear();
// separate messages by chunk
// inner map is used to only send one entry for same coordinates
Map<Key, Map<BlockVector, BlockChangeMessage>> chunks = new HashMap<>();
for (BlockChangeMessage message : messages) {
if (message != null) {
Key key = new Key(message.getX() >> 4, message.getZ() >> 4);
if (canSeeChunk(key)) {
Map<BlockVector, BlockChangeMessage> map = chunks.computeIfAbsent(key, k -> new HashMap<>());
map.put(new BlockVector(message.getX(), message.getY(), message.getZ()), message);
}
}
}
// send away
for (Map.Entry<Key, Map<BlockVector, BlockChangeMessage>> entry : chunks.entrySet()) {
Key key = entry.getKey();
List<BlockChangeMessage> value = new ArrayList<>(entry.getValue().values());
if (value.size() == 1) {
session.send(value.get(0));
} else if (value.size() > 1) {
session.send(new MultiBlockChangeMessage(key.getX(), key.getZ(), value));
}
}
// now send post-block-change messages
List<Message> postMessages = new ArrayList<>(afterBlockChanges);
afterBlockChanges.clear();
postMessages.forEach(session::send);
}
}
use of org.bukkit.util.BlockVector in project Glowstone by GlowstoneMC.
the class Explosion method explodeWithEvent.
public boolean explodeWithEvent() {
if (power < 0.1f)
return true;
Set<BlockVector> droppedBlocks = calculateBlocks();
EntityExplodeEvent event = EventFactory.callEvent(new EntityExplodeEvent(source, location, toBlockList(droppedBlocks), yield));
if (event.isCancelled())
return false;
yield = event.getYield();
playOutSoundAndParticles();
List<Block> blocks = toBlockList(droppedBlocks);
for (Block block : blocks) {
handleBlockExplosion((GlowBlock) block);
}
if (incendiary) {
for (Block block : blocks) {
setBlockOnFire((GlowBlock) block);
}
}
Collection<GlowPlayer> affectedPlayers = damageEntities();
for (GlowPlayer player : affectedPlayers) {
playOutExplosion(player, droppedBlocks);
}
return true;
}
use of org.bukkit.util.BlockVector in project Glowstone by GlowstoneMC.
the class UseBedCodec method decode.
@Override
public UseBedMessage decode(ByteBuf buf) throws IOException {
int id = ByteBufUtils.readVarInt(buf);
BlockVector pos = GlowBufUtils.readBlockPosition(buf);
return new UseBedMessage(id, pos.getBlockX(), pos.getBlockY(), pos.getBlockZ());
}
use of org.bukkit.util.BlockVector in project Glowstone by GlowstoneMC.
the class TabCompleteCodec method decode.
@Override
public TabCompleteMessage decode(ByteBuf buf) throws IOException {
String text = ByteBufUtils.readUTF8(buf);
boolean assumeCommand = buf.readBoolean();
boolean hasLocation = buf.readBoolean();
BlockVector location = null;
if (hasLocation) {
location = GlowBufUtils.readBlockPosition(buf);
}
return new TabCompleteMessage(text, assumeCommand, location);
}
Aggregations