Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Created OnlinePlayerArgument - Updated ImageCommand and ImageCommandBridge - Updated README.md > Related to #10
- Loading branch information
Showing
6 changed files
with
149 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,10 @@ | |
import org.bukkit.*; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.meta.ItemMeta; | ||
import org.bukkit.persistence.PersistentDataContainer; | ||
import org.bukkit.persistence.PersistentDataType; | ||
import org.bukkit.plugin.PluginDescriptionFile; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
@@ -21,10 +25,7 @@ | |
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
import java.util.*; | ||
|
||
public class ImageCommand { | ||
public static final int ITEMS_PER_PAGE = 9; | ||
|
@@ -42,6 +43,9 @@ public static void showHelp(@NotNull CommandSender s, @NotNull String commandNam | |
if (s.hasPermission("yamipa.download")) { | ||
s.sendMessage(ChatColor.AQUA + cmd + " download <url> <filename>" + ChatColor.RESET + " - Download image"); | ||
} | ||
if (s.hasPermission("yamipa.give")) { | ||
s.sendMessage(ChatColor.AQUA + cmd + " give <p> <filename> <#> <w> [<h>]" + ChatColor.RESET + " - Give image items"); | ||
} | ||
if (s.hasPermission("yamipa.list")) { | ||
s.sendMessage(ChatColor.AQUA + cmd + " list [<page>]" + ChatColor.RESET + " - List all images"); | ||
} | ||
|
@@ -130,12 +134,7 @@ public static void placeImage( | |
player.sendMessage(ChatColor.RED + "The requested file is not a valid image"); | ||
return; | ||
} | ||
if (height == 0) { | ||
float imageRatio = (float) sizeInPixels.height / sizeInPixels.width; | ||
height = Math.round(width * imageRatio); | ||
height = Math.min(height, FakeImage.MAX_DIMENSION); | ||
} | ||
final int finalHeight = height; | ||
final int finalHeight = (height == 0) ? FakeImage.getProportionalHeight(sizeInPixels, width) : height; | ||
|
||
// Ask player where to place image | ||
SelectBlockTask task = new SelectBlockTask(player); | ||
|
@@ -297,4 +296,44 @@ public static void showTopPlayers(@NotNull CommandSender sender) { | |
++printedLines; | ||
} | ||
} | ||
|
||
public static void giveImageItems( | ||
@NotNull CommandSender sender, | ||
@NotNull Player player, | ||
@NotNull ImageFile image, | ||
int amount, | ||
int width, | ||
int height | ||
) { | ||
YamipaPlugin plugin = YamipaPlugin.getInstance(); | ||
|
||
// Get image size in blocks | ||
Dimension sizeInPixels = image.getSize(); | ||
if (sizeInPixels == null) { | ||
sender.sendMessage(ChatColor.RED + "The requested file is not a valid image"); | ||
return; | ||
} | ||
if (height == 0) { | ||
height = FakeImage.getProportionalHeight(sizeInPixels, width); | ||
} | ||
|
||
// Create item stack | ||
ItemStack itemStack = new ItemStack(Material.PAPER, amount); | ||
ItemMeta itemMeta = Objects.requireNonNull(itemStack.getItemMeta()); | ||
PersistentDataContainer itemData = itemMeta.getPersistentDataContainer(); | ||
itemMeta.setDisplayName(image.getName() + ChatColor.AQUA + " (" + width + "x" + height + ")"); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
josemmo
Author
Owner
|
||
itemMeta.setLore(Collections.singletonList("Yamipa image")); | ||
itemData.set(new NamespacedKey(plugin, "filename"), PersistentDataType.STRING, image.getName()); | ||
itemData.set(new NamespacedKey(plugin, "width"), PersistentDataType.INTEGER, width); | ||
itemData.set(new NamespacedKey(plugin, "height"), PersistentDataType.INTEGER, height); | ||
itemStack.setItemMeta(itemMeta); | ||
|
||
// Add item stack to player's inventory | ||
player.getInventory().addItem(itemStack); | ||
sender.sendMessage( | ||
ChatColor.ITALIC + "Added " + amount + " " + | ||
(amount == 1 ? "image item" : "image items") + | ||
" to " + player.getName() + "'s inventory" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/io/josemmo/bukkit/plugin/commands/arguments/OnlinePlayerArgument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package io.josemmo.bukkit.plugin.commands.arguments; | ||
|
||
import com.mojang.brigadier.builder.RequiredArgumentBuilder; | ||
import com.mojang.brigadier.context.CommandContext; | ||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import com.mojang.brigadier.suggestion.Suggestions; | ||
import com.mojang.brigadier.suggestion.SuggestionsBuilder; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.entity.Player; | ||
import org.jetbrains.annotations.NotNull; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class OnlinePlayerArgument extends StringArgument { | ||
/** | ||
* Online player argument constructor | ||
* @param name Argument name | ||
*/ | ||
public OnlinePlayerArgument(@NotNull String name) { | ||
super(name); | ||
} | ||
|
||
@Override | ||
public @NotNull RequiredArgumentBuilder<?, ?> build() { | ||
return super.build().suggests(this::getSuggestions); | ||
} | ||
|
||
@Override | ||
public @NotNull Object parse(@NotNull CommandSender sender, @NotNull Object rawValue) throws CommandSyntaxException { | ||
Player player = getAllowedValues().get((String) rawValue); | ||
if (player == null) { | ||
throw newException("Expected online player (name or UUID)"); | ||
} | ||
return player; | ||
} | ||
|
||
private @NotNull CompletableFuture<Suggestions> getSuggestions( | ||
@NotNull CommandContext<?> ctx, | ||
@NotNull SuggestionsBuilder builder | ||
) { | ||
getAllowedValues().keySet().forEach(builder::suggest); | ||
return builder.buildFuture(); | ||
} | ||
|
||
private @NotNull Map<String, Player> getAllowedValues() { | ||
Map<String, Player> values = new HashMap<>(); | ||
for (Player player : Bukkit.getOnlinePlayers()) { | ||
values.put(player.getUniqueId().toString(), player); | ||
values.put(player.getName(), player); | ||
} | ||
return values; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry for putting my 5 cents again, but maybe it would be better to also add the ability to set a custom name for the item and fallback to this if it isn't set? Just thinking.