Skip to content

Commit

Permalink
Merge pull request #120 from HibiscusMC/development
Browse files Browse the repository at this point in the history
HMCWraps 1.4.4
  • Loading branch information
Skyslycer authored Apr 7, 2024
2 parents ad86295 + b1f053b commit e7d4edd
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public enum Messages {
COMMAND_CONVERT_FAILED("command.convert.failed"),
COMMAND_CREATE_FAILED("command.create.failed"),
COMMAND_CREATE_SUCCESS("command.create.success"),
COMMAND_OPEN("command.open"),
PLACEHOLDER_EQUIPPED("placeholder.equipped"),
PLACEHOLDER_NOT_EQUIPPED("placeholder.not-equipped");

Expand Down
6 changes: 5 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "de.skyslycer"
version = "1.4.3"
version = "1.4.4"

val shadePattern = "$group.hmcwraps.shade"

Expand Down Expand Up @@ -175,5 +175,9 @@ bukkit {
register("hmcwraps.shortcut.disable") {
description = "If this permission is applied to a player, the shortcut function is disabled only for that player."
}
register("hmcwraps.commands.open") {
description = "Gives access to open the wrap inventory for another player."
children = listOf("hmcwraps.management", "hmcwraps.admin")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.skyslycer.hmcwraps.HMCWraps;
import de.skyslycer.hmcwraps.HMCWrapsPlugin;
import de.skyslycer.hmcwraps.commands.annotations.AnyPermissionReader;
import de.skyslycer.hmcwraps.commands.annotations.NoHelp;
import de.skyslycer.hmcwraps.messages.Messages;
import de.skyslycer.hmcwraps.serialization.wrap.Wrap;
Expand Down Expand Up @@ -36,6 +37,7 @@ public static void registerCommands(HMCWrapsPlugin plugin) {
}
return wrap;
});
commandHandler.registerPermissionReader(new AnyPermissionReader());
commandHandler.getAutoCompleter().registerSuggestionFactory(0,
SuggestionProviderFactory.forType(Player.class, SuggestionProvider.map(Bukkit::getOnlinePlayers, Player::getName)));
commandHandler.getAutoCompleter().registerParameterSuggestions(Integer.class, SuggestionProvider.of(IntStream.range(1, 65).boxed().map(
Expand Down
23 changes: 18 additions & 5 deletions src/main/java/de/skyslycer/hmcwraps/commands/WrapCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.skyslycer.hmcwraps.HMCWraps;
import de.skyslycer.hmcwraps.HMCWrapsPlugin;
import de.skyslycer.hmcwraps.commands.annotations.AnyPermission;
import de.skyslycer.hmcwraps.gui.GuiBuilder;
import de.skyslycer.hmcwraps.messages.Messages;
import de.skyslycer.hmcwraps.serialization.files.WrapFile;
Expand Down Expand Up @@ -56,6 +57,7 @@ public class WrapCommand {
public static final String LIST_PERMISSION = "hmcwraps.commands.list";
public static final String CREATE_PERMISSION = "hmcwraps.commands.create";
public static final String WRAPS_PERMISSION = "hmcwraps.wraps";
public static final String WRAPS_OPEN_PERMISSION = "hmcwraps.commands.open";

private final Set<String> confirmingPlayers = new HashSet<>();

Expand All @@ -72,6 +74,10 @@ public void onWraps(Player player) {
plugin.getMessageHandler().send(player, Messages.NO_PERMISSION);
return;
}
openWrapsInventory(player);
}

private void openWrapsInventory(Player player) {
var item = player.getInventory().getItemInMainHand();
var slot = player.getInventory().getHeldItemSlot();
if (item.getType().isAir()) {
Expand All @@ -97,6 +103,14 @@ public void onWraps(Player player) {
GuiBuilder.open(plugin, player, player.getInventory().getItem(slot), slot);
}

@Subcommand("open")
@CommandPermission(WRAPS_OPEN_PERMISSION)
@Description("Open the wraps inventory for another player.")
public void onOpen(CommandSender sender, Player player) {
openWrapsInventory(player);
plugin.getMessageHandler().send(sender, Messages.COMMAND_OPEN, Placeholder.parsed("player", player.getName()));
}

@Subcommand("reload")
@CommandPermission(RELOAD_PERMISSION)
@Description("Reload configuration and messages.")
Expand Down Expand Up @@ -146,9 +160,11 @@ public void onConvert(CommandSender sender, @Optional String confirm) {
}

@Subcommand("wrap")
@Description("Wrap the item the player is holding in their main hand.")
@AnyPermission({WRAP_PERMISSION, WRAP_SELF_PERMISSION})
@AutoComplete("@wraps @players @actions")
public void onWrap(CommandSender sender, Wrap wrap, @Default("self") Player player, @Optional String actions) {
if (!(player.hasPermission(WRAP_PERMISSION) || (player == sender && player.hasPermission(WRAP_SELF_PERMISSION)))) {
if (player != sender && !sender.hasPermission(WRAP_PERMISSION)) {
plugin.getMessageHandler().send(sender, Messages.NO_PERMISSION);
return;
}
Expand Down Expand Up @@ -178,12 +194,9 @@ public void onWrap(CommandSender sender, Wrap wrap, @Default("self") Player play

@Subcommand("unwrap")
@Description("Unwrap the item a player is holding in their main hand.")
@AnyPermission({UNWRAP_PERMISSION, UNWRAP_SELF_PERMISSION})
@AutoComplete("@players @actions")
public void onUnwrap(CommandSender sender, @Default("self") Player player, @Optional String actions) {
if (!(player.hasPermission(UNWRAP_PERMISSION) || (player == sender && player.hasPermission(UNWRAP_SELF_PERMISSION)))) {
plugin.getMessageHandler().send(sender, Messages.NO_PERMISSION);
return;
}
var item = player.getInventory().getItemInMainHand().clone();
if (item.getType().isAir()) {
plugin.getMessageHandler().send(sender, Messages.COMMAND_NEED_ITEM);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.skyslycer.hmcwraps.commands.annotations;

import revxrsal.commands.annotation.DistributeOnMethods;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@DistributeOnMethods
@Target({ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface AnyPermission {
String[] value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.skyslycer.hmcwraps.commands.annotations;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import revxrsal.commands.bukkit.BukkitCommandActor;
import revxrsal.commands.command.trait.CommandAnnotationHolder;
import revxrsal.commands.process.PermissionReader;

public class AnyPermissionReader implements PermissionReader {

@Override public @Nullable revxrsal.commands.command.CommandPermission getPermission(@NotNull CommandAnnotationHolder command) {
var annotation = command.getAnnotation(AnyPermission.class);
if (annotation == null) return null;
var permissions = annotation.value();
return actor -> {
var sender = ((BukkitCommandActor) actor).getSender();
for (var permission : permissions) {
if (sender.hasPermission(permission)) return true;
}
return false;
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPickupItemEvent;

Expand All @@ -16,7 +17,7 @@ public PlayerPickupListener(HMCWrapsPlugin plugin) {
this.plugin = plugin;
}

@EventHandler
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onItemPickup(EntityPickupItemEvent event) {
if (!(event.getEntity() instanceof Player player)) {
return;
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/de/skyslycer/hmcwraps/wrap/WrapperImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,16 @@ private ItemStack setWrapPrivate(@Nullable Wrap wrap, ItemStack item, boolean ph
}
resetFakeDurability(item, editing);
if (currentWrap != null) {
meta.setDisplayName(originalData.name());
meta.setLore(originalData.lore());
meta.removeItemFlags(meta.getItemFlags().toArray(ItemFlag[]::new));
meta.addItemFlags(originalData.flags().toArray(ItemFlag[]::new));
if (currentWrap.getWrapName() != null) {
meta.setDisplayName(originalData.name());
}
if (currentWrap.getWrapLore() != null) {
meta.setLore(originalData.lore());
}
if (currentWrap.getWrapFlags() != null) {
meta.removeItemFlags(meta.getItemFlags().toArray(ItemFlag[]::new));
meta.addItemFlags(originalData.flags().toArray(ItemFlag[]::new));
}
}
if (wrap.getWrapName() != null) {
meta.setDisplayName(StringUtil.LEGACY_SERIALIZER.serialize(StringUtil.parseComponent(player, wrap.getWrapName())));
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/de/skyslycer/hmcwraps/wrap/WrapsLoaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

public class WrapsLoaderImpl implements WrapsLoader {
Expand Down Expand Up @@ -65,8 +67,16 @@ private void combineFiles() {
wrappableItems.put(type, wrappableItem);
}
}));
wrappableItems.values().forEach(wrappableItem -> wrappableItem.getWraps().values().forEach(wrap -> wraps.put(wrap.getUuid(), wrap)));

for (var wrappableItem : wrappableItems.entrySet()) {
for (var wrap : wrappableItem.getValue().getWraps().values()) {
if (wrap.getUuid() == null) {
plugin.getLogger().warning("A wrap with the material/collection '" + wrappableItem.getKey() + "' doesn't have a " +
"UUID assigned! Make sure every wrap has a UUID assigned and check for typos like 'uid' instead of 'uuid'.");
continue;
}
wraps.put(wrap.getUuid(), wrap);
}
}
wraps.remove("-");
}

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ command.convert.no-confirm=<red>You currently don't have anything to confirm! <g
command.convert.failed=<red>Some or all files failed to convert! <gray>Please <red>check <gray>the console for more information, <red>delete <gray>the folders <red>wraps/generated/ <gray>and <red>collections/generated/, and <red>try again!
command.create.failed=<red>An error occurred while generating the wrap file! Please report the error in the console to the developers.
command.create.success=<gray>Successfully <green>created <gray>the <green>wrap <gray>file! You can find it at: <green><path>! <gray>Type <green>/wraps reload <gray>once you want the new wrap to be loaded.
command.open=<gray>Successfully <green>opened <gray>the wraps inventory for <green><player>!
placeholder.equipped=<green>Equipped
placeholder.not-equipped=<red>Not equipped

0 comments on commit e7d4edd

Please sign in to comment.