Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Started creation of new gui utility basalt * Updated version in pom
- Loading branch information
Showing
15 changed files
with
313 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
labyrinth-gui/src/main/java/com/github/sanctum/labyrinth/gui/basalt/InventoryContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.github.sanctum.labyrinth.gui.basalt; | ||
|
||
import com.github.sanctum.labyrinth.data.service.AnvilMechanics; | ||
import com.github.sanctum.panther.container.PantherEntryMap; | ||
import com.github.sanctum.panther.container.PantherMap; | ||
import java.util.UUID; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.inventory.Inventory; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public abstract class InventoryContainer { | ||
|
||
private final Inventory inventory; | ||
private final InventoryProperties properties; | ||
private final PantherMap<UUID, Inventory> inventoryMap = new PantherEntryMap<>(); | ||
|
||
public InventoryContainer(Inventory inventory, InventoryProperties properties) { | ||
this.inventory = inventory; | ||
this.properties = properties; | ||
} | ||
|
||
public @Nullable Inventory getInventory() { | ||
return inventory; | ||
} | ||
|
||
public @Nullable Inventory getInventory(@NotNull Player player) { | ||
return inventoryMap.get(player.getUniqueId()); | ||
} | ||
|
||
public @Nullable InventorySnapshot newSnapshot() { | ||
if (inventory == null) return null; | ||
return new InventorySnapshot(inventory); | ||
} | ||
|
||
public @Nullable InventorySnapshot newSnapshot(@NotNull Player player) { | ||
if (inventory == null) return null; | ||
return new InventorySnapshot(inventoryMap.computeIfAbsent(player.getUniqueId(), () -> { | ||
// use properties to create copy of menu. | ||
switch (properties.getFormat()) { | ||
case ANVIL: | ||
AnvilMechanics mechanics = AnvilMechanics.getInstance(); | ||
final Object container = mechanics.newContainerAnvil(player, properties.getTitle()); | ||
Inventory ne = mechanics.toBukkitInventory(container); | ||
for (int i = 0; i < this.inventory.getSize() + 1; i++) { | ||
ne.setItem(i, this.inventory.getItem(i)); | ||
} | ||
return ne; | ||
case NORMAL: | ||
case PAGINATED: | ||
Inventory inv = Bukkit.createInventory(null, 54, properties.getTitle()); | ||
for (int i = 0; i < this.inventory.getSize() + 1; i++) { | ||
inv.setItem(i, this.inventory.getItem(i)); | ||
} | ||
return inv; | ||
} | ||
// this should never happen. | ||
return null; | ||
})); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
labyrinth-gui/src/main/java/com/github/sanctum/labyrinth/gui/basalt/InventoryFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.github.sanctum.labyrinth.gui.basalt; | ||
|
||
public enum InventoryFormat { | ||
|
||
ANVIL, | ||
NORMAL, | ||
PAGINATED, | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
labyrinth-gui/src/main/java/com/github/sanctum/labyrinth/gui/basalt/InventoryPane.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.github.sanctum.labyrinth.gui.basalt; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.IntStream; | ||
|
||
public enum InventoryPane { | ||
|
||
/** | ||
* The top bar of slots within the inventory. | ||
*/ | ||
TOP(i -> IntStream.range(0, 9).toArray()), | ||
|
||
/** | ||
* The bottom bar of slots within the inventory. | ||
*/ | ||
BOTTOM(i -> IntStream.range(i - 9, i).toArray()), | ||
|
||
/** | ||
* The middle space of the inventory. | ||
*/ | ||
MIDDLE(i -> { | ||
if (i <= 18) { | ||
return IntStream.range(0, 9).toArray(); | ||
} | ||
return IntStream.range(10, i).filter(n -> n < i - 9 && n % 9 != 0 && n % 9 != 8).toArray(); | ||
}), | ||
|
||
/** | ||
* The left bar of slots within the inventory. | ||
*/ | ||
LEFT(i -> IntStream.iterate(0, n -> n + 9).limit(i / 9).toArray()), | ||
|
||
/** | ||
* The right bar of slots within the inventory. | ||
*/ | ||
RIGHT(i -> IntStream.iterate(8, n -> n + 9).limit(i / 9).toArray()); | ||
|
||
private final Function<Integer, int[]> generatorFunction; | ||
private final Map<Integer, int[]> cache = new HashMap<>(); | ||
|
||
InventoryPane(final Function<Integer, int[]> generatorFunction) { | ||
this.generatorFunction = generatorFunction; | ||
} | ||
|
||
public int[] get(int slots) { | ||
int[] result = cache.computeIfAbsent(slots, generatorFunction); | ||
return Arrays.copyOf(result, result.length); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
labyrinth-gui/src/main/java/com/github/sanctum/labyrinth/gui/basalt/InventoryProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.github.sanctum.labyrinth.gui.basalt; | ||
|
||
public class InventoryProperties { | ||
|
||
private int size; | ||
private String title; | ||
private InventoryFormat type; | ||
|
||
public InventoryProperties(InventoryFormat format, String title) { | ||
this.title = title; | ||
this.type = format; | ||
this.size = 4;// decide size with new enum | ||
} | ||
|
||
public InventoryFormat getFormat() { | ||
return type; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public int getSize() { | ||
return size; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
labyrinth-gui/src/main/java/com/github/sanctum/labyrinth/gui/basalt/InventorySize.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.github.sanctum.labyrinth.gui.basalt; | ||
|
||
import com.github.sanctum.labyrinth.gui.unity.construct.Menu; | ||
|
||
public enum InventorySize { | ||
|
||
/** | ||
* Slots: 9 | ||
*/ | ||
ONE(9), | ||
|
||
/** | ||
* Slots: 18 | ||
*/ | ||
TWO(18), | ||
|
||
/** | ||
* Slots: 27 | ||
*/ | ||
THREE(27), | ||
|
||
/** | ||
* Slots: 36 | ||
*/ | ||
FOUR(36), | ||
|
||
/** | ||
* Slots: 45 | ||
*/ | ||
FIVE(45), | ||
|
||
/** | ||
* Slots: 54 | ||
*/ | ||
SIX(54); | ||
|
||
private final int slots; | ||
|
||
InventorySize(int slots) { | ||
this.slots = slots; | ||
} | ||
|
||
/** | ||
* @return The size of the inventory. | ||
*/ | ||
public int getSize() { | ||
return slots; | ||
} | ||
|
||
public int[] getSlots(InventoryPane layout) { | ||
return layout.get(getSize()); | ||
} | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
labyrinth-gui/src/main/java/com/github/sanctum/labyrinth/gui/basalt/InventorySnapshot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.github.sanctum.labyrinth.gui.basalt; | ||
|
||
import com.github.sanctum.labyrinth.task.TaskScheduler; | ||
import java.util.concurrent.CompletableFuture; | ||
import org.bukkit.inventory.Inventory; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public final class InventorySnapshot { | ||
|
||
private final ItemStack[] contents; | ||
private Inventory parent; | ||
private ItemStack[] modifiedContents; | ||
|
||
public InventorySnapshot(Inventory inventory) { | ||
this(inventory.getContents()); | ||
this.parent = inventory; | ||
} | ||
|
||
public InventorySnapshot(ItemStack[] inventory) { | ||
this.contents = inventory; | ||
this.modifiedContents = new ItemStack[inventory.length]; | ||
for (int i = 0; i < inventory.length + 1; i++) { | ||
modifiedContents[i] = new ItemStack(inventory[i]); | ||
} | ||
} | ||
|
||
public void set(int index, @Nullable ItemStack itemStack) { | ||
modifiedContents[index] = itemStack; | ||
} | ||
|
||
public void set(@Nullable ItemStack[] items) { | ||
if (items == null) { | ||
this.modifiedContents = new ItemStack[this.contents.length]; | ||
} else { | ||
this.modifiedContents = items; | ||
} | ||
} | ||
|
||
public @Nullable ItemStack get(int index) { | ||
return modifiedContents[index]; | ||
} | ||
|
||
public CompletableFuture<Void> reset() { | ||
return CompletableFuture.supplyAsync(() -> { | ||
TaskScheduler.of(() -> { | ||
this.modifiedContents = new ItemStack[this.contents.length]; | ||
for (int i = 0; i < this.contents.length + 1; i++) { | ||
modifiedContents[i] = new ItemStack(this.contents[i]); | ||
} | ||
}).schedule(); | ||
return null; | ||
}); | ||
} | ||
|
||
public CompletableFuture<Void> update() { | ||
return CompletableFuture.supplyAsync(() -> { | ||
TaskScheduler.of(() -> { | ||
for (int i = 0; i < this.modifiedContents.length + 1; i++) { | ||
this.parent.setItem(i, this.modifiedContents[i]); | ||
} | ||
}).schedule(); | ||
return null; | ||
}); | ||
} | ||
|
||
public CompletableFuture<Void> update(@NotNull Inventory inventory) { | ||
return CompletableFuture.supplyAsync(() -> { | ||
TaskScheduler.of(() -> { | ||
for (int i = 0; i < this.modifiedContents.length + 1; i++) { | ||
inventory.setItem(i, this.modifiedContents[i]); | ||
} | ||
}).schedule(); | ||
return null; | ||
}); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
labyrinth-gui/src/main/java/com/github/sanctum/labyrinth/gui/basalt/Menu.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.github.sanctum.labyrinth.gui.basalt; | ||
|
||
public class Menu { | ||
|
||
private final boolean isSharable; | ||
private final InventoryProperties properties; | ||
|
||
public Menu(String title, InventoryFormat format, boolean sharable) { | ||
this.isSharable = sharable; | ||
this.properties = new InventoryProperties(format, title); | ||
} | ||
|
||
public InventoryContainer getContainer() { | ||
if (isSharable) { | ||
// new instance | ||
} | ||
return null; // cached instance | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.