Skip to content

Commit 43a3cb0

Browse files
committed
Push local changes from a while ago
These changes were on my PC, pushing to main
1 parent 8d99e93 commit 43a3cb0

File tree

11 files changed

+171
-30
lines changed

11 files changed

+171
-30
lines changed

pom.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@
8181
<id>jitpack.io</id>
8282
<url>https://jitpack.io</url>
8383
</repository>
84+
<repository>
85+
<id>placeholderapi</id>
86+
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
87+
</repository>
8488
</repositories>
8589

8690
<dependencies>
@@ -108,13 +112,19 @@
108112
<groupId>com.github.Burchard36</groupId>
109113
<artifactId>Item-NBT-API</artifactId>
110114
<version>3.0.3</version>
111-
<scope>compile</scope>
115+
<scope>compil</scope>
112116
</dependency>
113117
<dependency>
114118
<groupId>com.github.MilkBowl</groupId>
115119
<artifactId>VaultAPI</artifactId>
116120
<version>1.7</version>
117121
<scope>provided</scope>
118122
</dependency>
123+
<dependency>
124+
<groupId>me.clip</groupId>
125+
<artifactId>placeholderapi</artifactId>
126+
<version>2.11.1</version>
127+
<scope>provided</scope>
128+
</dependency>
119129
</dependencies>
120130
</project>

src/main/java/com/burchard36/api/ApiSettings.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.burchard36.api.command.ApiCommand;
44
import com.burchard36.api.data.DataStore;
55
import lombok.Getter;
6+
import org.bukkit.Bukkit;
67

