This commit is contained in:
Lars Neuhaus 2024-07-06 21:53:11 +02:00
parent 47c6a01ce5
commit ccdff1ed64
7 changed files with 186 additions and 7 deletions

View File

@ -2,9 +2,12 @@ package eu.mhsl.minecraft.pixelblocks;
import eu.mhsl.minecraft.pixelblocks.commands.ChessCommand; import eu.mhsl.minecraft.pixelblocks.commands.ChessCommand;
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.listeners.PlayerInteractListener;
import eu.mhsl.minecraft.pixelblocks.listeners.PlayerJumpListener; import eu.mhsl.minecraft.pixelblocks.listeners.PlayerJumpListener;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.util.Objects; import java.util.Objects;
public final class PixelBlocks extends JavaPlugin { public final class PixelBlocks extends JavaPlugin {
@ -14,9 +17,14 @@ public final class PixelBlocks extends JavaPlugin {
public void onEnable() { public void onEnable() {
PixelBlocks.plugin = this; PixelBlocks.plugin = this;
this.getLogger().info("Hello World"); this.getLogger().info("Hello World");
new File(plugin.getDataFolder().getPath() + "/Maps").mkdir();
getServer().getPluginManager().registerEvents(new PlayerJumpListener(), this); getServer().getPluginManager().registerEvents(new PlayerJumpListener(), this);
getServer().getPluginManager().registerEvents(new PlayerInteractListener(), this);
Objects.requireNonNull(getCommand("chess")).setExecutor(new ChessCommand()); Objects.requireNonNull(getCommand("chess")).setExecutor(new ChessCommand());
Objects.requireNonNull(getCommand("createpixelblock")).setExecutor(new CreatePixelBlockCommand()); Objects.requireNonNull(getCommand("createpixelblock")).setExecutor(new CreatePixelBlockCommand());
Objects.requireNonNull(getCommand("exitworld")).setExecutor(new ExitWorldCommand());
} }
@Override @Override

View File

@ -1,6 +1,7 @@
package eu.mhsl.minecraft.pixelblocks.commands; package eu.mhsl.minecraft.pixelblocks.commands;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock; import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -13,10 +14,10 @@ public class CreatePixelBlockCommand implements CommandExecutor {
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if(sender instanceof Player p) { if(sender instanceof Player p) {
Location playerLocation = p.getLocation(); Location playerLocation = p.getLocation();
PixelBlock pixelBlock = new PixelBlock(playerLocation, 16); PixelBlock pixelBlock = new PixelBlock(playerLocation, p.getUniqueId(), 16);
pixelBlock.place(playerLocation); pixelBlock.place(playerLocation);
} }
return true; return(true);
} }
} }

View File

@ -0,0 +1,34 @@
package eu.mhsl.minecraft.pixelblocks.commands;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.*;
public class ExitWorldCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if(sender instanceof Player p) {
World playerWorld = p.getWorld();
World[] standardWorlds = {Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(1), Bukkit.getWorlds().get(2)};
if(Arrays.stream(standardWorlds).toList().contains(playerWorld)) {
Location newLocation = PixelBlock.placedBlocks.stream()
.filter(pixelBlock -> pixelBlock.owner == p.getUniqueId())
.findFirst()
.orElseThrow()
.lastEntryLocation;
// p.teleport(Bukkit.getServer().getWorlds().getFirst().getSpawnLocation());
}
}
return(true);
}
}

View File

@ -0,0 +1,84 @@
package eu.mhsl.minecraft.pixelblocks.listeners;
import eu.mhsl.minecraft.pixelblocks.PixelBlocks;
import eu.mhsl.minecraft.pixelblocks.pixelblock.Pixel;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import io.papermc.paper.event.player.PrePlayerAttackEntityEvent;
import io.papermc.paper.math.Position;
import org.bukkit.*;
import org.bukkit.entity.Blaze;
import org.bukkit.entity.BlockDisplay;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Interaction;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import static eu.mhsl.minecraft.pixelblocks.PixelBlocks.plugin;
public class PlayerInteractListener implements Listener {
@EventHandler
static void onPlayerInteract(PlayerInteractEvent event) {
if(event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock() != null) {
if(event.getClickedBlock().getType() == Material.GLASS) {
// final WorldCreator worldCreator = new WorldCreator(plugin.getDataFolder().getPath()+"/"+event.getPlayer().getUniqueId());
// worldCreator.type(WorldType.FLAT);
//
// World newWorld = Bukkit.createWorld(worldCreator);
//
// event.getPlayer().teleport(newWorld.getSpawnLocation());
}
}
}
@EventHandler
static void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
if(event.getRightClicked() instanceof Interaction) {
Location interactionLocation = event.getRightClicked().getLocation().add(0, PixelBlock.hitboxOffset+0.5, 0).toBlockLocation();
interactionLocation.setYaw(0);
interactionLocation.setPitch(0);
UUID playerUID = event.getPlayer().getUniqueId();
PixelBlock pixelBlock = PixelBlock.placedBlocks.stream()
.filter(block -> block.pixelBlockLocation.equals(interactionLocation))
.findFirst()
.orElseThrow();
UUID blockOwner = pixelBlock.owner;
if(playerUID == blockOwner) {
pixelBlock.lastEntryLocation = event.getPlayer().getLocation();
final WorldCreator worldCreator = new WorldCreator(plugin.getDataFolder().getPath()+"/"+event.getPlayer().getUniqueId());
worldCreator.type(WorldType.FLAT);
World newWorld = Bukkit.createWorld(worldCreator);
event.getPlayer().teleport(newWorld.getSpawnLocation());
} else {
event.getPlayer().sendMessage("This Block is not yours!");
}
}
}
@EventHandler
static void onPlayerAttackEntity(PrePlayerAttackEntityEvent event) {
if(event.getAttacked() instanceof Interaction) {
Location blockLocation = event.getAttacked().getLocation().add(0, PixelBlock.hitboxOffset+0.5, 0).toBlockLocation();
blockLocation.setYaw(0);
blockLocation.setPitch(0);
Bukkit.getScheduler().runTask(
PixelBlocks.plugin,
() -> PixelBlock.placedBlocks.stream()
// .filter(pixelBlock -> pixelBlock.pixelBlockLocation.distance(blockLocation) < 0.5)
.filter(pixelBlock -> pixelBlock.pixelBlockLocation.equals(blockLocation))
.findFirst()
.orElseThrow()
.remove());
}
}
}

