Skip to content

Commit

Permalink
Improves TranslatableItem
Browse files Browse the repository at this point in the history
Adds "/bloblib translatableitem" command
  • Loading branch information
anjoismysign committed Mar 7, 2024
1 parent c1a02bb commit 3a51162
Show file tree
Hide file tree
Showing 13 changed files with 300 additions and 18 deletions.
2 changes: 1 addition & 1 deletion ci-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.698.02</version>
<version>1.698.08</version>
<relativePath>pom.xml</relativePath>
</parent>
<artifactId>bloblib</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion local-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.698.02</version>
<version>1.698.08</version>
<relativePath>pom.xml</relativePath>
</parent>
<artifactId>bloblib</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.698.02</version>
<version>1.698.08</version>
<packaging>pom</packaging>

<properties>
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/us/mytheria/bloblib/api/BlobLibTranslatableAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import us.mytheria.bloblib.managers.BlobLibConfigManager;
import us.mytheria.bloblib.managers.TranslatableManager;

import java.util.List;
import java.util.Objects;


Expand Down Expand Up @@ -156,6 +157,17 @@ public TranslatableItem getTranslatableItem(@NotNull String key,
return plugin.getTranslatableItemManager().getAsset(key, player.getLocale());
}

/**
* Get all the translatable items by their key.
*
* @param key The key of the translatable
* @return The list of translatable items
*/
@NotNull
public List<TranslatableItem> getTranslatableItems(@NotNull String key) {
return plugin.getTranslatableItemManager().getAssets(key);
}

