Skip to content

Commit

Permalink
fix: head module
Browse files Browse the repository at this point in the history
  • Loading branch information
sakurawald committed May 19, 2024
1 parent 6a40b04 commit 3119166
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package io.github.sakurawald.module.initializer.head.api;

import com.mojang.serialization.MapDecoder;
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import io.github.sakurawald.Fuji;
import io.github.sakurawald.util.RegistryUtil;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.client.gui.screen.world.EditWorldScreen;
import net.minecraft.client.session.telemetry.TelemetryEventProperty;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.nbt.NbtHelper;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;
import net.minecraft.component.type.ProfileComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtString;
import net.minecraft.text.Text;
import net.minecraft.util.Uuids;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
import java.util.UUID;

public class Head {
public final String name;
Expand All @@ -33,7 +36,7 @@ public Head(@Nullable String name, UUID uuid, String value, @Nullable String tag
}

public Head(String name, UUID uuid, String value) {
this(name, uuid, value,null);
this(name, uuid, value, null);
}

public Head(UUID uuid, String value) {
Expand All @@ -45,41 +48,6 @@ public String getTagsOrEmpty() {
}

public ItemStack of() {
ItemStack ret = new ItemStack(Items.PLAYER_HEAD);
if (name != null) {
ret.set(DataComponentTypes.CUSTOM_NAME,Text.literal(name).styled(style -> style.withItalic(false)));
}

if (tags != null) {
NbtCompound displayTag = new NbtCompound();
NbtList loreTag = new NbtList();
loreTag.add(NbtString.of(Text.Serialization.toJsonString(Text.literal(tags), RegistryUtil.getDefaultWrapperLookup())));

displayTag.put("Lore", loreTag);
ret.apply(DataComponentTypes.CUSTOM_DATA,NbtComponent.DEFAULT, comp -> comp.apply(cur -> {
cur.put("display", displayTag);
}));
}



NbtCompound ownerTag = new NbtCompound();
ownerTag.putUuid("Id", uuid);

NbtCompound propertiesTag = new NbtCompound();
NbtList texturesTag = new NbtList();

NbtCompound textureValue = new NbtCompound();
textureValue.putString("Value", value);
texturesTag.add(textureValue);

propertiesTag.put("textures", texturesTag);
ownerTag.put("Properties", propertiesTag);

ret.apply(DataComponentTypes.CUSTOM_DATA,NbtComponent.DEFAULT, comp -> comp.apply(cur -> {
cur.put("SkullOwner", ownerTag);
}));

return ret;
return new GuiElementBuilder().setItem(Items.PLAYER_HEAD).setName(Text.literal(name)).setSkullOwner(value, null, uuid).asStack();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.github.sakurawald.util.GuiUtil;
import io.github.sakurawald.util.MessageUtil;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.screen.ScreenHandlerType;
Expand Down Expand Up @@ -54,11 +55,8 @@ private void updateNavigation() {
navigationLayer.setSlot(i, Items.PINK_STAINED_GLASS_PANE.getDefaultStack());
}

ItemStack prevItemStack = new Head(UUID.fromString("8aa062dc-9852-42b1-ae37-b2f8a3121c0e"),
GuiUtil.PREVIOUS_PAGE_ICON).of();
prevItemStack.set(DataComponentTypes.CUSTOM_NAME, MessageUtil.ofVomponent(parent.getPlayer(), "previous_page"));
navigationLayer.setSlot(
3, prevItemStack,
3, new GuiElementBuilder().setItem(Items.PLAYER_HEAD).setName(MessageUtil.ofVomponent(parent.getPlayer(), "previous_page")).setSkullOwner(GuiUtil.PREVIOUS_PAGE_ICON).asStack(),
((index, type, action) -> {
this.page -= 1;
if (this.page < 0) {
Expand All @@ -69,10 +67,8 @@ private void updateNavigation() {
})
);

ItemStack nextItemStack = new Head(UUID.fromString("8aa062dc-9852-42b1-ae37-b2f8a3121c0e"), GuiUtil.NEXT_PAGE_ICON).of();
nextItemStack.set(DataComponentTypes.CUSTOM_NAME, MessageUtil.ofVomponent(parent.getPlayer(), "next_page"));
navigationLayer.setSlot(
5, nextItemStack,
5, new GuiElementBuilder().setItem(Items.PLAYER_HEAD).setName(MessageUtil.ofVomponent(parent.getPlayer(), "next_page")).setSkullOwner(GuiUtil.NEXT_PAGE_ICON).asStack(),
((index, type, action) -> {
this.page += 1;
if (this.page >= getMaxPage()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
package io.github.sakurawald.module.initializer.head.gui;

import com.mojang.authlib.GameProfile;
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
import com.mojang.authlib.minecraft.MinecraftProfileTextures;
import com.mojang.authlib.minecraft.MinecraftSessionService;
import com.mojang.authlib.yggdrasil.ProfileResult;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.AnvilInputGui;
import io.github.sakurawald.Fuji;
import io.github.sakurawald.config.Configs;
import io.github.sakurawald.module.ModuleManager;
import io.github.sakurawald.module.initializer.head.HeadModule;
import io.github.sakurawald.util.MessageUtil;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.server.MinecraftServer;
import net.minecraft.text.Text;
import net.minecraft.util.UserCache;
import net.minecraft.util.Uuids;
import org.apache.logging.log4j.core.util.UuidUtil;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

class PlayerInputGui extends AnvilInputGui {
final HeadModule module = ModuleManager.getInitializer(HeadModule.class);
private final HeadGui parentGui;
private final ItemStack outputStack = Items.PLAYER_HEAD.getDefaultStack();
private long apiDebounce = 0;
private final ItemStack DEFAULT_PLAYER_HEAD = Items.PLAYER_HEAD.getDefaultStack();

public PlayerInputGui(HeadGui parentGui) {
super(parentGui.player, false);
this.parentGui = parentGui;
this.setDefaultInputValue("");
this.setSlot(1, Items.PLAYER_HEAD.getDefaultStack());
this.setSlot(2, outputStack);
this.resetSearchResult();
this.setTitle(MessageUtil.ofVomponent(player, "head.category.player"));
}

private void resetSearchResult() {
this.setSlot(2, this.DEFAULT_PLAYER_HEAD);
}

@Override
public void onTick() {
if (apiDebounce != 0 && apiDebounce <= System.currentTimeMillis()) {
Expand All @@ -50,78 +50,49 @@ public void onTick() {
MinecraftServer server = player.server;
UserCache profileCache = server.getUserCache();
if (profileCache == null) {
outputStack.apply(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT, comp -> comp.apply(cur -> {
cur.remove("SkullOwner");
})
);
resetSearchResult();
return;
}

Optional<GameProfile> possibleProfile = profileCache.findByName(this.getInput());
MinecraftSessionService sessionService = server.getSessionService();
if (possibleProfile.isEmpty()) {
outputStack.apply(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT, comp -> comp.apply(cur -> {
cur.remove("SkullOwner");
})
);
resetSearchResult();
return;
}

ProfileResult profileResult = sessionService.fetchProfile(possibleProfile.get().getId(), false);
if (profileResult == null) {
outputStack.apply(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT, comp -> comp.apply(cur -> {
cur.remove("SkullOwner");
})
);
resetSearchResult();
return;
}

GameProfile profile = profileResult.profile();

MinecraftProfileTextures textures = sessionService.getTextures(profile);
if (textures == MinecraftProfileTextures.EMPTY) {
outputStack.apply(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT, comp -> comp.apply(cur -> {
cur.remove("SkullOwner");
})
);
resetSearchResult();
return;
}

MinecraftProfileTexture texture = textures.skin();

NbtCompound ownerTag = new NbtCompound();
ownerTag.putUuid("Id", profile.getId());
ownerTag.putString("Name", profile.getName());

NbtCompound propertiesTag = new NbtCompound();
NbtList texturesTag = new NbtList();
NbtCompound textureValue = new NbtCompound();

textureValue.putString("Value", new String(Base64.getEncoder().encode(String.format("{\"textures\":{\"SKIN\":{\"url\":\"%s\"}}}", texture.getUrl()).getBytes()), StandardCharsets.UTF_8));

texturesTag.add(textureValue);
propertiesTag.put("textures", texturesTag);
ownerTag.put("Properties", propertiesTag);

outputStack.apply(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT, comp -> comp.apply(cur -> {
cur.put("SkullOwner", ownerTag);
}));

var builder = GuiElementBuilder.from(outputStack);
GuiElementBuilder builder = new GuiElementBuilder().setItem(Items.PLAYER_HEAD);
if (Configs.headHandler.model().economyType != HeadModule.EconomyType.FREE) {
builder.addLoreLine(Text.empty());
builder.addLoreLine(MessageUtil.ofVomponent(player, "head.price").copy().append(module.getCost()));
}

this.setSlot(2, builder.asStack(), (index, type, action, gui) ->
builder.setSkullOwner(profile, Fuji.SERVER);

ItemStack stack = builder.asStack();

this.setSlot(2, stack, (index, type, action, gui) ->
module.tryPurchase(player, 1, () -> {
var cursorStack = getPlayer().currentScreenHandler.getCursorStack();
if (player.currentScreenHandler.getCursorStack().isEmpty()) {
player.currentScreenHandler.setCursorStack(outputStack.copy());
} else if (ItemStack.areItemsAndComponentsEqual(outputStack, cursorStack) && cursorStack.getCount() < cursorStack.getMaxCount()) {
player.currentScreenHandler.setCursorStack(stack.copy());
} else if (ItemStack.areItemsAndComponentsEqual(stack, cursorStack) && cursorStack.getCount() < cursorStack.getMaxCount()) {
cursorStack.increment(1);
} else {
player.dropItem(outputStack.copy(), false);
player.dropItem(stack.copy(), false);
}
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.world.World;


Expand Down Expand Up @@ -48,6 +50,8 @@ private static int clearChat(CommandContext<ServerCommandSource> ctx) {
}

private static int magic(CommandContext<ServerCommandSource> ctx) {
ServerPlayerEntity player = ctx.getSource().getPlayer();
player.sendMessage(Text.literal(String.valueOf(player.getMainHandStack().getComponents())));

return 1;
}
Expand Down

0 comments on commit 3119166

Please sign in to comment.