Added support for talismans in crafting recipes

This commit is contained in:
Auxilor
2021-01-02 16:42:52 +00:00
parent 21025e455c
commit e2c0f554d9
3 changed files with 111 additions and 4 deletions

View File

@@ -88,6 +88,24 @@ public abstract class Talisman implements Listener, Watcher {
@Getter
private final TalismanConfig config;
/**
* The talisman item.
*/
@Getter
private ItemStack itemStack;
/**
* The talisman recipe.
*/
@Getter
private ShapedRecipe recipe = null;
/**
* The talisman recipe overlay.
*/
@Getter
private final Talisman[] recipeTalismanOverlay = new Talisman[9];
/**
* The base64 skull texture.
*/
@@ -174,12 +192,13 @@ public abstract class Talisman implements Listener, Watcher {
PersistentDataContainer container = outMeta.getPersistentDataContainer();
container.set(this.getKey(), PersistentDataType.INTEGER, 1);
out.setItemMeta(outMeta);
TalismanDisplay.displayTalisman(out);
this.itemStack = out;
Bukkit.getServer().removeRecipe(this.getKey());
if (this.isEnabled()) {
TalismanDisplay.displayTalisman(out);
ShapedRecipe recipe = new ShapedRecipe(this.getKey(), out);
List<String> recipeStrings = this.getConfig().getStrings(Talismans.OBTAINING_LOCATION + "recipe");
@@ -187,9 +206,20 @@ public abstract class Talisman implements Listener, Watcher {
recipe.shape("012", "345", "678");
for (int i = 0; i < 9; i++) {
recipe.setIngredient(String.valueOf(i).toCharArray()[0], Material.valueOf(recipeStrings.get(i).toUpperCase()));
char ingredientChar = String.valueOf(i).toCharArray()[0];
Material material;
if (recipeStrings.get(i).startsWith("talisman:")) {
material = Material.PLAYER_HEAD;
String talismanKey = recipeStrings.get(i).split(":")[1];
NamespacedKey talismanNamespacedKey = new NamespacedKey(this.getPlugin(), talismanKey);
recipeTalismanOverlay[i] = Talismans.getByKey(talismanNamespacedKey);
} else {
material = Material.valueOf(recipeStrings.get(i).toUpperCase());
}
recipe.setIngredient(ingredientChar, material);
}
this.recipe = recipe;
Bukkit.getServer().addRecipe(recipe);
}

View File

@@ -12,6 +12,8 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
public class TalismanCraftListener implements Listener {
@EventHandler
public void onCraft(@NotNull final PrepareItemCraftEvent event) {
@@ -63,4 +65,79 @@ public class TalismanCraftListener implements Listener {
event.setCancelled(true);
}
}
@EventHandler
public void prepareCraftTalismanListener(@NotNull final PrepareItemCraftEvent event) {
if (!(event.getRecipe() instanceof ShapedRecipe)) {
return;
}
ShapedRecipe recipe = (ShapedRecipe) event.getRecipe();
Talisman talisman = Talismans.getByKey(recipe.getKey());
if (talisman == null) {
return;
}
if (event.getViewers().isEmpty()) {
return;
}
for (int i = 0; i < 9; i++) {
ItemStack itemStack = event.getInventory().getMatrix()[i];
if (itemStack == null) {
continue;
}
if (itemStack.getType() != Material.PLAYER_HEAD) {
continue;
}
Talisman matchedTalisman = TalismanChecks.getTalismanOnItem(itemStack);
if (matchedTalisman == null || !Objects.equals(matchedTalisman, talisman.getRecipeTalismanOverlay()[i])) {
event.getInventory().setResult(new ItemStack(Material.AIR));
}
}
}
@EventHandler
public void craftTalismanListener(@NotNull final CraftItemEvent event) {
if (!(event.getRecipe() instanceof ShapedRecipe)) {
return;
}
ShapedRecipe recipe = (ShapedRecipe) event.getRecipe();
Talisman talisman = Talismans.getByKey(recipe.getKey());
if (talisman == null) {
return;
}
if (event.getViewers().isEmpty()) {
return;
}
for (int i = 0; i < 9; i++) {
ItemStack itemStack = event.getInventory().getMatrix()[i];
if (itemStack == null) {
continue;
}
if (itemStack.getType() != Material.PLAYER_HEAD) {
continue;
}
Talisman matchedTalisman = TalismanChecks.getTalismanOnItem(itemStack);
if (matchedTalisman == null || !Objects.equals(matchedTalisman, talisman.getRecipeTalismanOverlay()[i])) {
event.getInventory().setResult(new ItemStack(Material.AIR));
event.setCancelled(true);
}
}
}
}

View File

@@ -1,5 +1,5 @@
messages:
prefix: "&c&lTalismans&r &8» "
prefix: "&c&lTalismans&r &8» &r"
no-permission: "&cYou don't have permission to do this!"
not-player: "&cThis command must be run by a player"
reloaded: "Reloaded!"