Added Item functionalities and portal

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

View File

@@ -4,11 +4,11 @@ import eu.mhsl.minecraft.pixelblocks.PixelBlocks;
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 org.bukkit.entity.*;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
@@ -26,20 +26,21 @@ public class PixelBlock {
public Location pixelBlockLocation;
public ArrayList<Pixel> pixels = new ArrayList<>();
public Interaction hitbox;
public ItemDisplay barrier;
public long lastEntryTime;
public Location lastEntryLocation;
public UUID owner;
public UUID ownerUID;
public UUID uuid;
public PixelBlock(Location originLocation, UUID owner, UUID blockUUID) {
public PixelBlock(Location originLocation, UUID ownerUID, UUID blockUUID) {
PixelBlock.placedBlocks.add(this);
this.uuid = blockUUID;
this.pixelBlockLocation = originLocation.toBlockLocation();
this.pixelBlockLocation.setYaw(0);
this.pixelBlockLocation.setPitch(0);
this.owner = owner;
this.ownerUID = ownerUID;
createPixelWorld();
}
@@ -82,6 +83,11 @@ public class PixelBlock {
World newWorld = Bukkit.createWorld(worldCreator);
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);
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 {
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) {
if(!player.getUniqueId().equals(owner)) {
if(!player.getUniqueId().equals(ownerUID)) {
player.sendMessage("Dieser Pixelblock gehört nicht dir!");
return;
}
@@ -129,29 +157,38 @@ public class PixelBlock {
}
public void handleAttack(Player player) {
if(!player.getUniqueId().equals(owner)) {
player.sendMessage("Dieser Pixelblock gehört nicht dir!");
return;
}
// if(!player.getUniqueId().equals(ownerUID)) {
// player.sendMessage("Dieser Pixelblock gehört nicht dir!");
// 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) {
Location blockLocation = event.getBlock().getLocation();
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);
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);
return;
}
Bukkit.getScheduler().runTask(plugin, () -> {
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());
List<Pixel> pixels = this.pixels.stream().filter(pixel -> pixel.relativeLocation.equals(relativeLocation)).toList();
if(!pixels.isEmpty()) {
@@ -167,17 +204,17 @@ public class PixelBlock {
Location blockLocation = event.getBlock().getLocation();
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);
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);
return;
}
Bukkit.getScheduler().runTask(PixelBlocks.plugin, () -> {
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());
Pixel newPixel = new Pixel(relativeLocation, event.getBlock().getBlockData(), ((double) 1 /pixelsPerBlock), 0);
@@ -188,26 +225,67 @@ public class PixelBlock {
}
public Location getPlayerSpawnLocation(Player player) {
Location spawnLocation = getPixelWorld().getSpawnLocation().clone().subtract(0.5, 0, 0.5);
spawnLocation.setYaw(player.getLocation().getYaw());
spawnLocation.setPitch(player.getLocation().getPitch());
// Location spawnLocation = getPixelWorld().getSpawnLocation().clone().add(8, 0, -3.5);
Location spawnLocation = getPixelWorld().getSpawnLocation().clone().add((double) pixelsPerBlock/2, 0, 1.5-worldGrassBorderWidth);
// spawnLocation.setYaw(player.getLocation().getYaw());
// spawnLocation.setPitch(player.getLocation().getPitch());
return spawnLocation;
}
public Location getPortalLocation() {
return getPixelWorld().getSpawnLocation().clone().add((double) pixelsPerBlock/2 -2, 0, -worldGrassBorderWidth-1);
}
public World getPixelWorld() {
return Bukkit.getWorld(plugin.getDataFolder().getPath() + pathSeparator + this.uuid);
}
public void update() {
Bukkit.getScheduler().runTask(plugin, () -> {
clearEntities();
public void spawnInteraction(boolean fullBlock) {
if(fullBlock || pixels.isEmpty()) {
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);
} 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 y = 0; y < 16; y++) {
@@ -217,7 +295,6 @@ public class PixelBlock {
.getWorld(plugin.getDataFolder().getPath() + pathSeparator + this.uuid))
.getSpawnLocation()
.clone()
.subtract(0, 1, 0)
.add(relativeLocation.x(), relativeLocation.y(), relativeLocation.z());
BlockData block = blockLocation.getBlock().getBlockData();
@@ -232,6 +309,25 @@ public class PixelBlock {
for(Pixel pixel : this.pixels) {
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 = null;
}
if(barrier != null) {
this.barrier.remove();
this.barrier = null;
}
}
public boolean place(Location placeLocation) {