/**
* Will get the real locale from the locale.
*
Expand Down
118 changes: 111 additions & 7 deletions src/main/java/us/mytheria/bloblib/command/BlobLibCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginBase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import us.mytheria.bloblib.BlobLib;
import us.mytheria.bloblib.api.BlobLibMessageAPI;
import us.mytheria.bloblib.api.BlobLibTranslatableAPI;
import us.mytheria.bloblib.entities.PluginUpdater;
import us.mytheria.bloblib.entities.message.BlobMessage;
import us.mytheria.bloblib.entities.translatable.TranslatableItem;
import us.mytheria.bloblib.managers.BlobPlugin;
import us.mytheria.bloblib.utilities.PlayerUtil;

import java.io.BufferedReader;
import java.io.IOException;
Expand Down Expand Up @@ -81,6 +86,74 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
.toCommandSender(sender);
return true;
}
case "translatableitem" -> {
if (length < 3) {
BlobLibMessageAPI.getInstance()
.getMessage("TranslatableItem.Usage", sender)
.toCommandSender(sender);
return true;
}
String arg2 = args[1].toLowerCase();
String key = args[2];
switch (arg2) {
case "get" -> {
if (!(sender instanceof Player player)) {
BlobLibMessageAPI.getInstance()
.getMessage("System.Console-Not-Allowed-Command", sender)
.toCommandSender(sender);
return true;
}
TranslatableItem item = TranslatableItem.by(key);
if (item == null) {
BlobLibMessageAPI.getInstance()
.getMessage("TranslatableItem.Not-Found", player)
.modder()
.replace("%key%", key)
.get()
.handle(player);
return true;
}
ItemStack clone = item.localize(player).getClone();
PlayerUtil.giveItemToInventoryOrDrop(player, clone);
return true;
}
case "give" -> {
if (length < 4) {
BlobLibMessageAPI.getInstance()
.getMessage("TranslatableItem.Usage", sender)
.toCommandSender(sender);
return true;
}
String playerName = args[3];
Player player = main.getServer().getPlayer(playerName);
if (player == null) {
BlobLibMessageAPI.getInstance()
.getMessage("Player.Not-Found", sender)
.toCommandSender(sender);
return true;
}
TranslatableItem item = TranslatableItem.by(key);
if (item == null) {
BlobLibMessageAPI.getInstance()
.getMessage("TranslatableItem.Not-Found", sender)
.modder()
.replace("%key%", key)
.get()
.toCommandSender(sender);
return true;
}
ItemStack clone = item.localize(player).getClone();
PlayerUtil.giveItemToInventoryOrDrop(player, clone);
return true;
}
default -> {
BlobLibMessageAPI.getInstance()
.getMessage("TranslatableItem.Usage", sender)
.toCommandSender(sender);
return true;
}
}
}
case "update" -> {
PluginUpdater updater;
boolean isPlugin = false;
Expand Down Expand Up @@ -202,19 +275,20 @@ public void debug(CommandSender sender) {
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
if (command.getName().equalsIgnoreCase("bloblib")) {
if (sender.hasPermission("blobblib.admin")) {
List<String> l = new ArrayList<>();
List<String> list = new ArrayList<>();
int length = args.length;
switch (length) {
case 1 -> {
l.add("reload");
l.add("update");
l.add("download");
list.add("reload");
list.add("update");
list.add("download");
list.add("translatableitem");
}
case 2 -> {
String arg = args[0].toLowerCase();
switch (arg) {
case "update" -> {
l.addAll(main.getPluginManager().values().stream()
list.addAll(main.getPluginManager().values().stream()
.map(BlobPlugin::getPluginUpdater)
.filter(Objects::nonNull)
.filter(PluginUpdater::hasAvailableUpdate)
Expand All @@ -223,16 +297,46 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
.toList());
}
case "download" -> {
l.add("GitHub");
list.add("GitHub");
}
case "translatableitem" -> {
list.add("get");
list.add("give");
}
default -> {
}
}
}
case 3 -> {
String arg = args[0].toLowerCase();
if (!arg.equals("translatableitem"))
return list;
String sub = args[1].toLowerCase();
if (sub.equals("get") || sub.equals("give")) {
BlobLibTranslatableAPI.getInstance()
.getTranslatableItems("en_us")
.stream()
.map(TranslatableItem::getReference)
.forEach(list::add);
return list;
}
}
case 4 -> {
String arg = args[0].toLowerCase();
if (!arg.equals("translatableitem"))
return list;
String sub = args[1].toLowerCase();
if (!sub.equals("give"))
return list;
list.addAll(main.getServer().getOnlinePlayers().stream()
.map(Player::getName)
.toList());
return list;
}
default -> {
}
}
return l;
return list;
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@ public abstract class DisplayPackFloatingPet<T extends Display, R extends Clonea
* @param customName - the CustomName of the pet
* (if null will be used 'owner's Pet')
* @param settings - the settings of the pet
* @param packMaster - the pack master
* @param realIndex - the real index of the pet
*/
public DisplayPackFloatingPet(@NotNull Player owner,
@NotNull R display,
@Nullable Particle particle,
@Nullable String customName,
@NotNull DisplayFloatingPetSettings settings,
@NotNull PackMaster<DisplayPackFloatingPet<T, R>> packMaster,
int index) {
int realIndex) {
super(owner, display, particle, customName, settings);
this.packMaster = packMaster;
this.index = index;
this.holdIndex = packMaster.addComponent(this, index);
this.index = realIndex;
this.holdIndex = packMaster.addComponent(this, realIndex);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public int getMaxPerRow() {
* Adds a component to the master at the specified index (real index)
*
* @param component - the component
* @param realIndex - the index
* @param realIndex - the real index
* @return the index the component was added at
*/
public int addComponent(T component, int realIndex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ public static BlobMultiSlotable read(ConfigurationSection section, String key,
String action = null;
if (section.isString("Action"))
action = section.getString("Action");
return new BlobMultiSlotable(list, translatableItem.getClone(), key, permission, price,
ItemStack clone = translatableItem.getClone();
int amount = section.getInt("Amount", 1);
clone.setAmount(amount);
return new BlobMultiSlotable(list, clone, key, permission, price,
priceCurrency, action);
}
ConfigurationSection itemStackSection = section.getConfigurationSection("ItemStack");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ public static MetaBlobMultiSlotable read(ConfigurationSection section, String ke
String action = null;
if (section.isString("Action"))
action = section.getString("Action");
return new MetaBlobMultiSlotable(set, translatableItem.getClone(), key, meta, subMeta,
ItemStack clone = translatableItem.getClone();
int amount = section.getInt("Amount", 1);
clone.setAmount(amount);
return new MetaBlobMultiSlotable(set, clone, key, meta, subMeta,
permission, price, priceCurrency, action);
}
ConfigurationSection itemStackSection = section.getConfigurationSection("ItemStack");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ public UUID getHolderId() {
* @param itemStack The ItemStack to set the button to
*/
public void setButton(int slot, ItemStack itemStack) {
if (!Bukkit.isPrimaryThread()) {
Bukkit.getScheduler().runTask(Bukkit.getPluginManager().getPlugin("BlobLib"), () -> getInventory().setItem(slot, itemStack));
return;
}
getInventory().setItem(slot, itemStack);
}

Expand Down
Loading

0 comments on commit 3a51162

Please sign in to comment.