Skip to content

Commit

Permalink
Merge pull request #6 from TheNextLvl-net/head
Browse files Browse the repository at this point in the history
optimize head command
  • Loading branch information
NonSwag authored Jun 27, 2024
2 parents 7982993 + 160c096 commit d0d4285
Showing 1 changed file with 7 additions and 27 deletions.
34 changes: 7 additions & 27 deletions src/main/java/net/thenextlvl/tweaks/command/item/HeadCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.destroystokyo.paper.profile.ProfileProperty;
import com.google.gson.JsonParser;
import core.paper.item.ItemBuilder;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.thenextlvl.tweaks.TweaksPlugin;
Expand All @@ -20,7 +21,6 @@
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.UUID;

@CommandInfo(
name = "head",
Expand Down Expand Up @@ -51,7 +51,8 @@ private void value(Player player, String[] args) {
Placeholder.parsed("full_value", value));
else plugin.bundle().sendMessage(player, "item.head.none");
} else {
player.getInventory().addItem(setValue(new ItemStack(Material.PLAYER_HEAD), args[1]));
var head = new ItemBuilder(Material.PLAYER_HEAD).headValue(args[1]);
player.getInventory().addItem(head.ensureServerConversions());
plugin.bundle().sendMessage(player, "item.head.received");
}
}
Expand All @@ -64,7 +65,8 @@ private void url(Player player, String[] args) {
Placeholder.parsed("full_url", url));
else plugin.bundle().sendMessage(player, "item.head.none");
} else {
player.getInventory().addItem(setImgURL(new ItemStack(Material.PLAYER_HEAD), args[1]));
var head = new ItemBuilder(Material.PLAYER_HEAD).headURL(args[1]);
player.getInventory().addItem(head.ensureServerConversions());
plugin.bundle().sendMessage(player, "item.head.received");
}
}
Expand All @@ -76,7 +78,8 @@ private void player(Player player, String[] args) {
Placeholder.parsed("owner", owner));
else plugin.bundle().sendMessage(player, "item.head.none");
} else {
player.getInventory().addItem(setOwner(new ItemStack(Material.PLAYER_HEAD), args[1]));
var head = new ItemBuilder(Material.PLAYER_HEAD).head(args[1]);
player.getInventory().addItem(head.ensureServerConversions());
plugin.bundle().sendMessage(player, "item.head.received");
}
}
Expand All @@ -94,28 +97,6 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
return suggestions;
}

@SuppressWarnings("deprecation")
private static ItemStack setValue(ItemStack item, String base64) {
var id = new UUID(base64.hashCode(), base64.hashCode());
var nbt = "{SkullOwner:{Id:\"" + id + "\",Properties:{textures:[{Value:\"" + base64 + "\"}]}}}";
return Bukkit.getUnsafe().modifyItemStack(item, nbt);
}

private static ItemStack setImgURL(ItemStack item, String url) {
var link = "https://textures.minecraft.net/texture/";
if (!url.startsWith(link)) url = link + url;
var nbt = "{\"textures\":{\"SKIN\":{\"url\":\"" + url + "\"}}}";
var base64 = Base64.getEncoder().encodeToString(nbt.getBytes());
return setValue(item, base64);
}

private static ItemStack setOwner(ItemStack item, String owner) {
if (!(item.getItemMeta() instanceof SkullMeta skull)) return item;
skull.setOwningPlayer(Bukkit.getOfflinePlayer(owner));
item.setItemMeta(skull);
return item;
}

private static @Nullable String getValue(ItemStack item) {
if (!(item.getItemMeta() instanceof SkullMeta skull)) return null;
var profile = skull.getPlayerProfile();
Expand All @@ -141,7 +122,6 @@ private static ItemStack setOwner(ItemStack item, String owner) {
: null;
}


private @Nullable String getOwner(ItemStack item) {
return item.getItemMeta() instanceof SkullMeta skull
? skull.getOwningPlayer() != null
Expand Down

0 comments on commit d0d4285

Please sign in to comment.