Skip to content

Commit

Permalink
Fixes BlobEditors not processing return button
Browse files Browse the repository at this point in the history
Method overloading inside BlobLibInventoryAPI whenever constructing custom inventories
Fixes MessageManager#getMessage with locale parameter
ObjectDirector correctly applies the TranslatableBlock BlobLib.Remove-Element whenever attempting to remove an element
Improves ManagerDirector#detachAsset allowing custom output directory
Cleaned up ManagerDirector register assets, now allowing registering multiple at the same time
  • Loading branch information
anjoismysign committed Oct 10, 2023
1 parent 6e160ed commit 783ca24
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 162 deletions.
2 changes: 1 addition & 1 deletion ci-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.697.4</version>
<version>1.697.6</version>
<relativePath>pom.xml</relativePath>
</parent>
<artifactId>bloblib</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion local-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.697.4</version>
<version>1.697.6</version>
<relativePath>pom.xml</relativePath>
</parent>
<artifactId>bloblib</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.697.4</version>
<version>1.697.6</version>
<packaging>pom</packaging>

<properties>
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/us/mytheria/bloblib/api/BlobLibInventoryAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,38 @@ public MetaBlobInventory buildMetaInventory(@NotNull String key,
return buildMetaInventory(key, player.getLocale());
}

/**
* Will make Player to select from a list of elements.
* The selector will be placed in the inventory at the specified buttonRangeKey.
* The inventory will open automatically.
* Clicking the return button will close the selector inventory.
*
* @param blobInventoryKey the key of the BlobInventory
* @param player the player
* @param buttonRangeKey the button from the BlobInventory which contains the slots (per page) into which the selector will place the elements
* @param dataType the data type of the selector
* @param selectorList the list of elements to select from
* @param onSelect what's consumed when an element is selected
* @param display the function to display an element, needs to return the ItemStack to display
* @param <T> the type of the selector
* @return the selector
*/
public <T> BlobSelector<T> customSelector(@NotNull String blobInventoryKey,
@NotNull Player player,
@NotNull String buttonRangeKey,
@NotNull String dataType,
@NotNull Supplier<List<T>> selectorList,
@NotNull Consumer<T> onSelect,
@Nullable Function<T, ItemStack> display) {
return customSelector(blobInventoryKey,
player,
buttonRangeKey,
dataType,
selectorList,
onSelect,
display, null);
}

/**
* Will make Player to select from a list of elements.
* The selector will be placed in the inventory at the specified buttonRangeKey.
Expand Down Expand Up @@ -404,6 +436,48 @@ public <T> BlobSelector<T> selector(@NotNull Player player,
display, null);
}

/**
* Will allow player to edit a collection of elements.
* The editor will be placed in the inventory at the specified buttonRangeKey.
* The inventory will open automatically.
* Clicking the return button will close the editor inventory.
*
* @param blobInventoryKey the key of the BlobInventory
* @param player the player
* @param buttonRangeKey the button from the BlobInventory which contains the slots (per page) into which the editor will place the elements
* @param dataType the data type of the editor
* @param addCollection the collection of elements to add to
* @param onAdd what's consumed when an element is added
* @param addDisplay the function to display an element, needs to return the ItemStack to display
* @param viewCollection the collection of elements to view
* @param removeDisplay the function to display an element, needs to return the ItemStack to display
* @param onRemove what's consumed when an element is removed
* @param <T> the type of the editor
* @return the editor
*/
@Nullable
public <T> BlobEditor<T> customEditor(@NotNull String blobInventoryKey,
@NotNull Player player,
@NotNull String buttonRangeKey,
@NotNull String dataType,
@NotNull Supplier<Collection<T>> addCollection,
@NotNull Consumer<T> onAdd,
@Nullable Function<T, ItemStack> addDisplay,
@NotNull Supplier<Collection<T>> viewCollection,
@NotNull Function<T, ItemStack> removeDisplay,
@NotNull Consumer<T> onRemove) {
return customEditor(blobInventoryKey,
player,
buttonRangeKey,
dataType,
addCollection,
onAdd,
addDisplay,
viewCollection,
removeDisplay,
onRemove, null);
}

