Skip to content

Commit

Permalink
1.6.7
Browse files Browse the repository at this point in the history
- Fixed EventRegister#registerEvent method where for some events it would throw an error (stacked events)
- Added HeadInventoryItem.java which extends InventoryItem.java and basically converts String minecraft value head into head item
- Changed naming of some methods in HeadManager.java (they are split into Base64 methods and Minecraft URL)
- Changed way how they items in inventory are generated (instead of taking the old item, they are generated with fixed lore)
- Removed persistentDataContainer from ItemBuilder.java since from 1.6.6 we are working with item stacks and not custom generated items
- Changed a few things in MySqlDatabases (mostly null safety)
- Added OpMap#putAll method and new constructors (no idea why they weren't before)
- Fixed bug with PlaceholderAPI.java where the custom extension would disappear after /papi restart
- Added MathUtils#convertToRomanNumber method which generates roman number (in String) up to 3999
  • Loading branch information
ThisKarolGajda committed Apr 11, 2023
1 parent 3d36cbb commit d51bc05
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 119 deletions.
9 changes: 0 additions & 9 deletions src/main/java/me/opkarol/opc/OpC.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,4 @@

public class OpC extends OpPlugin {

@Override
public void enable() {

}

@Override
public void disable() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.lang.reflect.ParameterizedType;
import java.sql.ResultSet;
import java.util.Objects;

public abstract class MySqlDatabase<O> {
private final String tableName;
Expand All @@ -31,7 +32,7 @@ public MySqlDatabase(@NotNull Class<O> object) {
this.database = new MySqlConnection(OpAPI.getConfig(), "mysql");
this.objects = reflection.getObjects();
MySqlTable table = new MySqlTable(tableName);
objects.getObjectList().forEach(o -> table.addMySqlVariable(o.getVariable(), o.getAttributes()));
addVariables(table);
this.table = table;
this.counter = new MySqlIdCount(this.database, this.tableName);
}
Expand All @@ -45,7 +46,7 @@ public MySqlDatabase(Configuration configuration) {
this.database = new MySqlConnection(configuration, "mysql");
this.objects = reflection.getObjects();
MySqlTable table = new MySqlTable(tableName);
objects.getObjectList().forEach(o -> table.addMySqlVariable(o.getVariable(), o.getAttributes()));
addVariables(table);
this.table = table;
this.counter = new MySqlIdCount(this.database, this.tableName);
}
Expand All @@ -59,7 +60,7 @@ public MySqlDatabase(String mysql) {
this.database = new MySqlConnection(OpAPI.getConfig(), mysql);
this.objects = reflection.getObjects();
MySqlTable table = new MySqlTable(tableName);
objects.getObjectList().forEach(o -> table.addMySqlVariable(o.getVariable(), o.getAttributes()));
addVariables(table);
this.table = table;
this.counter = new MySqlIdCount(this.database, this.tableName);
}
Expand All @@ -73,7 +74,7 @@ public MySqlDatabase(@NotNull MySqlDatabaseSettings mysql) {
this.database = new MySqlConnection(mysql.getJdbc(), mysql.getUser(), mysql.getPassword());
this.objects = reflection.getObjects();
MySqlTable table = new MySqlTable(tableName);
objects.getObjectList().forEach(o -> table.addMySqlVariable(o.getVariable(), o.getAttributes()));
addVariables(table);
this.table = table;
this.counter = new MySqlIdCount(this.database, this.tableName);
}
Expand All @@ -85,11 +86,17 @@ public MySqlDatabase(Class<O> clazz, @NotNull MySqlDatabaseSettings mysql) {
this.database = new MySqlConnection(mysql.getJdbc(), mysql.getUser(), mysql.getPassword());
this.objects = reflection.getObjects();
MySqlTable table = new MySqlTable(tableName);
objects.getObjectList().forEach(o -> table.addMySqlVariable(o.getVariable(), o.getAttributes()));
addVariables(table);
this.table = table;
this.counter = new MySqlIdCount(this.database, this.tableName);
}

public void addVariables(MySqlTable table) {
objects.getObjectList().stream()
.filter(Objects::nonNull)
.forEach(o -> table.addMySqlVariable(o.getVariable(), o.getAttributes()));
}

@SuppressWarnings("unchecked")
public MySqlDatabase(Configuration configuration, String mysql) {
type = (Class<O>) ((ParameterizedType) this.getClass().getGenericSuperclass())
Expand All @@ -99,7 +106,7 @@ public MySqlDatabase(Configuration configuration, String mysql) {
this.database = new MySqlConnection(configuration, mysql);
this.objects = reflection.getObjects();
MySqlTable table = new MySqlTable(tableName);
objects.getObjectList().forEach(o -> table.addMySqlVariable(o.getVariable(), o.getAttributes()));
addVariables(table);
this.table = table;
this.counter = new MySqlIdCount(this.database, this.tableName);
}
Expand All @@ -112,7 +119,7 @@ public MySqlDatabase(Class<?> clazz, Configuration configuration, String mysql)
this.database = new MySqlConnection(configuration, mysql);
this.objects = reflection.getObjects();
MySqlTable table = new MySqlTable(tableName);
objects.getObjectList().forEach(o -> table.addMySqlVariable(o.getVariable(), o.getAttributes()));
addVariables(table);
this.table = table;
this.counter = new MySqlIdCount(this.database, this.tableName);
}
Expand All @@ -126,7 +133,7 @@ public MySqlDatabase() {
this.database = new MySqlConnection(OpAPI.getConfig(), "mysql");
this.objects = reflection.getObjects();
MySqlTable table = new MySqlTable(tableName);
objects.getObjectList().forEach(o -> table.addMySqlVariable(o.getVariable(), o.getAttributes()));
addVariables(table);
this.table = table;
this.counter = new MySqlIdCount(this.database, this.tableName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ public void load(Function<MySqlResultSet, O> getObjectFromSet) {
try {
while (set.next()) {
O object = getObjectFromSet.apply(new MySqlResultSet(set));
putObject(object);
if (object != null) {
putObject(object);
}
}
} catch (SQLException e) {
e.printStackTrace();
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/me/opkarol/opc/api/event/EventRegister.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@
import java.util.function.Consumer;

public class EventRegister {
private static final Plugin opC = OpAPI.getInstance();
private static final Plugin opc = OpAPI.getInstance();

public static <E extends Event> void registerEvent(Class<E> clazz, Consumer<E> consumer) {
registerEvent(clazz, EventPriority.NORMAL, consumer);
}

public static <E extends Event> void registerEvent(Class<E> clazz, EventPriority priority, Consumer<E> consumer) {
if (canRegister()) {
opC.getServer().getPluginManager()
.registerEvent(clazz, new Listener() {
}, priority,
(l, e) -> consumer.accept((E) e), opC, true);
try {
if (canRegister()) {
opc.getServer().getPluginManager().registerEvent(clazz, new Listener() {
},
priority, (l, e) -> {
if (e.getClass().equals(clazz)) {
consumer.accept((E) e);
}
}, opc, true);
}
} catch (ClassCastException exception) {
exception.printStackTrace();
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/me/opkarol/opc/api/extensions/PlaceholderAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ public static PlaceholderAPI getInstance() {
public @Nullable String onRequest(OfflinePlayer player, @NotNull String params) {
return biFunction.apply(player, params);
}

@Override
public boolean persist() {
return true;
}

@Override
public boolean canRegister() {
return true;
}
}.register();
}
return placeholderAPI;
Expand Down
89 changes: 48 additions & 41 deletions src/main/java/me/opkarol/opc/api/gui/events/InventoryListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,59 +25,54 @@
public class InventoryListener {

public InventoryListener(InventoryCache cache) {
// Disables minecraft`s glitch where you use and consume item while having open inventory
// Disables minecraft`s glitch (maybe it's on purpose) where you use and consume item while having open inventory
EventRegister.registerEvent(PlayerItemConsumeEvent.class, e -> e.setCancelled(e.getPlayer().getOpenInventory().getTopInventory().getHolder() instanceof IHolder));

// Disables inventory bypass where you can collect items which are the same (bypassing normal inventory click event)
EventRegister.registerEvent(InventoryClickEvent.class, e -> e.setCancelled(e.getAction().equals(InventoryAction.COLLECT_TO_CURSOR) && e.getInventory().getHolder() instanceof IHolder));

/*
The first thing this code does is checking if the inventory that was clicked on is not a "holder" using the isNotHolder method.
If it is not a holder, the code immediately returns and does nothing else.
Otherwise, the code gets the unique ID of the player who clicked (e.getWhoClicked().getUniqueId()) and the slot of the item that was clicked (e.getSlot()).
It then looks up the active inventory for that player in a cache using the unique ID, and if it finds one, it stores it in the inventory variable.
Next, the code checks the type of the inventory holder for the inventory using inventory.getInventoryHolder().getType().
Depending on the type, it will do different things.
If the type is DEFAULT, it gets the item in the clicked slot from the default holder's inventory using inventory.getInventoryHolder().getDefaultHolder().getInventory().get(slot),
and if it is present, it creates an OnItemClicked event and passes it to the interact method of the item's InventoryItemEventHolder.
If the event is cancelled, it sets the cancellation of the original event (e) to true as well.
If the type is PAGED, it gets the current page of the paged holder using invHolder.getCurrentPage(),
and then gets the item in the clicked slot from the current page's inventory using invHolder.getInventory().get(page).flatMap(inventoryPage -> inventoryPage.get(slot)).
If the item is present, it creates an OnItemClicked event and passes it to the interact method of the item's InventoryItemEventHolder, and sets the cancellation of the original event (e) to true if the event is cancelled.
It then checks if the item has any data, and if it is a "next" or "previous" button for a paged inventory. If it is a "next" button, it advances to the next page in the paged holder.
If it is a "previous" button, it goes back to the previous page. In either case, it reopens the inventory for the player using inventory.openInventory(e.getWhoClicked()).
*/
// Registers a click event for both PAGED and NOT_PAGED (DEFAULT) inventory type
EventRegister.registerEvent(InventoryClickEvent.class, e -> {
// Checks if the clicked inventory is not a "holder", if it is not, it returns and does nothing
if (isNotHolder(e.getClickedInventory())) {
return;
}

UUID uuid = e.getWhoClicked().getUniqueId();
int slot = e.getSlot();

// Gets the active inventory for the player from a cache using the UUID
Optional<ReplacementInventoryImpl> optional = cache.getActiveInventory(uuid);
if (optional.isEmpty()) {
return;
}

ReplacementInventoryImpl inventory = optional.get();

// Switches on the type of inventory holder for the clicked inventory
switch (inventory.getInventoryHolder().getType()) {
case DEFAULT ->
inventory.getInventoryHolder().getDefaultHolder().getInventory().get(slot).ifPresent(item -> {
OnItemClicked event = new OnItemClicked(inventory.getInventoryHolder(), e.getWhoClicked(), slot, item);
InventoryItemEventHolder eventHolder = item.getItemEventHolder();
if (eventHolder != null) {
eventHolder.interact(event);
}
if (event.isCancelled()) {
e.setCancelled(true);
}
});
case DEFAULT -> {
// If the inventory holder is a DEFAULT type, gets the item in the clicked slot from the default holder's inventory
// and if it is present, creates an OnItemClicked event and passes it to the interact method of the item's InventoryItemEventHolder.
// If the event is cancelled, sets the cancellation of the original event (e) to true as well.
inventory.getInventoryHolder().getDefaultHolder().getInventory().get(slot).ifPresent(item -> {
OnItemClicked event = new OnItemClicked(inventory.getInventoryHolder(), e.getWhoClicked(), slot, item);
InventoryItemEventHolder eventHolder = item.getItemEventHolder();
if (eventHolder != null) {
eventHolder.interact(event);
}
if (event.isCancelled()) {
e.setCancelled(true);
}
});
}
case PAGED -> {
// If the inventory holder is a PAGED type, gets the current page of the paged holder
// and then gets the item in the clicked slot from the current page's inventory.
// If the item is present, it creates an OnItemClicked event and passes it to the interact method of the item's InventoryItemEventHolder,
// and sets the cancellation of the original event (e) to true if the event is cancelled.
// It then checks if the item has any data, and if it is a "next" or "previous" button for a paged inventory.
// If it is a "next" button, it advances to the next page in the paged holder.
// If it is a "previous" button, it goes back to the previous page. In either case, it reopens the inventory for the player.
PagedInventoryHolder invHolder = inventory.getInventoryHolder().getPagedHolder();
int page = invHolder.getCurrentPage();
invHolder.getInventory().get(page)
Expand All @@ -97,7 +92,6 @@ If the event is cancelled, it sets the cancellation of the original event (e) to
return;
}

// Get the player's UUID
UUID uuid = player.getUniqueId();

// Check if there is an active inventory for the player in the cache
Expand All @@ -119,7 +113,6 @@ If the event is cancelled, it sets the cancellation of the original event (e) to
// Get the action to be taken when the inventory is closed
InventoryEventHolder eventHolder = inventory.getInventoryHolder().getActionManager().getCloseAction();
if (eventHolder != null) {
// Perform the action
eventHolder.close(event);
}

Expand All @@ -136,21 +129,35 @@ If the event is cancelled, it sets the cancellation of the original event (e) to
});
}

/**
* Checks if the clicked inventory is a "holder" inventory, meaning that it was created by the inventory framework
*
* @param inventory the clicked inventory
* @return true if the inventory is a "holder" inventory, false otherwise
*/
private boolean isNotHolder(Inventory inventory) {
if (inventory == null) {
return false;
}
return !(inventory.getHolder() instanceof IHolder);
}

private void handlePagedItemClick(@NotNull ReplacementInventoryImpl inventory, @NotNull InventoryClickEvent e, int slot, @NotNull InventoryItem item) {
OnItemClicked event = new OnItemClicked(inventory.getInventoryHolder(), e.getWhoClicked(), slot, item);
/**
* Handles the behavior when an item in a paged inventory is clicked
*
* @param inventory the ReplacementInventoryImpl instance for the active inventory
* @param event the InventoryClickEvent that triggered this method
* @param slot the slot index of the clicked item
* @param item the ItemStack of the clicked item
*/
private void handlePagedItemClick(@NotNull ReplacementInventoryImpl inventory, @NotNull InventoryClickEvent event, int slot, @NotNull InventoryItem item) {
OnItemClicked onItemClicked = new OnItemClicked(inventory.getInventoryHolder(), event.getWhoClicked(), slot, item);
InventoryItemEventHolder eventHolder = item.getItemEventHolder();
if (eventHolder != null) {
eventHolder.interact(event);
eventHolder.interact(onItemClicked);
}
if (event.isCancelled()) {
e.setCancelled(true);
if (onItemClicked.isCancelled()) {
event.setCancelled(true);
}
if (!item.hasAnyData()) {
return;
Expand All @@ -160,6 +167,6 @@ private void handlePagedItemClick(@NotNull ReplacementInventoryImpl inventory, @
} else if (item.isPagedInventoryButtonPrevious()) {
inventory.getInventoryHolder().getPagedHolder().previousPage();
}
inventory.openBestInventory(e.getWhoClicked());
inventory.openBestInventory(event.getWhoClicked());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import me.opkarol.opc.api.gui.events.OnItemClicked;

public abstract class InventoryItemEventHolder {
public abstract class InventoryItemEventHolder implements Cloneable {

public abstract void interact(OnItemClicked event);

public abstract InventoryItemEventHolder clone();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public InventoryItemExtender(Consumer<OnItemClicked> action) {
public void interact(OnItemClicked event) {
action.accept(event);
}

@Override
public InventoryItemEventHolder clone() {
return new InventoryItemExtender(action);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public Inventory generate(int page) {
for (int i = 0; i < getInventorySlots(); i++) {
Optional<InventoryItem> optional = get(i);
if (optional.isPresent()) {
inventory.setItem(i, optional.get());
inventory.setItem(i, optional.get().generate());
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/main/java/me/opkarol/opc/api/gui/items/HeadInventoryItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.opkarol.opc.api.gui.items;

import me.opkarol.opc.api.gui.events.OnItemClicked;
import me.opkarol.opc.api.gui.holder.item.InventoryItemEventHolder;
import me.opkarol.opc.api.tools.HeadManager;

import java.util.function.Consumer;

public class HeadInventoryItem extends InventoryItem {
public HeadInventoryItem(String minecraftValue, InventoryItemEventHolder itemEventHolder) {
super(HeadManager.getHeadFromMinecraftValueUrl(minecraftValue), itemEventHolder);
}

public HeadInventoryItem(String minecraftValue, Consumer<OnItemClicked> itemEventHolder) {
super(HeadManager.getHeadFromMinecraftValueUrl(minecraftValue), itemEventHolder);
}

public HeadInventoryItem(String minecraftValue) {
super(HeadManager.getHeadFromMinecraftValueUrl(minecraftValue));
}
}
10 changes: 9 additions & 1 deletion src/main/java/me/opkarol/opc/api/gui/items/InventoryItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.List;
import java.util.function.Consumer;

public class InventoryItem extends ItemBuilder {
public class InventoryItem extends ItemBuilder implements Cloneable {
private InventoryItemEventHolder itemEventHolder;
private List<InventoryItemSpecialData> specialData = new ArrayList<>();

Expand Down Expand Up @@ -74,4 +74,12 @@ public boolean isPagedInventoryButtonPrevious() {
public boolean hasAnyData() {
return specialData != null && specialData.size() > 0;
}

@Override
public @NotNull InventoryItem clone() {
InventoryItem clone = (InventoryItem) super.clone();
clone.itemEventHolder = this.itemEventHolder != null ? this.itemEventHolder.clone() : null;
clone.specialData = new ArrayList<>(this.specialData);
return clone;
}
}
Loading

0 comments on commit d51bc05

Please sign in to comment.