Skip to content

Commit

Permalink
Start choose screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Dueris committed Oct 30, 2024
1 parent c0dafe6 commit 058e285
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public Component makeIconPixels(@NotNull BufferedImage image) {
for (int x = 0; x < 8; ++x) {
for (int y = 0; y < 8; ++y) {
int col = iconImage.getRGB(x, y);
if ((new Color(col, true)).getAlpha() < 255) {
if (col == 0) {
icon = icon.append(Component.text("\uf002"));
} else {
icon = icon.append(Component.text(pixels.charAt(y)).color(TextColor.color(col)));
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package io.github.dueris.originspaper.screen;

import io.github.dueris.originspaper.component.OriginComponent;
import io.github.dueris.originspaper.origin.*;
import io.github.dueris.originspaper.util.LoopingLinkedObjectList;
import io.papermc.paper.adventure.PaperAdventure;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.component.ResolvableProfile;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.craftbukkit.inventory.CraftInventory;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.IntStream;

public class ChooseOriginScreen {
public static final List<ServerPlayer> CURRENTLY_RENDERING = new LinkedList<>();
protected static final ItemStack NEXT_STACK;
protected static final ItemStack BACK_STACK;

static {
NEXT_STACK = new ItemStack(Items.ARROW);
NEXT_STACK.set(DataComponents.CUSTOM_NAME, Component.literal("Next Origin"));

BACK_STACK = new ItemStack(Items.ARROW);
BACK_STACK.set(DataComponents.CUSTOM_NAME, Component.literal("Previous Origin"));
}

private final List<OriginLayer> layerList;
private final ServerPlayer holder;
private final LoopingLinkedObjectList<Origin> originSelection;

public ChooseOriginScreen(ServerPlayer holder) {
this.holder = holder;
List<OriginLayer> layers = new ObjectArrayList<>();
OriginComponent component = OriginComponent.ORIGIN.get(holder);

OriginLayerManager.values()
.stream()
.filter(OriginLayer::isEnabled)
.filter(Predicate.not(component::hasOrigin))
.forEach(layers::add);

Collections.sort(layers);
this.layerList = layers;
this.originSelection = new LoopingLinkedObjectList<>();

OriginLayer currentLayer = getCurrentLayer();
for (ResourceLocation originId : Objects.requireNonNull(currentLayer, "Current layers are null!").getOrigins(this.holder)) {

Origin origin = OriginManager.get(originId);
if (!origin.isChoosable()) {
continue;
}

ItemStack iconStack = origin.getDisplayItem();
if (iconStack.is(Items.PLAYER_HEAD)) {
iconStack.set(DataComponents.PROFILE, new ResolvableProfile(holder.getGameProfile()));
}

originSelection.add(origin);
}

originSelection.sort(Comparator.comparingInt((Origin o) -> o.getImpact().getImpactValue()).thenComparingInt(Origin::getOrder));
openSelection();
}

private void openSelection() {
// Build and open base
CraftInventory menu = buildBaseMenu();
CraftPlayer player = holder.getBukkitEntity();
player.openInventory(menu);

CURRENTLY_RENDERING.add(holder);

ItemStack[] builtContents = updateContents();
menu.setContents(Arrays.stream(builtContents)
.map(ItemStack::asBukkitCopy)
.distinct()
.toArray(org.bukkit.inventory.ItemStack[]::new)
);
}

private ItemStack @NotNull [] updateContents() {
ItemStack[] built = new ItemStack[53];

for (int i = 0; i <= 53; i++) {
if ((i == 0 || i == 1 || i == 2) ||
(i == 6 || i == 7 || i == 8)) {
built[i] = impact(i);
continue;
}

if (i == 45) {

}

built[i] = ItemStack.EMPTY;
}

return built;
}

private @NotNull ItemStack impact(int index) {
Impact impact = getCurrentOrigin().getImpact();
ItemStack impactItem = switch (impact) {
case LOW -> onlyIndexOf(new ItemStack(Items.GREEN_STAINED_GLASS_PANE), index, 0, 8);
case MEDIUM -> onlyIndexOf(new ItemStack(Items.YELLOW_STAINED_GLASS_PANE), index, 0, 1, 7, 8);
case HIGH -> new ItemStack(Items.RED_STAINED_GLASS_PANE);
case NONE -> new ItemStack(Items.GRAY_STAINED_GLASS_PANE);
};
impactItem.set(DataComponents.CUSTOM_NAME, impact.getTextComponent());
return impactItem;
}

private ItemStack onlyIndexOf(ItemStack ifTrue, int index, int... indexes) {
return IntStream.of(indexes).anyMatch(i -> i == index) ? ifTrue : ItemStack.EMPTY;
}

private @NotNull CraftInventory buildBaseMenu() {
return (CraftInventory) MinecraftServer.getServer().server.createInventory(this.holder.getBukkitEntity(), 54,
PaperAdventure.asAdventure(this.originSelection.getCurrent().getName()));
}

@Nullable
public OriginLayer getCurrentLayer() {
return layerList.getFirst();
}

@NotNull
public Origin getCurrentOrigin() {
return originSelection.getCurrent();
}
}
Loading

0 comments on commit 058e285

Please sign in to comment.