/**
* Will allow player to edit a collection of elements.
* The editor will be placed in the inventory at the specified buttonRangeKey.
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/us/mytheria/bloblib/entities/BlobFileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,27 @@ public void addFile(String key, File file) {
* @return if it's a fresh file / was just created.
*/
public boolean updateYAML(File file) {
return updateYAML(file, null);
}

/**
* It will auto update it with the most recent
* version that's embedded in the plugin jar.
*
* @param file the YAML file to update
* @param path the path to the embedded file
* @return if it's a fresh file / was just created.
*/
public boolean updateYAML(File file, String path) {
if (path == null)
path = file.getName();
String fileName = FilenameUtils.removeExtension(file.getName());
file.getParentFile().mkdirs();
try {
boolean isFresh = file.createNewFile();
ResourceUtil.updateYml(file.getParentFile(),
"/temp" + fileName + ".yml",
fileName + ".yml", file, getPlugin());
path, file, getPlugin());
return isFresh;
} catch (IOException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package us.mytheria.bloblib.entities;

import java.io.File;

public record FileDetachment(File file, boolean isFresh) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import us.mytheria.bloblib.api.BlobLibMessageAPI;
import us.mytheria.bloblib.api.BlobLibTranslatableAPI;
import us.mytheria.bloblib.entities.inventory.ObjectBuilder;
import us.mytheria.bloblib.itemstack.ItemStackBuilder;
import us.mytheria.bloblib.managers.Manager;
Expand Down Expand Up @@ -369,7 +370,8 @@ public void removeObject(Player player) {
String key = object.getKey();
ItemStackBuilder builder = ItemStackBuilder.build(Material.COMMAND_BLOCK);
builder.displayName(key);
builder.lore();
builder.lore(BlobLibTranslatableAPI.getInstance()
.getTranslatableBlock("BlobLib.Remove-Element", player).get());
if (ItemStack.class.isInstance(object.getClass())) {
ItemStack itemStack = (ItemStack) object;
builder.displayName(ItemStackUtil.display(itemStack));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class InventoryDataRegistry<T extends InventoryButton> {
@NotNull
private final String defaultLocale, key;
private final Map<String, InventoryBuilderCarrier<T>> carriers;
private final Map<String, Consumer<InventoryClickEvent>> clickEvents;
private final Map<String, Consumer<InventoryClickEvent>> buttonClickEvents;

/**
* Will instantiate a new InventoryDataRegistry with the specified default locale.
Expand All @@ -39,7 +39,7 @@ private InventoryDataRegistry(@NotNull String defaultLocale, @NotNull String key
this.defaultLocale = defaultLocale;
this.key = key;
this.carriers = new HashMap<>();
this.clickEvents = new HashMap<>();
this.buttonClickEvents = new HashMap<>();
}

/**
Expand All @@ -51,9 +51,9 @@ private InventoryDataRegistry(@NotNull String defaultLocale, @NotNull String key
public boolean process(@NotNull InventoryBuilderCarrier<T> carrier) {
Objects.requireNonNull(carrier, "carrier cannot be null");
String locale = carrier.locale();
if (this.carriers.containsKey(locale))
if (carriers.containsKey(locale))
return false;
this.carriers.put(locale, carrier);
carriers.put(locale, carrier);
return true;
}

Expand Down Expand Up @@ -93,7 +93,7 @@ public String getKey() {
* @param event the click event
*/
public void onClick(String button, Consumer<InventoryClickEvent> event) {
this.clickEvents.put(button, event);
this.buttonClickEvents.put(button, event);
}

/**
Expand All @@ -103,7 +103,7 @@ public void onClick(String button, Consumer<InventoryClickEvent> event) {
* @param event the click event
*/
public void processClickEvent(String button, InventoryClickEvent event) {
Consumer<InventoryClickEvent> clickEvent = this.clickEvents.get(button);
Consumer<InventoryClickEvent> clickEvent = this.buttonClickEvents.get(button);
if (clickEvent == null)
return;
clickEvent.accept(event);
Expand Down
Loading

0 comments on commit 783ca24

Please sign in to comment.