did stuff on create / delete command

This commit is contained in:
LunarAkai 2024-07-17 16:16:56 +02:00
commit bb2dfe8be5
7 changed files with 316 additions and 11 deletions

View file

@ -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");

View file

@ -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 "<name> [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<String> 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<String> result = new ArrayList<>();
result.add("");
if(!fixed_mode) {
result.add("fixed");
}
if(!noblocks) {
result.add("noblocks");
}
return result;
}
return List.of();
}
}

View file

@ -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<String> onTabComplete(CommandSender sender, Command command, String alias, ArgsParser args) {
return List.of();
}
}

View file

@ -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<URI> savedLinks;
private UUID[] textDisplays;
private Boolean isInFixedMode;
private List<UUID> 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<String> 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<UUID> getDisplays() {
return displays;
}
public BlockFoxDisplayMode getBlockFoxDisplayMode() {

View file

@ -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);
}

View file

@ -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"
# 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

View file

@ -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