Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
Remove unnecessary metadata from arrows
Browse files Browse the repository at this point in the history
  • Loading branch information
LlewVallis committed Nov 24, 2020
1 parent 4612b1d commit f4affb3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.destroystokyo.paper.Title;
import lombok.SneakyThrows;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.TextComponent;
import org.astropeci.omw.teams.GlobalTeamManager;
import org.bukkit.Bukkit;
Expand All @@ -18,6 +20,7 @@

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class PeriodicItemDispenser implements AutoCloseable {

Expand Down Expand Up @@ -121,6 +124,10 @@ private void giveItem(Player player, ItemStack item) {
}

String name = item.getItemMeta().getDisplayName();
if (name.isBlank()) {
name = defaultItemName(item.getType());
}

if (item.getAmount() > 1) {
name += "s";
}
Expand All @@ -129,6 +136,27 @@ private void giveItem(Player player, ItemStack item) {
}
}

private String defaultItemName(Material material) {
StringBuilder result = new StringBuilder();
boolean nextCharShouldBeCapitalised = true;

for (char chr : material.name().toCharArray()) {
if (nextCharShouldBeCapitalised) {
result.append(Character.toUpperCase(chr));
} else {
result.append(Character.toLowerCase(chr));
}

nextCharShouldBeCapitalised = false;

if (Character.isSpaceChar(chr)) {
nextCharShouldBeCapitalised = true;
}
}

return result.toString();
}

@SneakyThrows({ InvalidConfigurationException.class })
private Set<ItemStack> getItemsFromConfig() {
Set<ItemStack> items = new HashSet<>();
Expand Down Expand Up @@ -157,10 +185,10 @@ private Set<ItemStack> getItemsFromConfig() {
throw new InvalidConfigurationException("item amount was not an integer");
}

Object nameObject = getRequiredConfigProperty("name", itemConfiguration);
Object nameObject = itemConfiguration.get("name");
if (nameObject instanceof String) {
meta.setDisplayName(ChatColor.RESET.toString() + nameObject);
} else {
} else if (nameObject != null) {
throw new InvalidConfigurationException("item name was not a string");
}

Expand Down
1 change: 0 additions & 1 deletion plugin/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ items:
name: Shield

- material: ARROW
name: Arrow
amount: 3

settings:
Expand Down

0 comments on commit f4affb3

Please sign in to comment.