Added Item functionalities and portal

This commit is contained in:
Lars Neuhaus 2024-07-17 15:03:58 +02:00
parent ea2bd45a4b
commit f6c23e723d
9 changed files with 273 additions and 42 deletions

View File

@ -67,7 +67,7 @@ public class DataBase {
"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);" "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
); );
prep.setString(1, pixelBlock.uuid.toString()); prep.setString(1, pixelBlock.uuid.toString());
prep.setString(2, pixelBlock.owner.toString()); prep.setString(2, pixelBlock.ownerUID.toString());
prep.setString(3, pixelBlock.pixelBlockLocation.getWorld().getName()); prep.setString(3, pixelBlock.pixelBlockLocation.getWorld().getName());
prep.setDouble(4, pixelBlock.pixelBlockLocation.getX()); prep.setDouble(4, pixelBlock.pixelBlockLocation.getX());
@ -100,7 +100,7 @@ public class DataBase {
"entryLocationWorldName=?, entryLocationX=?, entryLocationY=?, entryLocationZ=? " + "entryLocationWorldName=?, entryLocationX=?, entryLocationY=?, entryLocationZ=? " +
"WHERE uuid=?;" "WHERE uuid=?;"
); );
prep.setString(1, pixelBlock.owner.toString()); prep.setString(1, pixelBlock.ownerUID.toString());
prep.setString(2, pixelBlock.pixelBlockLocation.getWorld().getName()); prep.setString(2, pixelBlock.pixelBlockLocation.getWorld().getName());
prep.setDouble(3, pixelBlock.pixelBlockLocation.getX()); prep.setDouble(3, pixelBlock.pixelBlockLocation.getX());
@ -133,13 +133,13 @@ public class DataBase {
public void loadPixelBlocks() { public void loadPixelBlocks() {
List<Entity> entities = new ArrayList<>(); List<Entity> entities = new ArrayList<>();
entities.addAll(Bukkit.getWorlds().get(0).getEntities().stream() entities.addAll(Bukkit.getWorlds().get(0).getEntities().stream()
.filter(entity -> entity.getType().equals(EntityType.BLOCK_DISPLAY) || entity.getType().equals(EntityType.INTERACTION)) .filter(entity -> entity.getType().equals(EntityType.BLOCK_DISPLAY) || entity.getType().equals(EntityType.INTERACTION) || entity.getType().equals(EntityType.ITEM_DISPLAY))
.toList()); .toList());
entities.addAll(Bukkit.getWorlds().get(1).getEntities().stream() entities.addAll(Bukkit.getWorlds().get(1).getEntities().stream()
.filter(entity -> entity.getType().equals(EntityType.BLOCK_DISPLAY) || entity.getType().equals(EntityType.INTERACTION)) .filter(entity -> entity.getType().equals(EntityType.BLOCK_DISPLAY) || entity.getType().equals(EntityType.INTERACTION) || entity.getType().equals(EntityType.ITEM_DISPLAY))
.toList()); .toList());
entities.addAll(Bukkit.getWorlds().get(2).getEntities().stream() entities.addAll(Bukkit.getWorlds().get(2).getEntities().stream()
.filter(entity -> entity.getType().equals(EntityType.BLOCK_DISPLAY) || entity.getType().equals(EntityType.INTERACTION)) .filter(entity -> entity.getType().equals(EntityType.BLOCK_DISPLAY) || entity.getType().equals(EntityType.INTERACTION) || entity.getType().equals(EntityType.ITEM_DISPLAY))
.toList()); .toList());
String sql = "CREATE TABLE IF NOT EXISTS pixelblocks (" + String sql = "CREATE TABLE IF NOT EXISTS pixelblocks (" +

View File

@ -3,6 +3,12 @@ package eu.mhsl.minecraft.pixelblocks;
import eu.mhsl.minecraft.pixelblocks.commands.CreatePixelBlockCommand; import eu.mhsl.minecraft.pixelblocks.commands.CreatePixelBlockCommand;
import eu.mhsl.minecraft.pixelblocks.commands.ExitWorldCommand; import eu.mhsl.minecraft.pixelblocks.commands.ExitWorldCommand;
import eu.mhsl.minecraft.pixelblocks.listeners.*; import eu.mhsl.minecraft.pixelblocks.listeners.*;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.util.Objects; import java.util.Objects;
@ -26,10 +32,29 @@ public final class PixelBlocks extends JavaPlugin {
getServer().getPluginManager().registerEvents(new BlockBreakListener(), this); getServer().getPluginManager().registerEvents(new BlockBreakListener(), this);
getServer().getPluginManager().registerEvents(new BlockPlaceListener(), this); getServer().getPluginManager().registerEvents(new BlockPlaceListener(), this);
getServer().getPluginManager().registerEvents(new CreatureSpawnListener(), this); getServer().getPluginManager().registerEvents(new CreatureSpawnListener(), this);
getServer().getPluginManager().registerEvents(new EntityExplodeListener(), this);
getServer().getPluginManager().registerEvents(new BlockExplodeListener(), this);
getServer().getPluginManager().registerEvents(new PlayerPortalListener(), this);
getServer().getPluginManager().registerEvents(new PlayerChangeWorldListener(), this); getServer().getPluginManager().registerEvents(new PlayerChangeWorldListener(), this);
Objects.requireNonNull(getCommand("createpixelblock")).setExecutor(new CreatePixelBlockCommand()); Objects.requireNonNull(getCommand("createpixelblock")).setExecutor(new CreatePixelBlockCommand());
Objects.requireNonNull(getCommand("exitworld")).setExecutor(new ExitWorldCommand()); Objects.requireNonNull(getCommand("exitworld")).setExecutor(new ExitWorldCommand());
ItemStack item = ItemStack.of(Material.GRAY_STAINED_GLASS);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("Pixelblock");
meta.setEnchantmentGlintOverride(true);
meta.setItemName("Pixelblock");
item.setItemMeta(meta);
NamespacedKey key = new NamespacedKey(this, "pixelblock");
ShapedRecipe recipe = new ShapedRecipe(key, item);
recipe.shape("ABA", "BCB", "ABA");
recipe.setIngredient('A', Material.DIAMOND_BLOCK);
recipe.setIngredient('B', Material.EMERALD_BLOCK);
recipe.setIngredient('C', Material.GRAY_STAINED_GLASS);
Bukkit.addRecipe(recipe);
} }
} }