View File

@ -3,15 +3,21 @@ package eu.mhsl.minecraft.pixelblocks.pixelblock;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.entity.BlockDisplay; import org.bukkit.entity.BlockDisplay;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.util.Transformation; import org.bukkit.util.Transformation;
import java.util.Objects;
import java.util.UUID;
public class Pixel { public class Pixel {
Location relativeLocation; Location relativeLocation;
BlockData blockData; BlockData blockData;
double scale; double scale;
double offset; double offset;
public UUID uuid;
public Pixel(Location relativeLocation, BlockData blockData, double scale, double offset) { public Pixel(Location relativeLocation, BlockData blockData, double scale, double offset) {
this.relativeLocation = new Location( this.relativeLocation = new Location(
relativeLocation.getWorld(), relativeLocation.getWorld(),
@ -41,5 +47,12 @@ public class Pixel {
Transformation transform = bd.getTransformation(); Transformation transform = bd.getTransformation();
transform.getScale().set(scale); transform.getScale().set(scale);
bd.setTransformation(transform); bd.setTransformation(transform);
this.uuid = bd.getUniqueId();
}
public void remove() {
Entity pixelEntity = this.relativeLocation.getWorld().getEntity(this.uuid);
if(pixelEntity != null) pixelEntity.remove();
} }
} }

View File

@ -5,31 +5,62 @@ import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Interaction;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class PixelBlock { public class PixelBlock {
double pixelsPerBlock; public static List<PixelBlock> placedBlocks = new ArrayList<>();
ArrayList<Pixel> pixels = new ArrayList<>();
public PixelBlock(Location originLocation, double pixelsPerBlock) { public Location pixelBlockLocation;
double pixelsPerBlock;
public ArrayList<Pixel> pixels = new ArrayList<>();
public Interaction hitbox;
public static float hitboxOffset = 0.005F;
public Location lastEntryLocation;
public UUID owner;
public PixelBlock(Location originLocation, UUID owner, double pixelsPerBlock) {
PixelBlock.placedBlocks.add(this);
this.pixelBlockLocation = originLocation.toBlockLocation();
this.pixelBlockLocation.setYaw(0);
this.pixelBlockLocation.setPitch(0);
this.pixelsPerBlock = pixelsPerBlock; this.pixelsPerBlock = pixelsPerBlock;
this.owner = owner;
for (int x = 0; x < pixelsPerBlock; x++) { for (int x = 0; x < pixelsPerBlock; x++) {
for (int y = 0; y < pixelsPerBlock; y++) { for (int y = 0; y < pixelsPerBlock; y++) {
for (int z = 0; z < pixelsPerBlock; z++) { for (int z = 0; z < pixelsPerBlock; z++) {
Location relativeLocation = new Location(originLocation.getWorld(), x, y, z); Location relativeLocation = new Location(originLocation.getWorld(), x, y, z);
Location blockLocation = originLocation.toBlockLocation().clone().add(relativeLocation); Location blockLocation = pixelBlockLocation.clone().add(relativeLocation);
BlockData block = blockLocation.getBlock().getBlockData(); BlockData block = blockLocation.getBlock().getBlockData();
if(block.getMaterial() != Material.AIR) { if(block.getMaterial() != Material.AIR) {
Pixel newPixel = new Pixel(relativeLocation, block, (1/pixelsPerBlock)-0.0001, 0.00005); Pixel newPixel = new Pixel(relativeLocation, block, (1/pixelsPerBlock)-0.0001, 0.00005*pixelsPerBlock);
pixels.add(newPixel); 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);
} }
public void place(Location placeLocation) { public void place(Location placeLocation) {
@ -39,4 +70,11 @@ public class PixelBlock {
} }
}); });
} }
public void remove() {
this.pixels.forEach(Pixel::remove);
hitbox.remove();
pixelBlockLocation.getBlock().setType(Material.AIR);
placedBlocks.remove(this);
}
} }

View File

@ -5,3 +5,4 @@ api-version: '1.21'
commands: commands:
chess: chess:
createpixelblock: createpixelblock:
exitworld: