Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
m1919810 committed Dec 29, 2024
1 parent 6664da3 commit 599861a
Showing 1 changed file with 25 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.thebusybiscuit.slimefun4.api.items;

import io.github.bakedlibs.dough.common.CommonPatterns;
import io.github.bakedlibs.dough.items.CustomItemStack;
import io.github.bakedlibs.dough.items.ItemMetaSnapshot;
import io.github.bakedlibs.dough.skins.PlayerHead;
import io.github.bakedlibs.dough.skins.PlayerSkin;
Expand Down Expand Up @@ -46,46 +47,39 @@ public class SlimefunItemStack extends ItemStack {
private boolean locked = false;
private String texture = null;

public SlimefunItemStack(@Nonnull String id, @Nonnull ItemStack item) {
public SlimefunItemStack(@Nonnull String id, @Nonnull ItemStack item, @Nonnull Consumer<ItemMeta> consumer) {
super(item.getType(), item.getAmount());

if (item.hasItemMeta()) {
setItemMeta(item.getItemMeta());
}

Validate.notNull(id, "The Item id must never be null!");
Validate.isTrue(
id.equals(id.toUpperCase(Locale.ROOT)), "Slimefun Item Ids must be uppercase! (e.g. 'MY_ITEM_ID')");
id.equals(id.toUpperCase(Locale.ROOT)), "Slimefun Item Ids must be uppercase! (e.g. 'MY_ITEM_ID')");

if (Slimefun.instance() == null) {
throw new PrematureCodeException(
"A SlimefunItemStack must never be be created before your Plugin was enabled.");
"A SlimefunItemStack must never be be created before your Plugin was enabled.");
}

this.id = id;

ItemMeta meta = getItemMeta();
ItemMeta meta = item.hasItemMeta()?item.getItemMeta(): getItemMeta();

Slimefun.getItemDataService().setItemData(meta, id);
Slimefun.getItemTextureService().setTexture(meta, id);
consumer.accept(meta);

setItemMeta(meta);
}

public SlimefunItemStack(@Nonnull String id, @Nonnull ItemStack item, @Nonnull Consumer<ItemMeta> consumer) {
this(id, item);

ItemMeta im = getItemMeta();
consumer.accept(im);
setItemMeta(im);
private static final Consumer<ItemMeta> DEFAULT_CONSUMER=(itemMeta -> {});
public SlimefunItemStack(@Nonnull String id, @Nonnull ItemStack item) {
this(id, item,DEFAULT_CONSUMER);
}

public SlimefunItemStack(@Nonnull String id, @Nonnull Material type, @Nonnull Consumer<ItemMeta> consumer) {
this(id, new ItemStack(type), consumer);
}

public SlimefunItemStack(
@Nonnull String id, @Nonnull Material type, @Nullable String name, @Nonnull Consumer<ItemMeta> consumer) {
@Nonnull String id, @Nonnull Material type, @Nullable String name, @Nonnull Consumer<ItemMeta> consumer) {
this(id, type, meta -> {
if (name != null) {
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
Expand Down Expand Up @@ -117,7 +111,7 @@ public SlimefunItemStack(@Nonnull String id, @Nonnull Material type, @Nullable S
}

public SlimefunItemStack(
@Nonnull String id, @Nonnull Material type, @Nonnull Color color, @Nullable String name, String... lore) {
@Nonnull String id, @Nonnull Material type, @Nonnull Color color, @Nullable String name, String... lore) {
this(id, type, im -> {
if (name != null) {
im.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
Expand All @@ -144,11 +138,11 @@ public SlimefunItemStack(
}

public SlimefunItemStack(
@Nonnull String id,
@Nonnull Color color,
@Nonnull PotionEffect effect,
@Nullable String name,
String... lore) {
@Nonnull String id,
@Nonnull Color color,
@Nonnull PotionEffect effect,
@Nullable String name,
String... lore) {
this(id, Material.POTION, im -> {
if (name != null) {
im.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
Expand Down Expand Up @@ -190,7 +184,7 @@ public SlimefunItemStack(@Nonnull String id, @Nonnull HeadTexture head, @Nullabl
}

public SlimefunItemStack(
@Nonnull String id, @Nonnull String texture, @Nullable String name, @Nonnull Consumer<ItemMeta> consumer) {
@Nonnull String id, @Nonnull String texture, @Nullable String name, @Nonnull Consumer<ItemMeta> consumer) {
this(id, getSkull(id, texture), meta -> {
if (name != null) {
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
Expand Down Expand Up @@ -309,17 +303,22 @@ public void lock() {
return texture;
} else if (CommonPatterns.HEXADECIMAL.matcher(texture).matches()) {
String value =
"{\"textures\":{\"SKIN\":{\"url\":\"http://textures.minecraft.net/texture/" + texture + "\"}}}";
"{\"textures\":{\"SKIN\":{\"url\":\"http://textures.minecraft.net/texture/" + texture + "\"}}}";
return Base64.getEncoder().encodeToString(value.getBytes(StandardCharsets.UTF_8));
} else {
throw new IllegalArgumentException(
"The provided texture for Item \"" + id + "\" does not seem to be a valid texture String!");
"The provided texture for Item \"" + id + "\" does not seem to be a valid texture String!");
}
}

@Override
public ItemStack clone() {
return new SlimefunItemStack(id, this);
//return new SlimefunItemStack(id, this);
ItemStack stack=super.clone();
if(stack instanceof SlimefunItemStack sfitem){
sfitem.locked=false;
}
return stack;
}

@Override
Expand Down

0 comments on commit 599861a

Please sign in to comment.