78
import java.util.ArrayList;
89
import java.util.List;
@@ -29,7 +30,7 @@ public class ApiSettings {
2930
private final CommandSettings commandSettings;
3031

3132
protected ApiSettings() {
32-
this.papiSupport = true;
33+
this.papiSupport = Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
3334
this.vaultPermissionSupport = true;
3435
this.useCommandModule = true;
3536
this.useInventoryModule = true;
@@ -55,12 +56,15 @@ public final ApiSettings setVaultPermissionSupport(boolean value) {
5556

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

src/main/java/com/burchard36/api/BurchAPI.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
import com.burchard36.api.utils.reflections.PackageScanner;
99
import com.google.gson.GsonBuilder;
1010
import lombok.Getter;
11+
import me.clip.placeholderapi.PlaceholderAPI;
1112
import net.milkbowl.vault.permission.Permission;
1213
import org.bukkit.ChatColor;
14+
import org.bukkit.entity.Player;
1315
import org.bukkit.plugin.RegisteredServiceProvider;
1416
import org.bukkit.plugin.java.JavaPlugin;
1517

18+
import javax.annotation.Nullable;
1619
import java.util.ArrayList;
1720
import java.util.List;
1821

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

145148
/**
146149
* Converts a normal string to a formatted one
150+
* <br>
151+
* This will also parse PlaceholderAPI placeholders
147152
* @param message Any {@link String} to convert
153+
* @param p Any {@link Player}, you can provide null if you do not have one
148154
* @return Converted String
149155
* @since 2.1.5
150156
*/
151-
public static String convert(final String message) {
157+
public static String convert(String message, @Nullable Player p) {
158+
message = String.format(message, "playernamehere", "servernamehere");
159+
if (p != null && INSTANCE.getApiSettings().isPapiSupport()) {
160+
message = PlaceholderAPI.setPlaceholders(p, message);
161+
}
152162
return ChatColor.translateAlternateColorCodes('&', message);
153163
}
154164

155165
/**
156166
* Converts a varargs {@link String} to colored {@link List} of {@link String}'s
167+
* <br>
168+
* This will also parse for PlaceholderAPI placeholders
169+
* @param p Any {@link Player}, you can provide null if you do not have one
157170
* @param message Varargs of any {@link String} to convert to colored format using ampersand color codes
158171
* @return A {@link List} of colored {@link String}'s (using ampersand color codes)
159172
* @since 2.1.5
160173
*/
161-
public static List<String> convert(final String... message) {
174+
public static List<String> convert(@Nullable Player p, final String... message) {
162175
final List<String> list = new ArrayList<>();
163176
for (int i = 0; i <= message.length; i++) {
164-
list.add(convert(message[i]));
177+
list.add(convert(message[i], p));
165178
}
166179
return list;
167180
}
Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,46 @@
11
package com.burchard36.api;
22

33
import com.burchard36.api.command.ApiCommand;
4+
import com.burchard36.api.command.actions.SubArgument;
5+
import com.burchard36.api.command.annotation.CommandName;
6+
import org.bukkit.Material;
7+
import org.bukkit.OfflinePlayer;
8+
import org.bukkit.entity.Player;
9+
import org.bukkit.inventory.ItemStack;
410

11+
@CommandName(name = "help")
512
public class TestArgument extends ApiCommand {
613

14+
public static void onArgument(SubArgument arg) {
15+
16+
}
17+
718
public TestArgument() {
8-
this.subArgument("test", (subArgument) -> {
9-
if (!subArgument.senderIsPlayer()) return;
1019

11-
});
20+
this.subArgument("testtt", null, TestArgument::onArgument)
21+
.subArgument("test", null, (subArgument) -> {
22+
if (!subArgument.senderIsPlayer()) return;
23+
24+
})
25+
.subArgument("test %player%", "needs.me", (subArgument) -> {
26+
if (subArgument.playerPresentAt(1).isEmpty()) return;// send msg, maybe have boolean return
27+
final OfflinePlayer player = subArgument.playerPresentAt(1).get();
28+
subArgument.commandSender().sendMessage("You sent me: " + player.getName());
29+
})
30+
.subArgument("give %player% %item% %amount%", "needs.give", (subArgument) -> {
31+
if (subArgument.playerPresentAt(1).isEmpty()) return; // send msg, maybe have boolean return
32+
if (subArgument.integerPresentAt(2).isEmpty()) return; // send msg, maybe have boolean return
33+
if (subArgument.materialPresentAt(3).isEmpty()) return; // send msg, maybe have boolean return
34+
35+
final OfflinePlayer offlinePlayer = subArgument.playerPresentAt(1).get();
36+
if (offlinePlayer.isOnline()) {
37+
final Player player = ((Player) offlinePlayer);
38+
final Material mat = subArgument.materialPresentAt(3).get();
39+
final int amt = subArgument.integerPresentAt(2).get();
40+
player.getInventory().addItem(new ItemStack(mat, amt));
41+
}
42+
})
43+
.onPlayerSender((playerSent) -> playerSent.player().sendMessage("Dumbass you need to send arguments"))
44+
.onConsoleSender((consoleSent) -> consoleSent.console().sendMessage("Your even more stupid, you sent it through console!"));
1245
}
1346
}

src/main/java/com/burchard36/api/command/actions/SubArgument.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,44 @@
11
package com.burchard36.api.command.actions;
22

33
import com.burchard36.api.utils.Logger;
4+
import org.bukkit.Bukkit;
5+
import org.bukkit.Material;
6+
import org.bukkit.OfflinePlayer;
47
import org.bukkit.command.CommandSender;
58
import org.bukkit.entity.Player;
69

7-
public record SubArgument(CommandSender commandSender) {
10+
import java.util.List;
11+
import java.util.Optional;
12+
13+
public record SubArgument(CommandSender commandSender, List<String> args) {
814

915
public boolean senderIsPlayer() {
1016
return commandSender instanceof Player;
1117
}
1218

19+
public Optional<OfflinePlayer> playerPresentAt(int index) {
20+
if (Bukkit.getOnlinePlayers().stream().anyMatch(p -> p.getName().equals(args.get(index))))
21+
return Optional.of(Bukkit.getOfflinePlayer(Bukkit.getPlayer(args.get(index)).getUniqueId()));
22+
if (!Bukkit.getOfflinePlayer(args.get(index)).hasPlayedBefore()) return Optional.empty();
23+
return Optional.of(Bukkit.getOfflinePlayer(args.get(index)));
24+
}
25+
26+
public Optional<Material> materialPresentAt(int index) {
27+
final Material mat = Material.getMaterial(args.get(index));
28+
if (mat == null) return Optional.empty();
29+
return Optional.of(mat);
30+
}
31+
32+
public Optional<Integer> integerPresentAt(int index) {
33+
final int integer;
34+
try {
35+
integer = Integer.parseInt(args.get(index));
36+
return Optional.of(integer);
37+
} catch (NumberFormatException ignored) {
38+
return Optional.empty();
39+
}
40+
}
41+
1342
/**
1443
* Gets a Player object from this SubArgument, only if the {@link CommandSender} is a {@link Player}
1544
* @return null if {@link SubArgument#commandSender()} is not a {@link Player}, otherwise returns a player object

src/main/java/com/burchard36/api/data/json/PlayerJsonDataStore.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ public void onEnable() {
7272
// same thing as previous error
7373
Logger.warn("ERR_INVALID_CONSTRUCTOR result from auto-loading" + keyClass.getName() + " class!");
7474
}
75+
76+
7577
}
7678
} else if (this.dataClass == null){
7779
final JsonPlayerDataFile dataFile = invocationResult.getKey();

src/main/java/com/burchard36/api/inventory/PluginInventory.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ public class PluginInventory {
5454
*/
5555
public PluginInventory(final int slots, final String name) {
5656
this.inventorySlots = slots;
57-
this.inventoryName = convert(name);
57+
this.inventoryName = convert(name, null);
5858
UUID inventoryUuid = UUID.randomUUID();
5959
this.inventoryHolder = new PluginHolder(inventoryUuid, null);
60-
this.inventory = Bukkit.createInventory(this.inventoryHolder, slots, convert(name));
60+
this.inventory = Bukkit.createInventory(this.inventoryHolder, slots, convert(name, null));
6161
}
6262

6363
/**
@@ -66,7 +66,7 @@ public PluginInventory(final int slots, final String name) {
6666
* @return instance of this class
6767
*/
6868
public PluginInventory setDisplayName(final String displayName) {
69-
this.inventoryName = convert(displayName);
69+
this.inventoryName = convert(displayName, null);
7070
this.inventory = Bukkit.createInventory(this.inventoryHolder, this.inventorySlots, this.inventoryName);
7171
return this;
7272
}
@@ -184,6 +184,10 @@ public PluginInventory fillWith(final List<ClickableItem> items, final boolean o
184184
* @param player Player to open inventory to
185185
*/
186186
public void open(final Player player) {
187+
this.inventory = /* Parse placeholders in the Inventory Title for provided player */
188+
Bukkit.createInventory(this.inventoryHolder,
189+
this.inventorySlots,
190+
convert(this.inventoryName, player));
187191
this.clickableItems.keySet().forEach((slotNum) -> {
188192
this.inventory.setItem(slotNum, this.clickableItems.get(slotNum).build());
189193
});

src/main/java/com/burchard36/api/utils/EntityWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public EntityWrapper(final Entity entity) {
2727
*/
2828
public final void setHologram(final String message) {
2929
this.entity.setCustomNameVisible(true);
30-
this.entity.setCustomName(BurchAPI.convert(message));
30+
this.entity.setCustomName(BurchAPI.convert(message, null));
3131
}
3232

3333
/**

src/main/java/com/burchard36/api/utils/ItemWrapper.java

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
import org.bukkit.Material;
66
import org.bukkit.NamespacedKey;
77
import org.bukkit.enchantments.Enchantment;
8+
import org.bukkit.entity.Player;
89
import org.bukkit.inventory.ItemStack;
910
import org.bukkit.inventory.meta.ItemMeta;
1011
import org.bukkit.inventory.meta.SkullMeta;
1112
import org.bukkit.persistence.PersistentDataContainer;
1213
import org.bukkit.persistence.PersistentDataType;
1314

1415
import javax.annotation.Nonnull;
16+
import javax.annotation.Nullable;
1517
import java.util.ArrayList;
1618
import java.util.HashMap;
1719
import java.util.List;
@@ -24,19 +26,19 @@
2426
*/
2527
public class ItemWrapper {
2628

27-
private final ItemStack itemStack;
28-
private ItemMeta itemMeta;
29-
private String displayName = null;
30-
private List<String> lore = new ArrayList<>();
29+
protected final ItemStack itemStack;
30+
protected ItemMeta itemMeta;
31+
protected String displayName = null;
32+
protected List<String> lore = new ArrayList<>();
33+
protected @Nullable Player owningPlayer = null;
3134

3235
/**
3336
* Specifies directly what {@link ItemStack} you want to wrap around
3437
* @param stack A {@link ItemStack}
3538
*/
36-
public ItemWrapper(final ItemStack stack) {
37-
if (stack == null) throw new IllegalArgumentException("ItemStack in ItemWrapper cannot be null!");
39+
public ItemWrapper(@Nonnull final ItemStack stack) {
3840
this.itemStack = stack;
39-
if (this.itemStack.getType() != Material.AIR) this.itemMeta = this.itemStack.getItemMeta();
41+
this.itemMeta = this.itemStack.getItemMeta();
4042
}
4143

4244
/**
@@ -45,7 +47,7 @@ public ItemWrapper(final ItemStack stack) {
4547
*/
4648
public ItemWrapper(@Nonnull final Material material) {
4749
this.itemStack = new ItemStack(material, 1);
48-
if (this.itemStack.getType() != Material.AIR) this.itemMeta = this.itemStack.getItemMeta();
50+
this.itemMeta = this.itemStack.getItemMeta();
4951
}
5052

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

63+
/**
64+
* Specifies directly what {@link ItemStack} you want to wrap around
65+
* <br>
66+
* The {@link Player} argument is for if you want certain support for things like PlaceholderAPI to parse
67+
* placeholders on this item for a specific player
68+
* @param stack An {@link ItemStack} cant be null
69+
* @param player A {@link Player} is nullable
70+
*/
71+
public ItemWrapper(final @Nonnull ItemStack stack, @Nullable Player player) {
72+
this.itemStack = stack;
73+
this.itemMeta = this.itemStack.getItemMeta();
74+
this.owningPlayer = player;
75+
}
76+
77+
/**
78+
* Creates a {@link ItemStack} with a {@link Integer} of 1 as the amount
79+
* <br>
80+
* This also allows you to put a {@link Player} as the second argument, to allow PlaceholderAPI parsing.
81+
* @param material A {@link Material} enum
82+
* @param player A nullable {@link Player}
83+
*/
84+
public ItemWrapper(@Nonnull final Material material, @Nullable final Player player) {
85+
this.itemStack = new ItemStack(material, 1);
86+
this.itemMeta = this.itemStack.getItemMeta();
87+
this.owningPlayer = player;
88+
}
89+
90+
/**
91+
* Creates a {@link ItemStack} with a specific {@link Material} and {@link Integer} amount
92+
* <br>
93+
* This also allows you to specify a {@link Player} for PlaceholderAPI parsing.
94+
* @param material A {@link Material} enum
95+
* @param amount A {@link Integer} amount, value cant be <= 0
96+
* @param player A nullable {@link Player}
97+
*/
98+
public ItemWrapper (@Nonnull final Material material,
99+
final int amount,
100+
@Nullable final Player player) {
101+
this.itemStack = new ItemStack(material, amount);
102+
this.itemMeta = this.itemStack.getItemMeta();
103+
this.owningPlayer = player;
104+
}
105+
61106
/**
62107
* Might be null
63108
* @return String of display name, or null
@@ -82,7 +127,7 @@ public final List<String> getLore() {
82127
public final ItemWrapper setDisplayName(final String displayName) {
83128
if (this.itemMeta == null) return this;
84129
this.displayName = displayName;
85-
this.itemMeta.setDisplayName(convert(this.displayName));
130+
this.itemMeta.setDisplayName(convert(this.displayName, null));
86131
this.itemStack.setItemMeta(this.itemMeta);
87132
return this;
88133
}
@@ -94,7 +139,7 @@ public final ItemWrapper setDisplayName(final String displayName) {
94139
*/
95140
public final ItemWrapper addItemLore(final String... lore) {
96141
for (int i = 0; i <= lore.length; i++) {
97-
this.lore.add(convert(lore[i]));
142+
this.lore.add(convert(lore[i], null));
98143
}
99144
this.setItemLore(this.lore);
100145
return this;
@@ -110,7 +155,7 @@ public final ItemWrapper setItemLore(final List<String> itemLore) {
110155
this.lore = itemLore;
111156
final List<String> colored = new ArrayList<>();
112157
itemLore.forEach((loreItem) -> {
113-
colored.add(convert(loreItem));
158+
colored.add(convert(loreItem, null));
114159
});
115160
this.itemMeta.setLore(colored);
116161
this.itemStack.setItemMeta(this.itemMeta);

0 commit comments

Comments
 (0)