own world per pixelblock, live update, owners
This commit is contained in:
@@ -7,12 +7,11 @@ import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.util.Transformation;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Pixel {
|
||||
Location relativeLocation;
|
||||
BlockData blockData;
|
||||
public Location relativeLocation;
|
||||
public BlockData blockData;
|
||||
double scale;
|
||||
double offset;
|
||||
|
||||
|
||||
@@ -1,74 +1,152 @@
|
||||
package eu.mhsl.minecraft.pixelblocks.pixelblock;
|
||||
|
||||
import eu.mhsl.minecraft.pixelblocks.PixelBlocks;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import eu.mhsl.minecraft.pixelblocks.chunkGenerators.EmptyChunkGenerator;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Interaction;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
|
||||
import static eu.mhsl.minecraft.pixelblocks.PixelBlocks.plugin;
|
||||
|
||||
public class PixelBlock {
|
||||
public static List<PixelBlock> placedBlocks = new ArrayList<>();
|
||||
public static float hitboxOffset = 0.005F;
|
||||
public static int worldGrassBorderWidth = 5;
|
||||
|
||||
public Location pixelBlockLocation;
|
||||
double pixelsPerBlock;
|
||||
public double pixelsPerBlock;
|
||||
public ArrayList<Pixel> pixels = new ArrayList<>();
|
||||
public Interaction hitbox;
|
||||
|
||||
public static float hitboxOffset = 0.005F;
|
||||
|
||||
public long lastEntryTime;
|
||||
public Location lastEntryLocation;
|
||||
public UUID owner;
|
||||
public UUID uuid;
|
||||
public int updateTaskID;
|
||||
|
||||
public PixelBlock(Location originLocation, UUID owner, double pixelsPerBlock) {
|
||||
PixelBlock.placedBlocks.add(this);
|
||||
this.uuid = UUID.randomUUID();
|
||||
|
||||
this.pixelBlockLocation = originLocation.toBlockLocation();
|
||||
this.pixelBlockLocation.setYaw(0);
|
||||
this.pixelBlockLocation.setPitch(0);
|
||||
this.pixelsPerBlock = pixelsPerBlock;
|
||||
this.owner = owner;
|
||||
|
||||
for (int x = 0; x < pixelsPerBlock; x++) {
|
||||
for (int y = 0; y < pixelsPerBlock; y++) {
|
||||
for (int z = 0; z < pixelsPerBlock; z++) {
|
||||
Location relativeLocation = new Location(originLocation.getWorld(), x, y, z);
|
||||
Location blockLocation = pixelBlockLocation.clone().add(relativeLocation);
|
||||
BlockData block = blockLocation.getBlock().getBlockData();
|
||||
|
||||
if(block.getMaterial() != Material.AIR) {
|
||||
Pixel newPixel = new Pixel(relativeLocation, block, (1/pixelsPerBlock)-0.0001, 0.00005*pixelsPerBlock);
|
||||
pixels.add(newPixel);
|
||||
}
|
||||
}
|
||||
for (int x = 0; x < 16; x++) {
|
||||
for (int z = 0; z < 16; z++) {
|
||||
Pixel newPixel = new Pixel(
|
||||
new Location(originLocation.getWorld(), x, 0, z),
|
||||
Material.GRASS_BLOCK.createBlockData(),
|
||||
(1/pixelsPerBlock)-0.0001,
|
||||
0.00005*pixelsPerBlock
|
||||
);
|
||||
pixels.add(newPixel);
|
||||
}
|
||||
}
|
||||
|
||||
pixelBlockLocation.getBlock().setType(Material.GLASS);
|
||||
|
||||
// BlockDisplay bd = (BlockDisplay) spawnLocation.getWorld().spawnEntity(
|
||||
// spawnLocation,
|
||||
// EntityType.BLOCK_DISPLAY
|
||||
// );
|
||||
|
||||
hitbox = (Interaction) pixelBlockLocation.getWorld().spawnEntity(
|
||||
pixelBlockLocation.clone().add(0.5, -hitboxOffset, 0.5),
|
||||
EntityType.INTERACTION
|
||||
);
|
||||
hitbox.setInteractionHeight(1F + 2*hitboxOffset);
|
||||
hitbox.setInteractionWidth(1F + 2*hitboxOffset);
|
||||
|
||||
createWorld();
|
||||
}
|
||||
|
||||
void createWorld() {
|
||||
final WorldCreator worldCreator = new WorldCreator(plugin.getDataFolder().getPath()+"/"+this.uuid);
|
||||
worldCreator.type(WorldType.FLAT);
|
||||
worldCreator.generator(new EmptyChunkGenerator());
|
||||
World newWorld = Bukkit.createWorld(worldCreator);
|
||||
|
||||
assert newWorld != null;
|
||||
Location borderStartLocation = newWorld.getSpawnLocation().clone().subtract(1, 1, 1);
|
||||
Location grassStartLocation = borderStartLocation.clone().subtract(worldGrassBorderWidth, 0, worldGrassBorderWidth);
|
||||
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
for (int x = 0; x < 18+2*worldGrassBorderWidth; x++) {
|
||||
for (int z = 0; z < 18+2*worldGrassBorderWidth; z++) {
|
||||
grassStartLocation.clone().add(x, 0, z).getBlock().setType(Material.GRASS_BLOCK);
|
||||
}
|
||||
}
|
||||
for (int x = 0; x < 18+2*worldGrassBorderWidth; x++) {
|
||||
for (int z = 0; z < 18+2*worldGrassBorderWidth; z++) {
|
||||
grassStartLocation.clone().add(x, -1, z).getBlock().setType(Material.DIRT);
|
||||
}
|
||||
}
|
||||
for (int x = 0; x < 18; x++) {
|
||||
for (int z = 0; z < 18; z++) {
|
||||
Location currentLocation = borderStartLocation.clone().add(x, 0, z);
|
||||
if(currentLocation.x() == borderStartLocation.x() || currentLocation.z() == borderStartLocation.z()) {
|
||||
currentLocation.getBlock().setType(Material.RED_CONCRETE);
|
||||
} else if (currentLocation.x() == borderStartLocation.x()+17 || currentLocation.z() == borderStartLocation.z()+17) {
|
||||
currentLocation.getBlock().setType(Material.RED_CONCRETE);
|
||||
}
|
||||
}
|
||||
}
|
||||
borderStartLocation.getBlock().setType(Material.WHITE_CONCRETE);
|
||||
});
|
||||
}
|
||||
|
||||
public void update() {
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
Player owner = null;
|
||||
for(Player player : Bukkit.getOnlinePlayers()) {
|
||||
if(player.getUniqueId() == this.owner) {
|
||||
owner = player;
|
||||
}
|
||||
}
|
||||
|
||||
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)};
|
||||
if(owner == null || Arrays.stream(standardWorlds).toList().contains(owner.getWorld())) {
|
||||
Bukkit.getScheduler().cancelTask(this.updateTaskID);
|
||||
}
|
||||
|
||||
for(Pixel pixel : this.pixels) { pixel.remove(); }
|
||||
pixels.clear();
|
||||
|
||||
for (int x = 0; x < 16; x++) {
|
||||
for (int y = 0; y < 16; y++) {
|
||||
for (int z = 0; z < 16; z++) {
|
||||
Location relativeLocation = new Location(pixelBlockLocation.getWorld(), x, y, z);
|
||||
Location blockLocation = Objects.requireNonNull(Bukkit
|
||||
.getWorld(plugin.getDataFolder().getPath() + "/" + this.uuid))
|
||||
.getSpawnLocation()
|
||||
.clone()
|
||||
.subtract(0, 1, 0)
|
||||
.add(relativeLocation.x(), relativeLocation.y(), relativeLocation.z());
|
||||
BlockData block = blockLocation.getBlock().getBlockData();
|
||||
|
||||
if(block.getMaterial() != Material.AIR) {
|
||||
Pixel newPixel = new Pixel(relativeLocation, block, (1/pixelsPerBlock)-0.0001, 0.00005*pixelsPerBlock);
|
||||
pixels.add(newPixel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(Pixel pixel : this.pixels) {
|
||||
pixel.spawn(this.pixelBlockLocation);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void place(Location placeLocation) {
|
||||
Bukkit.getScheduler().runTask(PixelBlocks.plugin, () -> {
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
for(Pixel pixel : pixels) {
|
||||
pixel.spawn(placeLocation.toBlockLocation());
|
||||
}
|
||||
});
|
||||
|
||||
this.updateTaskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this::update, 100L, 100L);
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
@@ -76,5 +154,6 @@ public class PixelBlock {
|
||||
hitbox.remove();
|
||||
pixelBlockLocation.getBlock().setType(Material.AIR);
|
||||
placedBlocks.remove(this);
|
||||
Bukkit.getScheduler().cancelTask(this.updateTaskID);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user