Skip to content

Commit

Permalink
* Change AnvilMechanics
Browse files Browse the repository at this point in the history
* Change old usage of file extension to new panther derived.
* Added default configurable loading methods to file manager.
* Getting ready for 1.20.4
  • Loading branch information
Hempfest committed Feb 4, 2024
1 parent 0f8034e commit c6a1f25
Show file tree
Hide file tree
Showing 23 changed files with 249 additions and 128 deletions.
2 changes: 1 addition & 1 deletion labyrinth-bukkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>labyrinth</artifactId>
<groupId>com.github.the-h-team</groupId>
<version>1.9.4</version>
<version>1.9.4-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion labyrinth-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>labyrinth</artifactId>
<groupId>com.github.the-h-team</groupId>
<version>1.9.4</version>
<version>1.9.4-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void copyYML(String ymlName, File file) {
* @throws IllegalArgumentException if name is empty
*/
public @NotNull FileManager get(@NotNull final String name, @Nullable final String desc) throws IllegalArgumentException {
return get(name, desc, YamlExtension.INSTANCE);
return get(name, desc, Configurable.Type.YAML);
}

/**
Expand Down Expand Up @@ -218,7 +218,7 @@ public void copyYML(String ymlName, File file) {
*/

public boolean exists(@NotNull final String name, @Nullable final String desc) {
return exists(name, desc, YamlExtension.INSTANCE);
return exists(name, desc, Configurable.Type.YAML);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.sanctum.panther.annotation.Note;
import com.github.sanctum.panther.file.Configurable;
import com.github.sanctum.panther.file.JsonConfiguration;
import java.io.InputStream;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -149,7 +150,7 @@ public void write(@Note("Provided table gets cleared upon finalization.") DataTa
* @return a new yml file containing all values from this json file.
*/
public @NotNull FileManager toYaml(@NotNull String name, String dir) {
FileManager n = FileList.search(plugin).get(name, dir, YamlExtension.INSTANCE);
FileManager n = FileList.search(plugin).get(name, dir, Configurable.Type.YAML);
Configurable c = getRoot();
if (c instanceof JsonConfiguration) {
n.write(copy(), false);
Expand Down Expand Up @@ -192,6 +193,36 @@ public void write(@Note("Provided table gets cleared upon finalization.") DataTa
return n;
}

/**
* This method assumes the resource being loaded is named and located no differently than this file managers local info and attempts to
* load the data from its source plugin jar.
*
* @return the same file manager instance.
*/
public @NotNull FileManager load() {
if (!getRoot().exists()) {
InputStream mainGrab = plugin.getResource(configuration.getName() + configuration.getType().get());
if (mainGrab == null) throw new IllegalStateException("Unable to load " + '"' + configuration.getName() + configuration.getType().get() + '"' + " from plugin " + plugin.getName());
FileList.copy(mainGrab, getRoot().getParent());
}
return this;
}

/**
* This method attempts to load a resource from the parent plugin under the specified root path.
*
* @param data the directory labeled file to load into this file manager.
* @return the same file manager instance.
*/
public @NotNull FileManager load(@NotNull String data) {
if (!getRoot().exists()) {
InputStream mainGrab = plugin.getResource(data);
if (mainGrab == null) throw new IllegalStateException("Unable to load " + '"' + data + '"' + " from plugin " + plugin.getName());
FileList.copy(mainGrab, getRoot().getParent());
}
return this;
}

/**
* Copy all contents to a datatable.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.sanctum.labyrinth.data;

import com.github.sanctum.panther.file.AbstractYamlConfiguration;
import com.github.sanctum.panther.file.Configurable;
import com.github.sanctum.panther.util.PantherLogger;
import java.io.File;
Expand All @@ -16,7 +17,7 @@
* @author Hempfest
* @version 1.0
*/
public class YamlConfiguration extends Configurable {
public class YamlConfiguration extends AbstractYamlConfiguration {

protected final String n;
protected final String d;
Expand Down Expand Up @@ -288,11 +289,6 @@ public List<Long> getLongList(String key) {
return getConfig().getLongList(key);
}

@Override
public Extension getType() {
return YamlExtension.INSTANCE;
}

public static class Node extends Configurable.Node {

public Node(String key, Configurable configuration) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,32 @@
*/
public interface AnvilMechanics extends Service {

/**
* @return The current anvil mechanics for this version.
*/
static @Note @NotNull AnvilMechanics getInstance() {
return Objects.requireNonNull(Bukkit.getServicesManager().load(AnvilMechanics.class));
}

/**
* Gets the next available NMS container id for the player
*
* @param player The player to getMechanics the next container id of
* @param container The container that a new id is being generated for
* @return The next available NMS container id
*/
int getNextContainerId(Player player, Object container);
@Deprecated()
default int getNextContainerId(Player player, Object container) {
return -1;
}

/**
* Closes the current inventory for the player
*
* @param player The player that needs their current inventory closed
*/
void handleInventoryCloseEvent(Player player);
@Deprecated
default void handleInventoryCloseEvent(Player player) {}

/**
* Sends PacketPlayOutOpenWindow to the player with the container id and window title
Expand All @@ -39,54 +50,63 @@ public interface AnvilMechanics extends Service {
* @param containerId The container id to open
* @param inventoryTitle The title of the inventory to be opened (only works in Minecraft 1.14 and above)
*/
void sendPacketOpenWindow(Player player, int containerId, String inventoryTitle);
@Deprecated
default void sendPacketOpenWindow(Player player, int containerId, String inventoryTitle) {}

/**
* Sends PacketPlayOutCloseWindow to the player with the container id
*
* @param player The player to send the packet to
* @param containerId The container id to close
*/
void sendPacketCloseWindow(Player player, int containerId);
@Deprecated
default void sendPacketCloseWindow(Player player, int containerId) {}

/**
* Sets the NMS player's active container to the default one
*
* @param player The player to set the active container of
*/
void setActiveContainerDefault(Player player);
@Deprecated
default void setActiveContainerDefault(Player player) {}

/**
* Sets the NMS player's active container to the one supplied
*
* @param player The player to set the active container of
* @param container The container to set as active
*/
void setActiveContainer(Player player, Object container);
@Deprecated
default void setActiveContainer(Player player, Object container) {}

/**
* Sets the supplied windowId of the supplied Container
*
* @param container The container to set the windowId of
* @param containerId The new windowId
*/
void setActiveContainerId(Object container, int containerId);
@Deprecated
default void setActiveContainerId(Object container, int containerId) {}

/**
* Adds a slot listener to the supplied container for the player
*
* @param container The container to add the slot listener to
* @param player The player to have as a listener
*/
void addActiveContainerSlotListener(Object container, Player player);
@Deprecated
default void addActiveContainerSlotListener(Object container, Player player){}

/**
* Gets the {@link Inventory} wrapper of the supplied NMS container
*
* @param container The NMS container to getMechanics the {@link Inventory} of
* @return The inventory of the NMS container
*/
Inventory toBukkitInventory(Object container);
@Deprecated
default Inventory toBukkitInventory(Object container) {
return null;
}

/**
* Creates a new ContainerAnvil
Expand All @@ -95,10 +115,59 @@ public interface AnvilMechanics extends Service {
* @param title The title of the anvil inventory
* @return The Container instance
*/
Object newContainerAnvil(Player player, String title);
@Deprecated
default Object newContainerAnvil(Player player, String title) {
return null;
}

/**
* Creates a new ContainerAnvil
*
* @param player The player to get the container of
* @param title The title of the anvil inventory
* @return The Container instance
*/
Container newContainer(@NotNull Player player, @NotNull String title, boolean override);

/**
* Get the players current active container.
*
* @param player the player to use.
* @return the specified player's container or null if none active.
*/
Container getContainer(@NotNull Player player);

/**
* Checks if the current Minecraft version actually supports custom titles
*
* @return The current supported state
*/
default boolean isCustomAllowed() {
return true;
}

interface Container {

/**
* @return the item context within the itemstack on the left-hand side of the anvil container.
*/
String getLeftInput();

/**
* Change to input text of the itemstack to the left-hand side of the anvil container.
*
* @param text the text to use
*/
void setLeftInput(String text);

Inventory getBukkitInventory();

void open(@NotNull Player player);

void close(@NotNull Player player);

void reset(@NotNull Player player);

static @Note @NotNull AnvilMechanics getInstance() {
return Objects.requireNonNull(Bukkit.getServicesManager().load(AnvilMechanics.class));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.sanctum.labyrinth.data.service;

import com.github.sanctum.labyrinth.api.Service;
import com.github.sanctum.panther.annotation.Note;
import java.util.Objects;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.jetbrains.annotations.NotNull;

/**
* Wraps NMS data to be used with creation of simple NPCs
*
* @author Hempfest
* @since 1.0
*/
public interface EntityMechanics extends Service {




static @Note @NotNull EntityMechanics getInstance() {
return Objects.requireNonNull(Bukkit.getServicesManager().load(EntityMechanics.class));
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.github.sanctum.labyrinth.event;

import com.github.sanctum.labyrinth.LabyrinthProvider;
import com.github.sanctum.labyrinth.data.service.LabyrinthOption;
import com.github.sanctum.labyrinth.formatting.string.ColoredString;
import com.github.sanctum.labyrinth.library.CommandUtils;
import com.github.sanctum.labyrinth.library.ListUtils;
import com.github.sanctum.panther.event.Vent;
import java.util.Collection;
Expand All @@ -30,11 +28,9 @@
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerCommandSendEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.server.TabCompleteEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -402,19 +398,6 @@ public void onChat(PlayerCommandPreprocessEvent e) {

}

@EventHandler
public void onCommandHide(PlayerCommandSendEvent e) {
CommandUtils.getVisibilityCalculations().forEach(calculation -> {
String test = calculation.accept(e.getPlayer());
if (test != null) e.getCommands().remove(test);
});
}

@EventHandler
public void onTabInsert(TabCompleteEvent e) {

}

@EventHandler(priority = EventPriority.HIGHEST)
public void onBuild(BlockPlaceEvent e) {
BlockPlace b = new LabyrinthVentCall<>(new BlockPlace(e.getPlayer(), e.getBlock())).run();
Expand Down
2 changes: 1 addition & 1 deletion labyrinth-gui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>labyrinth</artifactId>
<groupId>com.github.the-h-team</groupId>
<version>1.9.4</version>
<version>1.9.4-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public InventoryContainer(Inventory inventory, InventoryProperties properties) {
switch (properties.getFormat()) {
case ANVIL:
AnvilMechanics mechanics = AnvilMechanics.getInstance();
final Object container = mechanics.newContainerAnvil(player, properties.getTitle());
Inventory ne = mechanics.toBukkitInventory(container);
final AnvilMechanics.Container container = mechanics.newContainer(player, properties.getTitle(), false);
Inventory ne = container.getBukkitInventory();
for (int i = 0; i < this.inventory.getSize() + 1; i++) {
ne.setItem(i, this.inventory.getItem(i));
}
Expand Down

0 comments on commit c6a1f25

Please sign in to comment.