From bb2dfe8be582d0df5dbb6a739732f1bd3b5aa0ee Mon Sep 17 00:00:00 2001 From: LunarAkai Date: Wed, 17 Jul 2024 16:16:56 +0200 Subject: [PATCH] did stuff on create / delete command --- .../de/lunarakai/blockfox/BlockFoxPlugin.java | 4 + .../blockfox/commands/CreateCommand.java | 139 +++++++++++++++++- .../blockfox/commands/DeleteCommand.java | 62 +++++++- .../blockfox/common/BlockFoxDisplay.java | 97 +++++++++++- .../blockfox/common/DisplayList.java | 4 +- src/main/resources/lang/de_DE.properties | 12 +- src/main/resources/lang/en_US.properties | 9 ++ 7 files changed, 316 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/lunarakai/blockfox/BlockFoxPlugin.java b/src/main/java/de/lunarakai/blockfox/BlockFoxPlugin.java index 09f539d..ac067de 100644 --- a/src/main/java/de/lunarakai/blockfox/BlockFoxPlugin.java +++ b/src/main/java/de/lunarakai/blockfox/BlockFoxPlugin.java @@ -1,6 +1,8 @@ package de.lunarakai.blockfox; import de.iani.cubesideutils.bukkit.commands.CommandRouter; +import de.lunarakai.blockfox.commands.CreateCommand; +import de.lunarakai.blockfox.commands.DeleteCommand; import de.lunarakai.blockfox.commands.InfoCommand; import de.lunarakai.blockfox.commands.TestRSSCommand; import de.lunarakai.blockfox.common.BlockFoxManager; @@ -38,6 +40,8 @@ public final class BlockFoxPlugin extends JavaPlugin { CommandRouter commandRouter = new CommandRouter(getCommand("blockfox")); // Common commandRouter.addCommandMapping(new InfoCommand(this), "info"); + commandRouter.addCommandMapping(new CreateCommand(this), "create"); + commandRouter.addCommandMapping(new DeleteCommand(this), "delete"); // RSS commandRouter.addCommandMapping(new TestRSSCommand(this), "testrss"); diff --git a/src/main/java/de/lunarakai/blockfox/commands/CreateCommand.java b/src/main/java/de/lunarakai/blockfox/commands/CreateCommand.java index ff68b97..83ae0c9 100644 --- a/src/main/java/de/lunarakai/blockfox/commands/CreateCommand.java +++ b/src/main/java/de/lunarakai/blockfox/commands/CreateCommand.java @@ -1,4 +1,141 @@ package de.lunarakai.blockfox.commands; -public class CreateCommand { +import de.iani.cubesideutils.bukkit.commands.SubCommand; +import de.iani.cubesideutils.bukkit.commands.exceptions.DisallowsCommandBlockException; +import de.iani.cubesideutils.bukkit.commands.exceptions.IllegalSyntaxException; +import de.iani.cubesideutils.bukkit.commands.exceptions.InternalCommandException; +import de.iani.cubesideutils.bukkit.commands.exceptions.NoPermissionException; +import de.iani.cubesideutils.bukkit.commands.exceptions.RequiresPlayerException; +import de.iani.cubesideutils.commands.ArgsParser; +import de.lunarakai.blockfox.BlockFoxPlugin; +import de.lunarakai.blockfox.common.BlockFoxDisplay; +import de.lunarakai.lunarutils.chat.MessageUtils; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.regex.Pattern; +import javax.annotation.Nullable; +import org.bukkit.Location; +import org.bukkit.block.BlockFace; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.util.RayTraceResult; + +public class CreateCommand extends SubCommand { + private final BlockFoxPlugin plugin; + private static final Pattern VALID_NAME_PATTERN = Pattern.compile("^[a-z0-9_]+$"); + + public CreateCommand(BlockFoxPlugin plugin) { + this.plugin = plugin; + } + + @Override + public String getUsage() { + return " [fixed] [noblocks]"; + } + + @Override + public boolean requiresPlayer() { + return true; + } + + @Override + public String getRequiredPermission() { + return BlockFoxPlugin.PERMISSION_ADMIN; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String s, String commandString, ArgsParser args) throws DisallowsCommandBlockException, RequiresPlayerException, NoPermissionException, IllegalSyntaxException, InternalCommandException { + Player player = (Player) sender; + if(args.remaining() < 1 || args.remaining() > 3) { + MessageUtils.sendSimpleWarningMessage(player, commandString + getUsage()); + return true; + } + + String name = args.getNext().toLowerCase().trim(); + if(!VALID_NAME_PATTERN.matcher(name).matches()) { + MessageUtils.sendSimpleWarningMessage(player, "display.name.invalid"); + return true; + } + + if(plugin.getDisplayList().getBlockFoxDisplay(name) != null) { + MessageUtils.sendSimpleWarningMessage(player, "display.name.exists"); + return true; + } + + boolean fixed_mode = false; + boolean noblocks = false; + while(args.hasNext()) { + String arg = args.next().toLowerCase().trim(); + if(arg.equals("fixed")) { + fixed_mode = true; + } else if(arg.equals("noblocks")) { + noblocks = true; + } else { + MessageUtils.sendSimpleWarningMessage(player, commandString + getUsage()); + } + } + + BlockFace orientation = null; + Location location = null; + + @Nullable + RayTraceResult target = player.rayTraceBlocks(6); + if(target == null || target.getHitBlock() == null) { + MessageUtils.sendSimpleWarningMessage(player, "display.create.lookAtCenter"); + return true; + } + BlockFace face = target.getHitBlockFace(); + if(face != BlockFace.NORTH && face != BlockFace.WEST && face != BlockFace.EAST && face != BlockFace.SOUTH) { + MessageUtils.sendSimpleWarningMessage(player, "display.create.lookAtSide"); + return true; + } + location = target.getHitBlock().getLocation(); + orientation = face; + + BlockFoxDisplay newDisplay = new BlockFoxDisplay(plugin, name, location, orientation, fixed_mode); + if(plugin.getDisplayList().collidesWithBlockFoxDisplay(newDisplay)) { + MessageUtils.sendSimpleWarningMessage(player, "display.create.collidesWithOther"); + return true; + } + newDisplay.generateDisplays(); + if(!noblocks) { + newDisplay.generateBackgroundBlocks(); + } + plugin.getDisplayList().addBlockFoxDisplay(newDisplay); + MessageUtils.sendSimpleSuccessMessage(player, "display.create.success"); + return true; + } + + @Override + public Collection onTabComplete(CommandSender sender, Command command, String alias, ArgsParser args) { + if(args.remaining() == 2 || args.remaining() == 3) { + args.getNext(); + + boolean fixed_mode = false; + boolean noblocks = false; + + while(args.remaining() > 1) { + String arg = args.getNext().toLowerCase().trim(); + if(arg.equals("fixed")) { + fixed_mode = true; + } else if(arg.equals("noblocks")) { + noblocks = true; + } else { + return List.of(); + } + } + ArrayList result = new ArrayList<>(); + result.add(""); + if(!fixed_mode) { + result.add("fixed"); + } + if(!noblocks) { + result.add("noblocks"); + } + return result; + } + return List.of(); + } } diff --git a/src/main/java/de/lunarakai/blockfox/commands/DeleteCommand.java b/src/main/java/de/lunarakai/blockfox/commands/DeleteCommand.java index c680ddd..e488787 100644 --- a/src/main/java/de/lunarakai/blockfox/commands/DeleteCommand.java +++ b/src/main/java/de/lunarakai/blockfox/commands/DeleteCommand.java @@ -1,4 +1,64 @@ package de.lunarakai.blockfox.commands; -public class DeleteCommand { +import de.iani.cubesideutils.bukkit.commands.SubCommand; +import de.iani.cubesideutils.bukkit.commands.exceptions.DisallowsCommandBlockException; +import de.iani.cubesideutils.bukkit.commands.exceptions.IllegalSyntaxException; +import de.iani.cubesideutils.bukkit.commands.exceptions.InternalCommandException; +import de.iani.cubesideutils.bukkit.commands.exceptions.NoPermissionException; +import de.iani.cubesideutils.bukkit.commands.exceptions.RequiresPlayerException; +import de.iani.cubesideutils.commands.ArgsParser; +import de.lunarakai.blockfox.BlockFoxPlugin; +import de.lunarakai.blockfox.common.BlockFoxDisplay; +import de.lunarakai.lunarutils.chat.MessageUtils; +import java.util.Collection; +import java.util.List; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class DeleteCommand extends SubCommand { + private final BlockFoxPlugin plugin; + + public DeleteCommand(BlockFoxPlugin plugin) { + this.plugin = plugin; + } + + @Override + public String getUsage() { + return "[name]"; + } + + @Override + public boolean requiresPlayer() { + return true; + } + + @Override + public String getRequiredPermission() { + return BlockFoxPlugin.PERMISSION_ADMIN; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String s, String commandString, ArgsParser args) throws DisallowsCommandBlockException, RequiresPlayerException, NoPermissionException, IllegalSyntaxException, InternalCommandException { + Player player = (Player) sender; + BlockFoxDisplay display = plugin.getDisplayList().getBFDisplayAtBlock(player.getLocation().getBlock().getRelative(BlockFace.DOWN)); + if(display == null) { + Block target = player.getTargetBlockExact(6); + if(target != null) { + display = plugin.getDisplayList().getBFDisplayAtBlock(target); + } + } + if(display != null) { + plugin.getDisplayList().removeBlockFoxDisplay(display); + MessageUtils.sendSimpleWarningMessage(player, "display.delete.success"); + } + return false; + } + + @Override + public Collection onTabComplete(CommandSender sender, Command command, String alias, ArgsParser args) { + return List.of(); + } } diff --git a/src/main/java/de/lunarakai/blockfox/common/BlockFoxDisplay.java b/src/main/java/de/lunarakai/blockfox/common/BlockFoxDisplay.java index 806b52d..22b4190 100644 --- a/src/main/java/de/lunarakai/blockfox/common/BlockFoxDisplay.java +++ b/src/main/java/de/lunarakai/blockfox/common/BlockFoxDisplay.java @@ -5,12 +5,20 @@ import de.lunarakai.blockfox.BlockFoxPlugin; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.UUID; import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; import org.bukkit.block.BlockFace; +import org.bukkit.block.data.BlockData; import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.entity.Display; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.entity.TextDisplay; public class BlockFoxDisplay { private final BlockFoxPlugin plugin; @@ -21,7 +29,8 @@ public class BlockFoxDisplay { private DisplayStatus displayStatus = DisplayStatus.INACTIVE; private BlockFoxDisplayMode blockFoxDisplayMode = BlockFoxDisplayMode.RSS_FEED; private ArrayList savedLinks; - private UUID[] textDisplays; + private Boolean isInFixedMode; + private List displays; private Player currentPlayer; // Width = 5 @@ -31,7 +40,7 @@ public class BlockFoxDisplay { this.plugin = plugin; this.name = Preconditions.checkNotNull(displaySection.getString("name")); this.location = Preconditions.checkNotNull(displaySection.getLocation("location")); - this.textDisplays = new UUID[99]; // TODO + this.displays = new ArrayList<>(); // TODO this.savedLinks = new ArrayList<>(); BlockFace orientation = BlockFace.NORTH; @@ -56,7 +65,7 @@ public class BlockFoxDisplay { for(int i = 0; i < list.size(); i++) { String textDisplay = list.get(i); if(textDisplay != null) { - textDisplays[i] = UUID.fromString(textDisplay); + displays.set(i, UUID.fromString(textDisplay)); } } @@ -74,13 +83,89 @@ public class BlockFoxDisplay { } } + public BlockFoxDisplay(BlockFoxPlugin plugin, String name, Location location, BlockFace orientation, boolean isInFixedMode) { + this.plugin = plugin; + this.name = Preconditions.checkNotNull(name, "name is null"); + this.location = Preconditions.checkNotNull(location, "location is null"); + this.displays = new ArrayList<>(); // TODO + this.isInFixedMode = isInFixedMode; + + Preconditions.checkArgument(Math.abs(orientation.getModX()) + Math.abs(orientation.getModZ()) == 1, "no cardinal direction"); + this.orientation = orientation; + int d0x = orientation.getModX(); + int d0z = orientation.getModZ(); + int d1x = -d0z; + int d1z = d0x; + this.centerLocation = location.clone().add(0.5, 0, 0.5); + } + + public void generateBackgroundBlocks() { + World world = location.getWorld(); + int d0x = orientation.getModX(); + int d0z = orientation.getModZ(); + int d1x = -d0z; + int d1z = d0x; + Location loc = location.clone(); + + BlockData block0 = Material.SMOOTH_QUARTZ.createBlockData(); + + for (int fx = -1 - 5 - 3; fx < 2; fx++) { + for (int fy = -1; fy < 3 - 1; fy++) { + loc.set(location.getX() + d1x * fx, location.getY() + fy, location.getZ() + d1z * fx); + world.setBlockData(loc, block0); + } + } + } + + /* + Mitte = Block(1,1) + */ + public void generateDisplays() { + // Todo: + // 2 Text Displays + // 5 Item Displays + weitere als Buttons für Fediverse Navigation + // s. https://imgur.com/9XJ4XWw + int sizeWidth = 5; + int sizeHeight = 3; + + World world = location.getWorld(); + for(UUID uuid : displays) { + if(uuid != null) { + Entity display = world.getEntity(uuid); + if(display instanceof Display) { + display.remove(); + } + } + } + Collections.fill(displays, null); + + float rotation0 = 0; + + rotation0 = getRotationYaw(); + float rotation = rotation0; + + int d0x = orientation.getModX(); + int d0z = orientation.getModZ(); + int d1x = -d0z; + int d1z = d0x; + + Location loc = location.clone(); + + TextDisplay textDisplayTitle = world.spawn(loc, TextDisplay.class, textDisplay -> { + + }); + + + + } + public void save(ConfigurationSection displaySection) { displaySection.set("name", name); displaySection.set("location", location); displaySection.set("orientation", orientation.name()); displaySection.set("mode", blockFoxDisplayMode.getModeName()); List textDisplays = new ArrayList<>(); - for(UUID uuid : this.textDisplays) { + for(UUID uuid : this.displays) { textDisplays.add(uuid == null ? null : uuid.toString()); } displaySection.set("textdisplays", textDisplays); @@ -122,8 +207,8 @@ public class BlockFoxDisplay { return displayStatus; } - public UUID[] getTextDisplays() { - return textDisplays; + public List getDisplays() { + return displays; } public BlockFoxDisplayMode getBlockFoxDisplayMode() { diff --git a/src/main/java/de/lunarakai/blockfox/common/DisplayList.java b/src/main/java/de/lunarakai/blockfox/common/DisplayList.java index eb34d5e..76982f3 100644 --- a/src/main/java/de/lunarakai/blockfox/common/DisplayList.java +++ b/src/main/java/de/lunarakai/blockfox/common/DisplayList.java @@ -79,7 +79,7 @@ public class DisplayList { // for(Location loc : blockFoxDisplay.getBlocks()) { // bfdisplayBlocks.put(loc.clone(), blockFoxDisplay); // } - for(UUID uuid : blockFoxDisplay.getTextDisplays()) { + for(UUID uuid : blockFoxDisplay.getDisplays()) { if(uuid != null) { bfdisplayItemDisplays.put(uuid, blockFoxDisplay); } @@ -134,7 +134,7 @@ public class DisplayList { plugin.getManager().leaveDisplay(blockFoxDisplay.getCurrentPlayer(), true); } - for(UUID uuid : blockFoxDisplay.getTextDisplays()) { + for(UUID uuid : blockFoxDisplay.getDisplays()) { if(uuid != null) { bfdisplayItemDisplays.remove(uuid); } diff --git a/src/main/resources/lang/de_DE.properties b/src/main/resources/lang/de_DE.properties index 0f55711..78fc5c0 100644 --- a/src/main/resources/lang/de_DE.properties +++ b/src/main/resources/lang/de_DE.properties @@ -1,6 +1,16 @@ +# Info Command blockfox.info.version=Version blockfox.info.developer=Entwickelt von blockfox.info.website=Website blockfox.info.license=Lizenz -blockfox.display.usagecanceled="Die Benutzung wurde abgebrochen" \ No newline at end of file +# Creation / Deletion +display.name.invalid=Ungültiger Displayname. Erlaubt sind Buchstaben, Zahlen und der Unterstrich +display.name.exists=Ein Display mit diesem Namen existiert bereits +display.create.lookAtCenter=Bitte gucke den Block an, der im Zentrum des Displays sein soll. +display.create.lookAtSide=Bitte gucke die Seite des Blockes an, wo das Display erstellt werden soll. +display.create.collidesWithOther=An dieser Stelle befindet sich bereits ein Display. +display.create.success=Das Display wurde erfolgreich angelegt. +display.delete.success=Das Display wurde erfolgreich gelöscht. + +blockfox.display.usagecanceled=Die Benutzung wurde abgebrochen diff --git a/src/main/resources/lang/en_US.properties b/src/main/resources/lang/en_US.properties index 765a328..203a097 100644 --- a/src/main/resources/lang/en_US.properties +++ b/src/main/resources/lang/en_US.properties @@ -4,4 +4,13 @@ blockfox.info.developer=Developed by blockfox.info.website=Website blockfox.info.license=License +# Creation / Deletion +display.name.invalid=Invalid display name. Allowed are letters, numbers and the underscore +display.name.exists=A display with this name already exists +display.create.lookAtCenter=Please look at the block that should be in the center of the display. +display.create.lookAtSide=Please look at the side of the block where the display is to be created. +display.create.collidesWithOther=There is already a display at this location. +display.create.success=The display has been created successfully. +display.delete.success=The display has been successfully deleted. + blockfox.display.usagecanceled=The usage was canceled \ No newline at end of file