View File

@ -22,7 +22,7 @@ public class ExitWorldCommand implements CommandExecutor {
if(!Arrays.stream(standardWorlds).toList().contains(playerWorld)) { if(!Arrays.stream(standardWorlds).toList().contains(playerWorld)) {
List<PixelBlock> ownedBlocks = PixelBlock.placedBlocks.stream() List<PixelBlock> ownedBlocks = PixelBlock.placedBlocks.stream()
.filter(pixelBlock -> pixelBlock.owner.equals(p.getUniqueId())) .filter(pixelBlock -> pixelBlock.ownerUID.equals(p.getUniqueId()))
.sorted(Comparator.comparing(pixelBlock -> pixelBlock.lastEntryTime)) .sorted(Comparator.comparing(pixelBlock -> pixelBlock.lastEntryTime))
.toList(); .toList();

View File

@ -0,0 +1,22 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockExplodeEvent;
import java.util.Arrays;
public class BlockExplodeListener implements Listener {
@EventHandler
static void onBlockExplode(BlockExplodeEvent event) {
Location eventLocation = event.getBlock().getLocation();
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)};
if(!Arrays.stream(standardWorlds).toList().contains(eventLocation.getWorld())) {
event.setCancelled(true);
}
}
}

View File

@ -7,20 +7,48 @@ import org.bukkit.World;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.Arrays; import java.util.Arrays;
import java.util.UUID;
public class BlockPlaceListener implements Listener { public class BlockPlaceListener implements Listener {
@EventHandler @EventHandler
static void onBlockPlace(BlockPlaceEvent event) { static void onBlockPlace(BlockPlaceEvent event) {
Location blockLocation = event.getBlock().getLocation(); if(event.getItemInHand().getItemMeta().getDisplayName().contains("Pixelblock")
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)}; && !event.getItemInHand().getItemMeta().getItemName().isEmpty()
&& event.getItemInHand().getItemMeta().getEnchantmentGlintOverride()) {
event.setCancelled(true);
if(!Arrays.stream(standardWorlds).toList().contains(blockLocation.getWorld())) { World playerWorld = event.getPlayer().getWorld();
PixelBlock pixelBlock = PixelBlock.getPixelBlockFromWorld(blockLocation.getWorld()); World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1)};
assert pixelBlock != null; if(Arrays.stream(standardWorlds).toList().contains(playerWorld)) {
pixelBlock.handleBlockPlace(event, true); Location newBlockLocation = event.getBlock().getLocation();
if(event.getItemInHand().getItemMeta().getItemName().equals("Pixelblock")) {
ItemMeta newMeta = event.getItemInHand().getItemMeta();
newMeta.setItemName(UUID.randomUUID().toString());
event.getItemInHand().setItemMeta(newMeta);
}
PixelBlock pixelBlock = new PixelBlock(newBlockLocation, event.getPlayer().getUniqueId(), UUID.fromString(event.getItemInHand().getItemMeta().getItemName()));
pixelBlock.place(newBlockLocation);
event.getPlayer().getInventory().remove(event.getItemInHand());
} else {
event.getPlayer().sendMessage("Du kannst nur in der Overworld oder im Nether Pixelblocks platzieren!");
}
} else {
Location blockLocation = event.getBlock().getLocation();
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)};
if(!Arrays.stream(standardWorlds).toList().contains(blockLocation.getWorld())) {
PixelBlock pixelBlock = PixelBlock.getPixelBlockFromWorld(blockLocation.getWorld());
assert pixelBlock != null;
pixelBlock.handleBlockPlace(event, true);
}
} }
} }
} }

View File

@ -0,0 +1,22 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;
import java.util.Arrays;
public class EntityExplodeListener implements Listener {
@EventHandler
static void onEntityExplode(EntityExplodeEvent event) {
Location eventLocation = event.getLocation();
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)};
if(!Arrays.stream(standardWorlds).toList().contains(eventLocation.getWorld())) {
event.setCancelled(true);
}
}
}

View File

@ -1,8 +1,8 @@
package eu.mhsl.minecraft.pixelblocks.listeners; package eu.mhsl.minecraft.pixelblocks.listeners;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -18,9 +18,21 @@ public class PlayerMoveListener implements Listener {
if(!Arrays.stream(standardWorlds).toList().contains(playerWorld)) { if(!Arrays.stream(standardWorlds).toList().contains(playerWorld)) {
Location playerLocation = event.getPlayer().getLocation(); Location playerLocation = event.getPlayer().getLocation();
PixelBlock pixelBlock = PixelBlock.getPixelBlockFromWorld(playerWorld);
assert pixelBlock != null;
Location portalLocation = pixelBlock.getPortalLocation().clone();
if(playerLocation.y() < -65) { if(playerLocation.y() < -65) {
playerWorld.getSpawnLocation().clone().subtract(1, 1, 1).getBlock().setType(Material.WHITE_CONCRETE); event.getPlayer().teleport(pixelBlock.getPlayerSpawnLocation(event.getPlayer()));
event.getPlayer().teleport(event.getPlayer().getWorld().getSpawnLocation()); } else if(playerLocation.getZ() < portalLocation.getZ()+1
&& playerLocation.getZ() > portalLocation.getZ()) {
if(playerLocation.getX() < portalLocation.getX()+3
&& playerLocation.getX() > portalLocation.getX()+1) {
if(playerLocation.getY() < portalLocation.getY()+4
&& playerLocation.getY() > portalLocation.getY()+1) {
event.getPlayer().teleport(pixelBlock.lastEntryLocation);
}
}
} }
} }
} }

View File

@ -0,0 +1,21 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerPortalEvent;
import java.util.Arrays;
public class PlayerPortalListener implements Listener {
@EventHandler
static void onPlayerPortal(PlayerPortalEvent event) {
World worldLeft = event.getFrom().getWorld();
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)};
if(!Arrays.stream(standardWorlds).toList().contains(worldLeft)) {
event.setCancelled(true);
}
}
}

View File

@ -4,11 +4,11 @@ import eu.mhsl.minecraft.pixelblocks.PixelBlocks;
import eu.mhsl.minecraft.pixelblocks.chunkGenerators.EmptyChunkGenerator; import eu.mhsl.minecraft.pixelblocks.chunkGenerators.EmptyChunkGenerator;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.entity.EntityType; import org.bukkit.entity.*;
import org.bukkit.entity.Interaction;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.FileUtils;
import java.io.File; import java.io.File;
@ -26,20 +26,21 @@ public class PixelBlock {
public Location pixelBlockLocation; public Location pixelBlockLocation;
public ArrayList<Pixel> pixels = new ArrayList<>(); public ArrayList<Pixel> pixels = new ArrayList<>();
public Interaction hitbox; public Interaction hitbox;
public ItemDisplay barrier;
public long lastEntryTime; public long lastEntryTime;
public Location lastEntryLocation; public Location lastEntryLocation;
public UUID owner; public UUID ownerUID;
public UUID uuid; public UUID uuid;
public PixelBlock(Location originLocation, UUID owner, UUID blockUUID) { public PixelBlock(Location originLocation, UUID ownerUID, UUID blockUUID) {
PixelBlock.placedBlocks.add(this); PixelBlock.placedBlocks.add(this);
this.uuid = blockUUID; this.uuid = blockUUID;
this.pixelBlockLocation = originLocation.toBlockLocation(); this.pixelBlockLocation = originLocation.toBlockLocation();
this.pixelBlockLocation.setYaw(0); this.pixelBlockLocation.setYaw(0);
this.pixelBlockLocation.setPitch(0); this.pixelBlockLocation.setPitch(0);
this.owner = owner; this.ownerUID = ownerUID;
createPixelWorld(); createPixelWorld();
} }
@ -82,6 +83,11 @@ public class PixelBlock {
World newWorld = Bukkit.createWorld(worldCreator); World newWorld = Bukkit.createWorld(worldCreator);
assert newWorld != null; assert newWorld != null;
newWorld.setGameRule(GameRule.RANDOM_TICK_SPEED, 0);
newWorld.setGameRule(GameRule.DO_FIRE_TICK, false);
newWorld.setGameRule(GameRule.DO_MOB_SPAWNING, false);
newWorld.setGameRule(GameRule.DO_WEATHER_CYCLE, false);
newWorld.setGameRule(GameRule.DO_VINES_SPREAD, false);
newWorld.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false); newWorld.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, false);
Location borderStartLocation = newWorld.getSpawnLocation().clone().subtract(1, 1, 1); Location borderStartLocation = newWorld.getSpawnLocation().clone().subtract(1, 1, 1);
@ -108,15 +114,37 @@ public class PixelBlock {
} }
} }
} }
borderStartLocation.getBlock().setType(Material.WHITE_CONCRETE);
for (int x = (2*worldGrassBorderWidth+pixelsPerBlock-1)/2; x < (2*worldGrassBorderWidth+pixelsPerBlock-1)/2+4; x++) {
for (int y = 0; y < 5; y++) {
grassStartLocation.clone().add(x, 1+y, 0).getBlock().setType(Material.OBSIDIAN);
}
}
for (int x = (2*worldGrassBorderWidth+pixelsPerBlock-1)/2+1; x < (2*worldGrassBorderWidth+pixelsPerBlock-1)/2+3; x++) {
for (int y = 1; y < 4; y++) {
grassStartLocation.clone().add(x, 1+y, 0).getBlock().setType(Material.NETHER_PORTAL);
}
}
}); });
} else { } else {
new WorldCreator(plugin.getDataFolder().getPath() + pathSeparator + this.uuid).createWorld(); new WorldCreator(plugin.getDataFolder().getPath() + pathSeparator + this.uuid).createWorld();
} }
} }
public ItemStack getAsItem() {
ItemStack itemStack = ItemStack.of(Material.GRAY_STAINED_GLASS);
ItemMeta meta = itemStack.getItemMeta();
meta.setDisplayName("Pixelblock von " + Objects.requireNonNull(Bukkit.getPlayer(this.ownerUID)).getName());
meta.setEnchantmentGlintOverride(true);
meta.setItemName(this.uuid.toString());
itemStack.setItemMeta(meta);
return itemStack;
}
public void handleInteraction(Player player) { public void handleInteraction(Player player) {
if(!player.getUniqueId().equals(owner)) { if(!player.getUniqueId().equals(ownerUID)) {
player.sendMessage("Dieser Pixelblock gehört nicht dir!"); player.sendMessage("Dieser Pixelblock gehört nicht dir!");
return; return;
} }
@ -129,29 +157,38 @@ public class PixelBlock {
} }
public void handleAttack(Player player) { public void handleAttack(Player player) {
if(!player.getUniqueId().equals(owner)) { // if(!player.getUniqueId().equals(ownerUID)) {
player.sendMessage("Dieser Pixelblock gehört nicht dir!"); // player.sendMessage("Dieser Pixelblock gehört nicht dir!");
return; // return;
} // }
this.delete(); Objects.requireNonNull(Bukkit.getWorld(plugin.getDataFolder().getPath() + pathSeparator + this.uuid)).getPlayers().forEach(
player1 -> {
player1.sendMessage("Der Pixelblock wurde abgebaut!");
player1.teleport(this.lastEntryLocation);
}
);
this.remove();
this.pixelBlockLocation.getWorld().playSound(this.pixelBlockLocation, Sound.BLOCK_COPPER_BULB_BREAK, 1.0F, 30);
this.pixelBlockLocation.getWorld().dropItem(this.pixelBlockLocation, this.getAsItem());
} }
public void handleBlockBreak(BlockBreakEvent event, boolean liveUpdate) { public void handleBlockBreak(BlockBreakEvent event, boolean liveUpdate) {
Location blockLocation = event.getBlock().getLocation(); Location blockLocation = event.getBlock().getLocation();
Location spawnLocation = blockLocation.getWorld().getSpawnLocation(); Location spawnLocation = blockLocation.getWorld().getSpawnLocation();
if(blockLocation.x() < spawnLocation.x() || blockLocation.z() < spawnLocation.z() || blockLocation.y() < spawnLocation.y()-1) { if(blockLocation.x() < spawnLocation.x() || blockLocation.z() < spawnLocation.z() || blockLocation.y() < spawnLocation.y()) {
event.setCancelled(true); event.setCancelled(true);
return; return;
} else if(blockLocation.x() > spawnLocation.x()+15 || blockLocation.z() > spawnLocation.z()+15 || blockLocation.y() > spawnLocation.y()+14) { } else if(blockLocation.x() > spawnLocation.x()+15 || blockLocation.z() > spawnLocation.z()+15 || blockLocation.y() > spawnLocation.y()+15) {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
Bukkit.getScheduler().runTask(plugin, () -> { Bukkit.getScheduler().runTask(plugin, () -> {
if(liveUpdate) { if(liveUpdate) {
Location relativeLocation = blockLocation.subtract(spawnLocation.x(), spawnLocation.y()-1, spawnLocation.z()); Location relativeLocation = blockLocation.subtract(spawnLocation.x(), spawnLocation.y(), spawnLocation.z());
relativeLocation.setWorld(this.pixelBlockLocation.getWorld()); relativeLocation.setWorld(this.pixelBlockLocation.getWorld());
List<Pixel> pixels = this.pixels.stream().filter(pixel -> pixel.relativeLocation.equals(relativeLocation)).toList(); List<Pixel> pixels = this.pixels.stream().filter(pixel -> pixel.relativeLocation.equals(relativeLocation)).toList();
if(!pixels.isEmpty()) { if(!pixels.isEmpty()) {
@ -167,17 +204,17 @@ public class PixelBlock {
Location blockLocation = event.getBlock().getLocation(); Location blockLocation = event.getBlock().getLocation();
Location spawnLocation = blockLocation.getWorld().getSpawnLocation(); Location spawnLocation = blockLocation.getWorld().getSpawnLocation();
if(blockLocation.x() < spawnLocation.x() || blockLocation.z() < spawnLocation.z() || blockLocation.y() < spawnLocation.y()-1) { if(blockLocation.x() < spawnLocation.x() || blockLocation.z() < spawnLocation.z() || blockLocation.y() < spawnLocation.y()) {
event.setCancelled(true); event.setCancelled(true);
return; return;
} else if(blockLocation.x() > spawnLocation.x()+15 || blockLocation.z() > spawnLocation.z()+15 || blockLocation.y() > spawnLocation.y()+14) { } else if(blockLocation.x() > spawnLocation.x()+15 || blockLocation.z() > spawnLocation.z()+15 || blockLocation.y() > spawnLocation.y()+15) {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
Bukkit.getScheduler().runTask(PixelBlocks.plugin, () -> { Bukkit.getScheduler().runTask(PixelBlocks.plugin, () -> {
if(liveUpdate) { if(liveUpdate) {
Location relativeLocation = blockLocation.subtract(spawnLocation.x(), spawnLocation.y()-1, spawnLocation.z()); Location relativeLocation = blockLocation.subtract(spawnLocation.x(), spawnLocation.y(), spawnLocation.z());
relativeLocation.setWorld(this.pixelBlockLocation.getWorld()); relativeLocation.setWorld(this.pixelBlockLocation.getWorld());
Pixel newPixel = new Pixel(relativeLocation, event.getBlock().getBlockData(), ((double) 1 /pixelsPerBlock), 0); Pixel newPixel = new Pixel(relativeLocation, event.getBlock().getBlockData(), ((double) 1 /pixelsPerBlock), 0);
@ -188,26 +225,67 @@ public class PixelBlock {
} }
public Location getPlayerSpawnLocation(Player player) { public Location getPlayerSpawnLocation(Player player) {
Location spawnLocation = getPixelWorld().getSpawnLocation().clone().subtract(0.5, 0, 0.5); // Location spawnLocation = getPixelWorld().getSpawnLocation().clone().add(8, 0, -3.5);
spawnLocation.setYaw(player.getLocation().getYaw()); Location spawnLocation = getPixelWorld().getSpawnLocation().clone().add((double) pixelsPerBlock/2, 0, 1.5-worldGrassBorderWidth);
spawnLocation.setPitch(player.getLocation().getPitch()); // spawnLocation.setYaw(player.getLocation().getYaw());
// spawnLocation.setPitch(player.getLocation().getPitch());
return spawnLocation; return spawnLocation;
} }
public Location getPortalLocation() {
return getPixelWorld().getSpawnLocation().clone().add((double) pixelsPerBlock/2 -2, 0, -worldGrassBorderWidth-1);
}
public World getPixelWorld() { public World getPixelWorld() {
return Bukkit.getWorld(plugin.getDataFolder().getPath() + pathSeparator + this.uuid); return Bukkit.getWorld(plugin.getDataFolder().getPath() + pathSeparator + this.uuid);
} }
public void update() { public void spawnInteraction(boolean fullBlock) {
Bukkit.getScheduler().runTask(plugin, () -> { if(fullBlock || pixels.isEmpty()) {
clearEntities();
hitbox = (Interaction) pixelBlockLocation.getWorld().spawnEntity( hitbox = (Interaction) pixelBlockLocation.getWorld().spawnEntity(
pixelBlockLocation.clone().add(0.5, -hitboxOffset, 0.5), pixelBlockLocation.clone().add(0.5, -hitboxOffset, 0.5),
EntityType.INTERACTION EntityType.INTERACTION
); );
hitbox.setInteractionHeight(1F + 2*hitboxOffset); hitbox.setInteractionHeight(1F + 2*hitboxOffset);
hitbox.setInteractionWidth(1F + 2*hitboxOffset); hitbox.setInteractionWidth(1F + 2*hitboxOffset);
} else {
double startingX = this.pixels.stream()
.min(Comparator.comparing(pixel -> pixel.relativeLocation.getX()))
.orElseThrow().relativeLocation.getX();
double startingY = this.pixels.stream()
.min(Comparator.comparing(pixel -> pixel.relativeLocation.getY()))
.orElseThrow().relativeLocation.getY();
double startingZ = this.pixels.stream()
.min(Comparator.comparing(pixel -> pixel.relativeLocation.getZ()))
.orElseThrow().relativeLocation.getZ();
double endingX = this.pixels.stream()
.max(Comparator.comparing(pixel -> pixel.relativeLocation.getX()))
.orElseThrow().relativeLocation.getX();
double endingY = this.pixels.stream()
.max(Comparator.comparing(pixel -> pixel.relativeLocation.getY()))
.orElseThrow().relativeLocation.getY();
double endingZ = this.pixels.stream()
.max(Comparator.comparing(pixel -> pixel.relativeLocation.getZ()))
.orElseThrow().relativeLocation.getZ();
hitbox = (Interaction) pixelBlockLocation.getWorld().spawnEntity(
pixelBlockLocation.clone().add(((startingX+endingX+1)/2)/16, (startingY/16)-hitboxOffset, ((startingZ+endingZ+1)/2)/16),
EntityType.INTERACTION
);
hitbox.setInteractionHeight((float) (endingY-startingY+1)/16 + 2*hitboxOffset);
if((endingX-startingX) > (endingZ-startingZ)) {
hitbox.setInteractionWidth((float) (endingX-startingX+1)/16 + 2*hitboxOffset);
} else {
hitbox.setInteractionWidth((float) (endingZ-startingZ+1)/16 + 2*hitboxOffset);
}
}
}
public void update() {
Bukkit.getScheduler().runTask(plugin, () -> {
clearEntities();
for (int x = 0; x < 16; x++) { for (int x = 0; x < 16; x++) {
for (int y = 0; y < 16; y++) { for (int y = 0; y < 16; y++) {
@ -217,7 +295,6 @@ public class PixelBlock {
.getWorld(plugin.getDataFolder().getPath() + pathSeparator + this.uuid)) .getWorld(plugin.getDataFolder().getPath() + pathSeparator + this.uuid))
.getSpawnLocation() .getSpawnLocation()
.clone() .clone()
.subtract(0, 1, 0)
.add(relativeLocation.x(), relativeLocation.y(), relativeLocation.z()); .add(relativeLocation.x(), relativeLocation.y(), relativeLocation.z());
BlockData block = blockLocation.getBlock().getBlockData(); BlockData block = blockLocation.getBlock().getBlockData();
@ -232,6 +309,25 @@ public class PixelBlock {
for(Pixel pixel : this.pixels) { for(Pixel pixel : this.pixels) {
pixel.spawn(this.pixelBlockLocation); pixel.spawn(this.pixelBlockLocation);
} }
if(this.pixels.size() < 5) {
Location relativeLocation = new Location(pixelBlockLocation.getWorld(), 0, 0, 0);
BlockData block = Material.GRAY_STAINED_GLASS.createBlockData();
Pixel newPixel = new Pixel(relativeLocation, block, 1, 0);
pixels.add(newPixel);
newPixel.spawn(this.pixelBlockLocation);
Location itemDisplayLocation = this.pixelBlockLocation.clone().add(0.5, 0.5, 0.5);
this.barrier = (ItemDisplay) this.pixelBlockLocation.getWorld().spawnEntity(
itemDisplayLocation,
EntityType.ITEM_DISPLAY
);
this.barrier.setItemStack(ItemStack.of(Material.BARRIER));
spawnInteraction(true);
} else {
spawnInteraction(false);
}
}); });
} }
@ -245,6 +341,11 @@ public class PixelBlock {
hitbox.remove(); hitbox.remove();
hitbox = null; hitbox = null;
} }
if(barrier != null) {
this.barrier.remove();
this.barrier = null;
}
} }
public boolean place(Location placeLocation) { public boolean place(Location placeLocation) {