Skip to content

Commit

Permalink
No longer depends on Paper, serializes components instead, temporarily.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt committed Jun 23, 2021
1 parent b8832bc commit 8aed1db
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 331 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ subprojects {
}

group = "dev.triumphteam"
version = "3.0.2"
version = "3.0.3"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package dev.triumphteam.gui.builder.gui;

import dev.triumphteam.gui.components.util.Legacy;
import dev.triumphteam.gui.guis.PaginatedGui;
import org.jetbrains.annotations.Contract;

Expand Down Expand Up @@ -55,7 +56,7 @@ public PaginatedBuilder pageSize(final int pageSize) {
@Contract(" -> new")
@Override
public PaginatedGui create() {
final PaginatedGui gui = new PaginatedGui(getRows(), pageSize, getTitle(), getModifiers());
final PaginatedGui gui = new PaginatedGui(getRows(), pageSize, Legacy.SERIALIZER.serialize(getTitle()), getModifiers());

final Consumer<PaginatedGui> consumer = getConsumer();
if (consumer != null) consumer.accept(gui);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package dev.triumphteam.gui.builder.gui;

import dev.triumphteam.gui.components.ScrollType;
import dev.triumphteam.gui.components.util.Legacy;
import dev.triumphteam.gui.guis.ScrollingGui;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.Contract;
Expand Down Expand Up @@ -81,7 +82,7 @@ public ScrollingBuilder pageSize(final int pageSize) {
@Contract(" -> new")
@Override
public ScrollingGui create() {
final ScrollingGui gui = new ScrollingGui(getRows(), pageSize, getTitle(), scrollType, getModifiers());
final ScrollingGui gui = new ScrollingGui(getRows(), pageSize, Legacy.SERIALIZER.serialize(getTitle()), scrollType, getModifiers());

final Consumer<ScrollingGui> consumer = getConsumer();
if (consumer != null) consumer.accept(gui);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* MIT License
*
* <p>
* Copyright (c) 2021 TriumphTeam
*
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -24,6 +24,7 @@
package dev.triumphteam.gui.builder.gui;

import dev.triumphteam.gui.components.GuiType;
import dev.triumphteam.gui.components.util.Legacy;
import dev.triumphteam.gui.guis.Gui;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -68,10 +69,11 @@ public SimpleBuilder type(@NotNull final GuiType guiType) {
@Override
public Gui create() {
final Gui gui;
final String title = Legacy.SERIALIZER.serialize(getTitle());
if (guiType == null || guiType == GuiType.CHEST) {
gui = new Gui(getRows(), getTitle(), getModifiers());
gui = new Gui(getRows(), title, getModifiers());
} else {
gui = new Gui(guiType, getTitle(), getModifiers());
gui = new Gui(guiType, title, getModifiers());
}

final Consumer<Gui> consumer = getConsumer();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* MIT License
*
* <p>
* Copyright (c) 2021 TriumphTeam
*
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -23,6 +23,7 @@
*/
package dev.triumphteam.gui.builder.gui;

import dev.triumphteam.gui.components.util.Legacy;
import dev.triumphteam.gui.guis.StorageGui;
import org.jetbrains.annotations.Contract;

Expand All @@ -41,7 +42,7 @@ public final class StorageBuilder extends BaseGuiBuilder<StorageGui, StorageBuil
@Contract(" -> new")
@Override
public StorageGui create() {
final StorageGui gui = new StorageGui(getRows(), getTitle(), getModifiers());
final StorageGui gui = new StorageGui(getRows(), Legacy.SERIALIZER.serialize(getTitle()), getModifiers());

final Consumer<StorageGui> consumer = getConsumer();
if (consumer != null) consumer.accept(gui);
Expand Down
115 changes: 37 additions & 78 deletions core/src/main/java/dev/triumphteam/gui/guis/BaseGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,13 @@
import dev.triumphteam.gui.components.InteractionModifier;
import dev.triumphteam.gui.components.exception.GuiException;
import dev.triumphteam.gui.components.util.GuiFiller;
import dev.triumphteam.gui.components.util.Legacy;
import dev.triumphteam.gui.components.util.VersionHelper;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -114,34 +110,34 @@ public abstract class BaseGui implements InventoryHolder {
private boolean runOpenAction = true;

/**
* The main constructor, using {@link Component}.
* The main constructor, using {@link String}.
*
* @param rows The amount of rows to use.
* @param title The GUI title using {@link Component}.
* @param rows The amount of rows to use.
* @param title The GUI title using {@link String}.
* @param interactionModifiers Modifiers to select which interactions are allowed.
* @since 3.0.0.
*/
public BaseGui(final int rows, @NotNull final Component title, @NotNull final Set<InteractionModifier> interactionModifiers) {
public BaseGui(final int rows, @NotNull final String title, @NotNull final Set<InteractionModifier> interactionModifiers) {
int finalRows = rows;
if (!(rows >= 1 && rows <= 6)) finalRows = 1;
this.rows = finalRows;
this.interactionModifiers = safeCopyOf(interactionModifiers);

inventory = createRowedInventory(title);
inventory = Bukkit.createInventory(this, this.rows * 9, title);
}

/**
* Alternative constructor that takes {@link GuiType} instead of rows number.
*
* @param guiType The {@link GuiType} to use.
* @param title The GUI title using {@link Component}.
* @param guiType The {@link GuiType} to use.
* @param title The GUI title using {@link String}.
* @param interactionModifiers Modifiers to select which interactions are allowed.
* @since 3.0.0
*/
public BaseGui(@NotNull final GuiType guiType, @NotNull final Component title, @NotNull final Set<InteractionModifier> interactionModifiers) {
public BaseGui(@NotNull final GuiType guiType, @NotNull final String title, @NotNull final Set<InteractionModifier> interactionModifiers) {
this.guiType = guiType;
this.interactionModifiers = safeCopyOf(interactionModifiers);
inventory = createTypedInventory(title);
inventory = Bukkit.createInventory(this, guiType.getInventoryType(), title);
}

/**
Expand All @@ -161,7 +157,7 @@ private EnumSet<InteractionModifier> safeCopyOf(@NotNull final Set<InteractionMo
*
* @param rows The amount of rows the GUI should have.
* @param title The GUI title.
* @deprecated In favor of {@link BaseGui#BaseGui(int, Component, Set)}.
* @deprecated In favor of {@link BaseGui#BaseGui(int, String, Set)}.
*/
@Deprecated
public BaseGui(final int rows, @NotNull final String title) {
Expand All @@ -178,7 +174,7 @@ public BaseGui(final int rows, @NotNull final String title) {
*
* @param guiType The {@link GuiType} to use.
* @param title The GUI title.
* @deprecated In favor of {@link BaseGui#BaseGui(GuiType, Component, Set)}.
* @deprecated In favor of {@link BaseGui#BaseGui(GuiType, String, Set)}.
*/
@Deprecated
public BaseGui(@NotNull final GuiType guiType, @NotNull final String title) {
Expand Down Expand Up @@ -422,6 +418,29 @@ public void update() {
for (HumanEntity viewer : new ArrayList<>(inventory.getViewers())) ((Player) viewer).updateInventory();
}

/**
* Updates the title of the GUI.
* <i>This method may cause LAG if used on a loop</i>.
*
* @param title The title to set.
* @return The GUI for easier use when declaring, works like a builder.
*/
public BaseGui updateTitle(@NotNull final String title) {
updating = true;

final List<HumanEntity> viewers = new ArrayList<>(inventory.getViewers());

inventory = Bukkit.createInventory(this, inventory.getSize(), title);

for (final HumanEntity player : viewers) {
open(player);
}

updating = false;

return this;
}

/**
* Updates the specified item in the GUI at runtime, without creating a new {@link GuiItem}.
*
Expand Down Expand Up @@ -469,54 +488,6 @@ public void updateItem(final int row, final int col, @NotNull final GuiItem item
updateItem(getSlotFromRowCol(row, col), item);
}

/**
* Updates the title of the GUI.
* <i>This method may cause LAG if used on a loop</i>.
*
* @param title The title to set.
* @return The GUI for easier use when declaring, works like a builder.
* @deprecated Should use {@link #updateTitle(Component)} instead.
*/
@Deprecated
public BaseGui updateTitle(@NotNull final String title) {
updating = true;

final List<HumanEntity> viewers = new ArrayList<>(inventory.getViewers());

inventory = Bukkit.createInventory(this, inventory.getSize(), title);

for (final HumanEntity player : viewers) {
open(player);
}

updating = false;

return this;
}

/**
* Updates the title of the GUI.
* <i>I do not recommend using this too often.</i>
*
* @param title The title to set.
* @return The GUI for easier use when declaring, works like a builder.
*/
public BaseGui updateTitle(@NotNull final Component title) {
updating = true;

final List<HumanEntity> viewers = new ArrayList<>(inventory.getViewers());

inventory = createRowedInventory(title);

for (final HumanEntity player : viewers) {
open(player);
}

updating = false;

return this;
}

/**
* Disable item placement inside the GUI.
*
Expand Down Expand Up @@ -805,13 +776,8 @@ public void setInventory(@NotNull final Inventory inventory) {
this.inventory = inventory;
}

/**
* Creates a rowed {@link Inventory}.
* If the server is unfortunately legacy it'll serialize to string using the stupid format.
*
* @param title The title as a {@link Component}.
* @return The new rowed {@link Inventory}.
*/
/*
TODO fix this part, find a better solution for using Paper
protected Inventory createRowedInventory(@NotNull final Component title) {
if (VersionHelper.IS_COMPONENT_LEGACY) {
return Bukkit.createInventory(this, this.rows * 9, Legacy.SERIALIZER.serialize(title));
Expand All @@ -820,21 +786,14 @@ protected Inventory createRowedInventory(@NotNull final Component title) {
return inventory = Bukkit.createInventory(this, this.rows * 9, title);
}
/**
* Creates a typed {@link Inventory}.
* If the server is unfortunately legacy it'll serialize to string using the stupid format.
*
* @param title The title as a {@link Component}.
* @return The new typed {@link Inventory}.
*/
private Inventory createTypedInventory(@NotNull final Component title) {
final InventoryType inventoryType = guiType.getInventoryType();
if (VersionHelper.IS_COMPONENT_LEGACY) {
return Bukkit.createInventory(this, inventoryType, Legacy.SERIALIZER.serialize(title));
}
return Bukkit.createInventory(this, inventoryType, title);
}
}*/

/**
* Checks if the slot introduces is a valid slot.
Expand Down
Loading

0 comments on commit 8aed1db

Please sign in to comment.