Skip to content

Commit

Permalink
Push local changes from a while ago
Browse files Browse the repository at this point in the history
These changes were on my PC, pushing to main
  • Loading branch information
Burchard36 committed Jun 6, 2023
1 parent 8d99e93 commit 43a3cb0
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 30 deletions.
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>placeholderapi</id>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
</repositories>

<dependencies>
Expand Down Expand Up @@ -108,13 +112,19 @@
<groupId>com.github.Burchard36</groupId>
<artifactId>Item-NBT-API</artifactId>
<version>3.0.3</version>
<scope>compile</scope>
<scope>compil</scope>
</dependency>
<dependency>
<groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.11.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
8 changes: 6 additions & 2 deletions src/main/java/com/burchard36/api/ApiSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.burchard36.api.command.ApiCommand;
import com.burchard36.api.data.DataStore;
import lombok.Getter;
import org.bukkit.Bukkit;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -29,7 +30,7 @@ public class ApiSettings {
private final CommandSettings commandSettings;

protected ApiSettings() {
this.papiSupport = true;
this.papiSupport = Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
this.vaultPermissionSupport = true;
this.useCommandModule = true;
this.useInventoryModule = true;
Expand All @@ -55,12 +56,15 @@ public final ApiSettings setVaultPermissionSupport(boolean value) {

/**
* Sets whether you want PlaceholderAPI support (true) or want none (false)
* <br>
* This value is defaulted to true if PlaceholderAPI is already running on the server, do note
* your plugin still needs to depend or soft-depend in the plugin.yml for PlaceholderAPI
* @param value A {@link Boolean}
* @return Instance of this class for chaining
* @since 2.1.8
*/
public final ApiSettings setPlaceholderAPISupport(boolean value) {
this.papiSupport = value;
this.papiSupport = value && Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
return this;
}

Expand Down
19 changes: 16 additions & 3 deletions src/main/java/com/burchard36/api/BurchAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
import com.burchard36.api.utils.reflections.PackageScanner;
import com.google.gson.GsonBuilder;
import lombok.Getter;
import me.clip.placeholderapi.PlaceholderAPI;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -144,24 +147,34 @@ public final <T> PackageScanner<T> newPackageScanner() {

/**
* Converts a normal string to a formatted one
* <br>
* This will also parse PlaceholderAPI placeholders
* @param message Any {@link String} to convert
* @param p Any {@link Player}, you can provide null if you do not have one
* @return Converted String
* @since 2.1.5
*/
public static String convert(final String message) {
public static String convert(String message, @Nullable Player p) {
message = String.format(message, "playernamehere", "servernamehere");
if (p != null && INSTANCE.getApiSettings().isPapiSupport()) {
message = PlaceholderAPI.setPlaceholders(p, message);
}
return ChatColor.translateAlternateColorCodes('&', message);
}

/**
* Converts a varargs {@link String} to colored {@link List} of {@link String}'s
* <br>
* This will also parse for PlaceholderAPI placeholders
* @param p Any {@link Player}, you can provide null if you do not have one
* @param message Varargs of any {@link String} to convert to colored format using ampersand color codes
* @return A {@link List} of colored {@link String}'s (using ampersand color codes)
* @since 2.1.5
*/
public static List<String> convert(final String... message) {
public static List<String> convert(@Nullable Player p, final String... message) {
final List<String> list = new ArrayList<>();
for (int i = 0; i <= message.length; i++) {
list.add(convert(message[i]));
list.add(convert(message[i], p));
}
return list;
}
Expand Down
39 changes: 36 additions & 3 deletions src/main/java/com/burchard36/api/TestArgument.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
package com.burchard36.api;

import com.burchard36.api.command.ApiCommand;
import com.burchard36.api.command.actions.SubArgument;
import com.burchard36.api.command.annotation.CommandName;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

@CommandName(name = "help")
public class TestArgument extends ApiCommand {

public static void onArgument(SubArgument arg) {

}

public TestArgument() {
this.subArgument("test", (subArgument) -> {
if (!subArgument.senderIsPlayer()) return;

});
this.subArgument("testtt", null, TestArgument::onArgument)
.subArgument("test", null, (subArgument) -> {
if (!subArgument.senderIsPlayer()) return;

})
.subArgument("test %player%", "needs.me", (subArgument) -> {
if (subArgument.playerPresentAt(1).isEmpty()) return;// send msg, maybe have boolean return
final OfflinePlayer player = subArgument.playerPresentAt(1).get();
subArgument.commandSender().sendMessage("You sent me: " + player.getName());
})
.subArgument("give %player% %item% %amount%", "needs.give", (subArgument) -> {
if (subArgument.playerPresentAt(1).isEmpty()) return; // send msg, maybe have boolean return
if (subArgument.integerPresentAt(2).isEmpty()) return; // send msg, maybe have boolean return
if (subArgument.materialPresentAt(3).isEmpty()) return; // send msg, maybe have boolean return

final OfflinePlayer offlinePlayer = subArgument.playerPresentAt(1).get();
if (offlinePlayer.isOnline()) {
final Player player = ((Player) offlinePlayer);
final Material mat = subArgument.materialPresentAt(3).get();
final int amt = subArgument.integerPresentAt(2).get();
player.getInventory().addItem(new ItemStack(mat, amt));
}
})
.onPlayerSender((playerSent) -> playerSent.player().sendMessage("Dumbass you need to send arguments"))
.onConsoleSender((consoleSent) -> consoleSent.console().sendMessage("Your even more stupid, you sent it through console!"));
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
package com.burchard36.api.command.actions;

import com.burchard36.api.utils.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public record SubArgument(CommandSender commandSender) {
import java.util.List;
import java.util.Optional;

public record SubArgument(CommandSender commandSender, List<String> args) {

public boolean senderIsPlayer() {
return commandSender instanceof Player;
}

public Optional<OfflinePlayer> playerPresentAt(int index) {
if (Bukkit.getOnlinePlayers().stream().anyMatch(p -> p.getName().equals(args.get(index))))
return Optional.of(Bukkit.getOfflinePlayer(Bukkit.getPlayer(args.get(index)).getUniqueId()));
if (!Bukkit.getOfflinePlayer(args.get(index)).hasPlayedBefore()) return Optional.empty();
return Optional.of(Bukkit.getOfflinePlayer(args.get(index)));
}

public Optional<Material> materialPresentAt(int index) {
final Material mat = Material.getMaterial(args.get(index));
if (mat == null) return Optional.empty();
return Optional.of(mat);
}

public Optional<Integer> integerPresentAt(int index) {
final int integer;
try {
integer = Integer.parseInt(args.get(index));
return Optional.of(integer);
} catch (NumberFormatException ignored) {
return Optional.empty();
}
}

/**
* Gets a Player object from this SubArgument, only if the {@link CommandSender} is a {@link Player}
* @return null if {@link SubArgument#commandSender()} is not a {@link Player}, otherwise returns a player object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public void onEnable() {
// same thing as previous error
Logger.warn("ERR_INVALID_CONSTRUCTOR result from auto-loading" + keyClass.getName() + " class!");
}


}
} else if (this.dataClass == null){
final JsonPlayerDataFile dataFile = invocationResult.getKey();
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/burchard36/api/inventory/PluginInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public class PluginInventory {
*/
public PluginInventory(final int slots, final String name) {
this.inventorySlots = slots;
this.inventoryName = convert(name);
this.inventoryName = convert(name, null);
UUID inventoryUuid = UUID.randomUUID();
this.inventoryHolder = new PluginHolder(inventoryUuid, null);
this.inventory = Bukkit.createInventory(this.inventoryHolder, slots, convert(name));
this.inventory = Bukkit.createInventory(this.inventoryHolder, slots, convert(name, null));
}

/**
Expand All @@ -66,7 +66,7 @@ public PluginInventory(final int slots, final String name) {
* @return instance of this class
*/
public PluginInventory setDisplayName(final String displayName) {
this.inventoryName = convert(displayName);
this.inventoryName = convert(displayName, null);
this.inventory = Bukkit.createInventory(this.inventoryHolder, this.inventorySlots, this.inventoryName);
return this;
}
Expand Down Expand Up @@ -184,6 +184,10 @@ public PluginInventory fillWith(final List<ClickableItem> items, final boolean o
* @param player Player to open inventory to
*/
public void open(final Player player) {
this.inventory = /* Parse placeholders in the Inventory Title for provided player */
Bukkit.createInventory(this.inventoryHolder,
this.inventorySlots,
convert(this.inventoryName, player));
this.clickableItems.keySet().forEach((slotNum) -> {
this.inventory.setItem(slotNum, this.clickableItems.get(slotNum).build());
});
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/burchard36/api/utils/EntityWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public EntityWrapper(final Entity entity) {
*/
public final void setHologram(final String message) {
this.entity.setCustomNameVisible(true);
this.entity.setCustomName(BurchAPI.convert(message));
this.entity.setCustomName(BurchAPI.convert(message, null));
}

/**
Expand Down
67 changes: 56 additions & 11 deletions src/main/java/com/burchard36/api/utils/ItemWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -24,19 +26,19 @@
*/
public class ItemWrapper {

private final ItemStack itemStack;
private ItemMeta itemMeta;
private String displayName = null;
private List<String> lore = new ArrayList<>();
protected final ItemStack itemStack;
protected ItemMeta itemMeta;
protected String displayName = null;
protected List<String> lore = new ArrayList<>();
protected @Nullable Player owningPlayer = null;

/**
* Specifies directly what {@link ItemStack} you want to wrap around
* @param stack A {@link ItemStack}
*/
public ItemWrapper(final ItemStack stack) {
if (stack == null) throw new IllegalArgumentException("ItemStack in ItemWrapper cannot be null!");
public ItemWrapper(@Nonnull final ItemStack stack) {
this.itemStack = stack;
if (this.itemStack.getType() != Material.AIR) this.itemMeta = this.itemStack.getItemMeta();
this.itemMeta = this.itemStack.getItemMeta();
}

/**
Expand All @@ -45,7 +47,7 @@ public ItemWrapper(final ItemStack stack) {
*/
public ItemWrapper(@Nonnull final Material material) {
this.itemStack = new ItemStack(material, 1);
if (this.itemStack.getType() != Material.AIR) this.itemMeta = this.itemStack.getItemMeta();
this.itemMeta = this.itemStack.getItemMeta();
}

/**
Expand All @@ -58,6 +60,49 @@ public ItemWrapper(@Nonnull final Material material, final int amount) {
if (this.itemStack.getType() != Material.AIR) this.itemMeta = this.itemStack.getItemMeta();
}

/**
* Specifies directly what {@link ItemStack} you want to wrap around
* <br>
* The {@link Player} argument is for if you want certain support for things like PlaceholderAPI to parse
* placeholders on this item for a specific player
* @param stack An {@link ItemStack} cant be null
* @param player A {@link Player} is nullable
*/
public ItemWrapper(final @Nonnull ItemStack stack, @Nullable Player player) {
this.itemStack = stack;
this.itemMeta = this.itemStack.getItemMeta();
this.owningPlayer = player;
}

/**
* Creates a {@link ItemStack} with a {@link Integer} of 1 as the amount
* <br>
* This also allows you to put a {@link Player} as the second argument, to allow PlaceholderAPI parsing.
* @param material A {@link Material} enum
* @param player A nullable {@link Player}
*/
public ItemWrapper(@Nonnull final Material material, @Nullable final Player player) {
this.itemStack = new ItemStack(material, 1);
this.itemMeta = this.itemStack.getItemMeta();
this.owningPlayer = player;
}

/**
* Creates a {@link ItemStack} with a specific {@link Material} and {@link Integer} amount
* <br>
* This also allows you to specify a {@link Player} for PlaceholderAPI parsing.
* @param material A {@link Material} enum
* @param amount A {@link Integer} amount, value cant be <= 0
* @param player A nullable {@link Player}
*/
public ItemWrapper (@Nonnull final Material material,
final int amount,
@Nullable final Player player) {
this.itemStack = new ItemStack(material, amount);
this.itemMeta = this.itemStack.getItemMeta();
this.owningPlayer = player;
}

/**
* Might be null
* @return String of display name, or null
Expand All @@ -82,7 +127,7 @@ public final List<String> getLore() {
public final ItemWrapper setDisplayName(final String displayName) {
if (this.itemMeta == null) return this;
this.displayName = displayName;
this.itemMeta.setDisplayName(convert(this.displayName));
this.itemMeta.setDisplayName(convert(this.displayName, null));
this.itemStack.setItemMeta(this.itemMeta);
return this;
}
Expand All @@ -94,7 +139,7 @@ public final ItemWrapper setDisplayName(final String displayName) {
*/
public final ItemWrapper addItemLore(final String... lore) {
for (int i = 0; i <= lore.length; i++) {
this.lore.add(convert(lore[i]));
this.lore.add(convert(lore[i], null));
}
this.setItemLore(this.lore);
return this;
Expand All @@ -110,7 +155,7 @@ public final ItemWrapper setItemLore(final List<String> itemLore) {
this.lore = itemLore;
final List<String> colored = new ArrayList<>();
itemLore.forEach((loreItem) -> {
colored.add(convert(loreItem));
colored.add(convert(loreItem, null));
});
this.itemMeta.setLore(colored);
this.itemStack.setItemMeta(this.itemMeta);
Expand Down
Loading

0 comments on commit 43a3cb0

Please sign in to comment.