-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): item & itemfactory to build items based on properties
- Loading branch information
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/de/rexlmanu/boilerplate/item/ConfigItem.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,28 @@ | ||
package de.rexlmanu.boilerplate.item; | ||
|
||
import de.exlll.configlib.Configuration; | ||
import java.util.List; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.experimental.FieldDefaults; | ||
import org.bukkit.Material; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@Builder | ||
@NoArgsConstructor | ||
@Configuration | ||
@FieldDefaults(level = AccessLevel.PROTECTED) | ||
public class ConfigItem { | ||
@Nullable String name; | ||
@Nullable Material material; | ||
@Nullable List<String> lore; | ||
@Nullable Integer amount; | ||
@Nullable String texture; | ||
@Nullable String owner; | ||
@Nullable Integer customModelData; | ||
} |
85 changes: 85 additions & 0 deletions
85
src/main/java/de/rexlmanu/boilerplate/item/ItemFactory.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,85 @@ | ||
package de.rexlmanu.boilerplate.item; | ||
|
||
import com.destroystokyo.paper.profile.PlayerProfile; | ||
import com.destroystokyo.paper.profile.ProfileProperty; | ||
import com.google.inject.Inject; | ||
import com.google.inject.Singleton; | ||
import java.util.UUID; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.kyori.adventure.text.minimessage.MiniMessage; | ||
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; | ||
import org.bukkit.Material; | ||
import org.bukkit.Server; | ||
import org.bukkit.inventory.ItemFlag; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.meta.ItemMeta; | ||
import org.bukkit.inventory.meta.SkullMeta; | ||
|
||
@Singleton | ||
@RequiredArgsConstructor(onConstructor = @__(@Inject)) | ||
@Slf4j | ||
public class ItemFactory { | ||
private final MiniMessage miniMessage; | ||
private final Server server; | ||
|
||
public ItemStack make(ConfigItem configItem, TagResolver... tagResolver) { | ||
if (configItem == null) { | ||
log.warn("ConfigItem is null."); | ||
return new ItemStack(Material.BARRIER); | ||
} | ||
Material material = configItem.material; | ||
|
||
if (material == null || material.isAir()) { | ||
log.warn("Material {} is not valid.", material); | ||
material = Material.BARRIER; | ||
} | ||
|
||
ItemStack itemStack = new ItemStack(material); | ||
if (configItem.amount != null) { | ||
itemStack.setAmount(configItem.amount); | ||
} | ||
|
||
ItemMeta itemMeta = itemStack.getItemMeta(); | ||
|
||
if (!isEmpty(configItem.name)) { | ||
itemMeta.displayName(this.miniMessage.deserialize(configItem.name, tagResolver)); | ||
} | ||
|
||
if (configItem.customModelData != null) itemMeta.setCustomModelData(configItem.customModelData); | ||
|
||
if (configItem.lore != null && !configItem.lore.isEmpty()) { | ||
itemMeta.lore( | ||
configItem.lore.stream() | ||
.map(lore -> this.miniMessage.deserialize(lore, tagResolver)) | ||
.toList()); | ||
} | ||
|
||
if (!isEmpty(configItem.texture) && itemMeta instanceof SkullMeta skullMeta) { | ||
PlayerProfile profile = | ||
this.server.createProfile(createUUIDBasedOnTexture(configItem.texture)); | ||
profile.setProperty(new ProfileProperty("textures", configItem.texture)); | ||
skullMeta.setPlayerProfile(profile); | ||
} | ||
|
||
if (!isEmpty(configItem.owner) && itemMeta instanceof SkullMeta skullMeta) { | ||
skullMeta.setOwningPlayer(this.server.getOfflinePlayer(configItem.owner)); | ||
} | ||
|
||
for (ItemFlag value : ItemFlag.values()) { | ||
itemMeta.addItemFlags(value); | ||
} | ||
|
||
itemStack.setItemMeta(itemMeta); | ||
return itemStack; | ||
} | ||
|
||
private static boolean isEmpty(String string) { | ||
return string == null || string.isEmpty(); | ||
} | ||
|
||
private static UUID createUUIDBasedOnTexture(String texture) { | ||
|
||
return new UUID(texture.hashCode(), texture.hashCode()); | ||
} | ||
} |