Skip to content

Commit

Permalink
Powers will automatically detect if "list" or "tree" is fitting in GU…
Browse files Browse the repository at this point in the history
…I, if no type is specified
  • Loading branch information
Lucraft committed Feb 4, 2024
1 parent 15ec50c commit 06cc0ca
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void drawTooltips(GuiGraphics guiGraphics, int mouseX, int mouseY, int wi
guiGraphics.fill(0, 0, PowersScreen.WINDOW_INSIDE_WIDTH, PowersScreen.WINDOW_INSIDE_HEIGHT, Mth.floor(this.fade * 255.0F) << 24);
guiGraphics.pose().popPose();

if (this.hovered != null) {
if (this.hovered != null && !overlayActive) {
var description = this.hovered.getProperty(Ability.DESCRIPTION);

if (description != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,24 @@ protected void init() {
this.selectedTab = null;

AtomicInteger i = new AtomicInteger();
PowerManager.getPowerHandler(this.minecraft.player).ifPresent(handler -> handler.getPowerHolders().values().forEach(holder -> {
if (!holder.getPower().isHidden() && holder.getAbilities().values().stream().anyMatch(en -> !en.getProperty(Ability.HIDDEN_IN_GUI))) {
if (holder.getPower().getGuiDisplayType() == Power.GuiDisplayType.TREE)
this.tabs.add(TreePowerTab.create(this.minecraft, this, i.getAndIncrement(), holder));
else
this.tabs.add(ListPowerTab.create(this.minecraft, this, i.getAndIncrement(), holder));
}
}));
PowerManager.getPowerHandler(this.minecraft.player).ifPresent(handler -> handler.getPowerHolders()
.values()
.stream()
.sorted(Comparator.comparingInt(holder -> PowerManager.getInstance(false).getPowers().stream().toList().indexOf(holder.getPower())))
.forEach(holder -> {
if (!holder.getPower().isHidden() && holder.getAbilities().values().stream().anyMatch(en -> !en.getProperty(Ability.HIDDEN_IN_GUI))) {
var type = holder.getPower().getGuiDisplayType();

if (type == Power.GuiDisplayType.AUTO) {
type = TreePowerTab.canBeTree(holder) ? Power.GuiDisplayType.TREE : Power.GuiDisplayType.LIST;
}

this.tabs.sort(Comparator.comparingInt(o -> PowerManager.getInstance(false).getPowers().stream().toList().indexOf(o.powerHolder.getPower())));
if (type == Power.GuiDisplayType.TREE)
this.tabs.add(TreePowerTab.create(this.minecraft, this, i.getAndIncrement(), holder));
else
this.tabs.add(ListPowerTab.create(this.minecraft, this, i.getAndIncrement(), holder));
}
}));

if (this.tabs.size() > PowerTabType.MAX_TABS) {
int guiLeft = (this.width - WINDOW_WIDTH) / 2;
Expand All @@ -147,7 +155,7 @@ protected void init() {
}

if (!this.tabs.isEmpty()) {
this.selectedTab = tabs.get(0);
this.selectedTab = this.tabs.get(0);
this.selectedTab.onOpened();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,15 @@ public static TreePowerTab create(Minecraft minecraft, PowersScreen screen, int
return null;
}

public static boolean canBeTree(IPowerHolder holder) {
return holder.getAbilities().values().stream().filter(entry -> !entry.getProperty(Ability.HIDDEN_IN_GUI)).anyMatch(entry -> {
List<AbilityEntry> parents = Ability.findParentsWithinHolder(entry.getConfiguration(), holder);
List<AbilityEntry> children = Ability.findChildrenWithinHolder(entry.getConfiguration(), holder);

return !parents.isEmpty() || !children.isEmpty();
});
}

public void scroll(double dragX, double dragY) {
if (this.maxX - this.minX > PowersScreen.WINDOW_INSIDE_WIDTH) {
this.scrollX = Mth.clamp(this.scrollX + dragX, -(this.maxX - PowersScreen.WINDOW_INSIDE_WIDTH), -this.minX);
Expand Down
5 changes: 3 additions & 2 deletions common/src/main/java/net/threetag/palladium/power/Power.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ public static Power fromJSON(ResourceLocation id, JsonObject json) {
Component name = Component.Serializer.fromJson(json.get("name"));
TextureReference background = GsonUtil.getAsTextureReference(json, "background", null);
TextureReference abilityBarTexture = GsonUtil.getAsTextureReference(json, "ability_bar_texture", null);
GuiDisplayType displayType = GuiDisplayType.getByName(GsonHelper.getAsString(json, "gui_display_type", "list"));
GuiDisplayType displayType = GuiDisplayType.getByName(GsonHelper.getAsString(json, "gui_display_type", "auto"));

if (displayType == null) {
throw new JsonParseException("Unknown gui display type '" + GsonHelper.getAsString(json, "gui_display_type", "list") + "'");
throw new JsonParseException("Unknown gui display type '" + GsonHelper.getAsString(json, "gui_display_type", "list") + "', must be either 'list' or 'tree'");
}

Power power = new Power(id,
Expand Down Expand Up @@ -192,6 +192,7 @@ public static Power fromJSON(ResourceLocation id, JsonObject json) {

public enum GuiDisplayType {

AUTO("auto"),
TREE("tree"),
LIST("list");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "Unlock Test",
"icon": "minecraft:iron_ingot",
"gui_display_type": "tree",
"persistent_data": true,
"abilities": {
"item": {
Expand Down

0 comments on commit 06cc0ca

Please sign in to comment.