mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 10:30:42 +00:00
Add serializable mob spawners and banners
This commit is contained in:
@@ -62,6 +62,7 @@ public class TreeConfig extends TerraConfig implements Tree {
|
||||
|
||||
@Override
|
||||
public boolean plant(Location location, Random random, JavaPlugin javaPlugin) {
|
||||
location.subtract(0, 1, 0);
|
||||
Location mut = location.clone().subtract(0, yOffset, 0);
|
||||
if(!spawnable.contains(location.getBlock().getType())) return false;
|
||||
Structure struc = structure.get(random);
|
||||
|
||||
@@ -138,7 +138,9 @@ public class Structure implements Serializable {
|
||||
* @param r Rotation
|
||||
*/
|
||||
public void paste(@NotNull Location origin, Rotation r) {
|
||||
this.executeForBlocksInRange(getRange(Axis.X), getRange(Axis.Y), getRange(Axis.Z), block -> pasteBlock(block, origin, r), r);
|
||||
Range xRange = getRange(Axis.X);
|
||||
Range zRange = getRange(Axis.Z);
|
||||
this.executeForBlocksInRange(xRange, getRange(Axis.Y), zRange, block -> pasteBlock(block, origin, r), r);
|
||||
}
|
||||
|
||||
public boolean checkSpawns(Location origin, Rotation r) {
|
||||
@@ -184,6 +186,7 @@ public class Structure implements Serializable {
|
||||
Location loc = origin.clone().add(block.getX(), block.getY(), block.getZ());
|
||||
Block worldBlock = loc.getBlock();
|
||||
main: while(worldBlock.isEmpty()) {
|
||||
if(loc.getBlockY() > 255 || loc.getBlockY() < 0) return;
|
||||
if(block.getPull() == null) break;
|
||||
switch(block.getPull()) {
|
||||
case UP:
|
||||
@@ -194,7 +197,7 @@ public class Structure implements Serializable {
|
||||
break;
|
||||
default: break main;
|
||||
}
|
||||
if(loc.getBlockY() > 255 || loc.getBlockY() < 0) return;
|
||||
|
||||
}
|
||||
int offset = block.getPullOffset();
|
||||
if(offset != 0) worldBlock = worldBlock.getRelative((offset > 0) ? BlockFace.UP : BlockFace.DOWN, Math.abs(offset));
|
||||
@@ -207,10 +210,12 @@ public class Structure implements Serializable {
|
||||
((Directional) data).setFacing(rt);
|
||||
} else if(data instanceof MultipleFacing) {
|
||||
MultipleFacing mfData = (MultipleFacing) data;
|
||||
List<BlockFace> faces = new ArrayList<>(mfData.getFaces());
|
||||
for(BlockFace face : faces) {
|
||||
mfData.setFace(face, false);
|
||||
mfData.setFace(getRotatedFace(face, r), true);
|
||||
Map<BlockFace, Boolean> faces = new HashMap<>();
|
||||
for(BlockFace f : mfData.getAllowedFaces()) {
|
||||
faces.put(f, mfData.hasFace(f));
|
||||
}
|
||||
for(Map.Entry<BlockFace, Boolean> face : faces.entrySet()) {
|
||||
mfData.setFace(getRotatedFace(face.getKey(), r), face.getValue());
|
||||
}
|
||||
} else if(data instanceof Rail) {
|
||||
Rail.Shape newShape = getRotatedRail(((Rail) data).getShape(), r);
|
||||
@@ -225,7 +230,7 @@ public class Structure implements Serializable {
|
||||
connections.put(f, rData.getFace(f));
|
||||
}
|
||||
for(Map.Entry<BlockFace, RedstoneWire.Connection> e : connections.entrySet()) {
|
||||
rData.setFace(RotationUtil.getRotatedFace(e.getKey(), r), e.getValue());
|
||||
rData.setFace(getRotatedFace(e.getKey(), r), e.getValue());
|
||||
}
|
||||
}
|
||||
worldBlock.setBlockData(data, false);
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package com.dfsek.terra.structure;
|
||||
|
||||
import com.dfsek.terra.structure.serialize.SerializableBlockData;
|
||||
import com.dfsek.terra.structure.serialize.block.SerializableBanner;
|
||||
import com.dfsek.terra.structure.serialize.block.SerializableBlockState;
|
||||
import com.dfsek.terra.structure.serialize.block.SerializableMonsterCage;
|
||||
import com.dfsek.terra.structure.serialize.block.SerializableSign;
|
||||
import org.bukkit.block.Banner;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.block.CreatureSpawner;
|
||||
import org.bukkit.block.Sign;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
|
||||
@@ -23,6 +27,10 @@ public class StructureContainedBlock implements Serializable {
|
||||
public StructureContainedBlock(int x, int y, int z, BlockState state, BlockData d, StructureSpawnRequirement spawn, Pull pull, int pullOffset) {
|
||||
if(state instanceof Sign) {
|
||||
this.state = new SerializableSign((org.bukkit.block.Sign) state);
|
||||
} else if(state instanceof CreatureSpawner) {
|
||||
this.state = new SerializableMonsterCage((CreatureSpawner) state);
|
||||
} else if(state instanceof Banner) {
|
||||
this.state = new SerializableBanner((Banner) state);
|
||||
} else this.state = null;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.dfsek.terra.structure.serialize;
|
||||
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class SerializableEnchantment implements Serializable {
|
||||
public static final long serialVersionUID = 5298928608478640006L;
|
||||
private final SerializableNamespacedKey enchant;
|
||||
public SerializableEnchantment(Enchantment e) {
|
||||
enchant = new SerializableNamespacedKey(e.getKey());
|
||||
}
|
||||
public Enchantment toEnchantment() {
|
||||
return Enchantment.getByKey(enchant.getNamespacedKey());
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.dfsek.terra.structure.serialize;
|
||||
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class SerializableInventory implements Serializable {
|
||||
public static final long serialVersionUID = 5298928608478640004L;
|
||||
private SerializableItemStack[] items;
|
||||
public SerializableInventory(Inventory inv) {
|
||||
items = new SerializableItemStack[inv.getSize()];
|
||||
for(int i = 0; i < inv.getSize(); i++) {
|
||||
ItemStack item = inv.getItem(i);
|
||||
if(item == null) return;
|
||||
items[i] = new SerializableItemStack(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.dfsek.terra.structure.serialize;
|
||||
|
||||
import org.bukkit.attribute.Attribute;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class SerializableItemMeta implements Serializable {
|
||||
public static final long serialVersionUID = 5298928608478640009L;
|
||||
public SerializableItemMeta(ItemMeta meta) {
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.dfsek.terra.structure.serialize;
|
||||
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SerializableItemStack implements Serializable {
|
||||
public static final long serialVersionUID = 5298928608478640005L;
|
||||
private final HashMap<SerializableEnchantment, Integer> enchantments = new HashMap<>();
|
||||
private final SerializableBlockData blockData;
|
||||
private transient ItemStack stack;
|
||||
private final SerializableItemMeta meta;
|
||||
private final int amount;
|
||||
public SerializableItemStack(ItemStack item) {
|
||||
for(Map.Entry<Enchantment, Integer> e : item.getEnchantments().entrySet()) {
|
||||
this.enchantments.put(new SerializableEnchantment(e.getKey()), e.getValue());
|
||||
}
|
||||
this.meta = new SerializableItemMeta(item.getItemMeta());
|
||||
this.blockData = new SerializableBlockData(item.getType().createBlockData());
|
||||
this.amount = item.getAmount();
|
||||
}
|
||||
public ItemStack getStack() {
|
||||
if(stack == null) {
|
||||
stack = new ItemStack(blockData.getData().getMaterial(), amount);
|
||||
}
|
||||
return stack;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.dfsek.terra.structure.serialize;
|
||||
|
||||
import org.bukkit.NamespacedKey;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class SerializableNamespacedKey implements Serializable {
|
||||
public static final long serialVersionUID = 5298928608478640007L;
|
||||
private final String namespace;
|
||||
private final String key;
|
||||
public SerializableNamespacedKey(NamespacedKey key) {
|
||||
this.namespace = key.getNamespace();
|
||||
this.key = key.getKey();
|
||||
}
|
||||
public NamespacedKey getNamespacedKey() {
|
||||
return new NamespacedKey(namespace, key);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.dfsek.terra.structure.serialize.block;
|
||||
|
||||
import org.bukkit.DyeColor;
|
||||
import org.bukkit.block.Banner;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.block.Sign;
|
||||
import org.bukkit.block.banner.Pattern;
|
||||
import org.bukkit.block.banner.PatternType;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class SerializableBanner implements SerializableBlockState {
|
||||
private final DyeColor base;
|
||||
private final ArrayList<Pattern> patterns = new ArrayList<>();
|
||||
|
||||
public SerializableBanner(Banner banner) {
|
||||
this.base = banner.getBaseColor();
|
||||
for(org.bukkit.block.banner.Pattern bukkitPattern : banner.getPatterns()) {
|
||||
patterns.add(new Pattern(bukkitPattern.getPattern(), bukkitPattern.getColor()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockState getState(BlockState orig) {
|
||||
if(!(orig instanceof Banner)) throw new IllegalArgumentException("Provided BlockState is not a banner.");
|
||||
Banner banner = (Banner) orig;
|
||||
banner.setBaseColor(base);
|
||||
for(Pattern pattern : patterns) {
|
||||
banner.addPattern(new org.bukkit.block.banner.Pattern(pattern.getColor(), pattern.getType()));
|
||||
}
|
||||
return banner;
|
||||
}
|
||||
private static final class Pattern implements Serializable {
|
||||
private final DyeColor color;
|
||||
private final PatternType type;
|
||||
public Pattern(PatternType type, DyeColor color) {
|
||||
this.color = color;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public DyeColor getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public PatternType getType() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.dfsek.terra.structure.serialize.block;
|
||||
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
||||
public class SerializableChest implements SerializableContainer {
|
||||
long serialVersionUID = 5298928608478640003L;
|
||||
@Override
|
||||
public Inventory getInventory(Inventory orig) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockState getState(BlockState orig) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.dfsek.terra.structure.serialize.block;
|
||||
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
||||
public interface SerializableContainer extends SerializableBlockState {
|
||||
long serialVersionUID = 5298928608478640002L;
|
||||
Inventory getInventory(Inventory orig);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.dfsek.terra.structure.serialize.block;
|
||||
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.block.CreatureSpawner;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.polydev.gaea.math.Range;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class SerializableMonsterCage implements SerializableBlockState {
|
||||
private final EntityType type;
|
||||
private final int minDelay;
|
||||
private final int maxDelay;
|
||||
private final int maxNear;
|
||||
private final int playerRange;
|
||||
private final int delay;
|
||||
private final int count;
|
||||
public SerializableMonsterCage(CreatureSpawner orig) {
|
||||
this.type = orig.getSpawnedType();
|
||||
this.minDelay = orig.getMinSpawnDelay();
|
||||
this.maxDelay = orig.getMaxSpawnDelay();
|
||||
this.maxNear = orig.getMaxNearbyEntities();
|
||||
this.playerRange = orig.getRequiredPlayerRange();
|
||||
this.delay = orig.getDelay();
|
||||
this.count = orig.getSpawnCount();
|
||||
}
|
||||
@Override
|
||||
public BlockState getState(BlockState orig) {
|
||||
if(!(orig instanceof CreatureSpawner)) throw new IllegalArgumentException("BlockState is not a Monster Spawner!");
|
||||
CreatureSpawner spawner = (CreatureSpawner) orig;
|
||||
spawner.setSpawnedType(type);
|
||||
spawner.setMinSpawnDelay(minDelay);
|
||||
spawner.setMaxSpawnDelay(maxDelay);
|
||||
spawner.setMaxNearbyEntities(maxNear);
|
||||
spawner.setRequiredPlayerRange(playerRange);
|
||||
spawner.setDelay(delay);
|
||||
spawner.setSpawnCount(count);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user