Skip to content

Commit

Permalink
Made authenticated items unusable in crafts. Fixes DEV-25
Browse files Browse the repository at this point in the history
  • Loading branch information
PainOchoco committed Sep 8, 2023
1 parent 8a3440d commit a417f44
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.laboulangerie.laboulangeriecore.advancements.AdvancementListeners;
import net.laboulangerie.laboulangeriecore.authenticate.AuthenticateCommand;
import net.laboulangerie.laboulangeriecore.authenticate.AuthenticateListener;
import net.laboulangerie.laboulangeriecore.betonquest.HasHouseCondition;
import net.laboulangerie.laboulangeriecore.betonquest.HousesStockCondition;
import net.laboulangerie.laboulangeriecore.betonquest.KingCondition;
Expand Down Expand Up @@ -241,7 +242,7 @@ private void registerListeners() {
new TabListener(), new NameTagListener(), new ElytraGenRemover(), new SpeedPathListener(),
new TradesHook(), new HouseShop(), new HouseWandListener(), new HouseListener(), new eEggHeadClick(),
new ConversionInv(), miscListener, new AdvancementListeners(), new DragonsListener(),
new TradeOverflowListener());
new TradeOverflowListener(), new AuthenticateListener());

if (getServer().getPluginManager().getPlugin("QuickShop") != null)
getServer().getPluginManager().registerEvents(new ChestShopListener(), this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ public Authenticable(ItemStack item) {
}

public boolean isAuthenticated() {
return item.getItemMeta().getPersistentDataContainer()
return item.getItemMeta() != null && item.getItemMeta().getPersistentDataContainer()
.get(new NamespacedKey(LaBoulangerieCore.PLUGIN, "authority"), PersistentDataType.STRING) != null;
}

/**
* If the value is present but invalid for whatever reason, it will return AuthorityType.NATION
* If the value is present but invalid for whatever reason, it will return AuthorityType.INVALID
*/
public AuthorityType getAuthorityType() {
if (!isAuthenticated()) throw new Error("The item isn't authenticated, can't retrieve authority type!");

String value = getValue();
return value.startsWith("p") ? AuthorityType.PLAYER
: value.startsWith("t") ? AuthorityType.TOWN : AuthorityType.NATION;
: value.startsWith("t") ? AuthorityType.TOWN
: value.startsWith("n") ? AuthorityType.NATION
: AuthorityType.INVALID;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package net.laboulangerie.laboulangeriecore.authenticate;

import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

public class AuthenticateListener implements Listener {

@EventHandler(priority = EventPriority.MONITOR)
private void onInventoryClickWithAuthenticate(InventoryClickEvent event) {
Inventory inventory = event.getInventory();

int resultSlot = inventory.getSize() - 1;
switch (inventory.getType()) {
case WORKBENCH:
case CRAFTING:
resultSlot = 0;
break;
case CARTOGRAPHY:
case ENCHANTING:
case ANVIL:
break;
default:
return;
}

if (!containsAuthenticate(inventory)
|| !isAuthenticated(event.getCurrentItem()))
return;

inventory.setItem(resultSlot, new ItemStack(Material.AIR));
}

private boolean containsAuthenticate(Inventory inventory) {
ItemStack[] items = inventory.getContents();
boolean containsAuthenticated = false;

for (int i = 0; i < items.length; i++) {
if (isAuthenticated(items[i])) {
containsAuthenticated = true;
break;
}
}
return containsAuthenticated;
}

private boolean isAuthenticated(ItemStack item) {
return item != null && new Authenticable(item).isAuthenticated();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package net.laboulangerie.laboulangeriecore.authenticate;

public enum AuthorityType {
PLAYER("p", "joueur"), TOWN("t", "ville"), NATION("n", "nation");
PLAYER("p", "joueur"),
TOWN("t", "ville"),
NATION("n", "nation"),
INVALID(null, null);

private String prefix;
private String suffix;
Expand Down

0 comments on commit a417f44

Please sign in to comment.