Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/main/java/me/zenox/superitems/SuperItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import me.zenox.superitems.data.ConfigLoader;
import me.zenox.superitems.data.LanguageLoader;
import me.zenox.superitems.enchant.EnchantRegistry;
import me.zenox.superitems.events.DimensionLocker;
import me.zenox.superitems.events.InventoryListener;
import me.zenox.superitems.events.OtherEvent;
import me.zenox.superitems.events.PlayerUseItemEvent;
import me.zenox.superitems.events.*;
import me.zenox.superitems.item.ItemRegistry;
import me.zenox.superitems.item.VanillaItem;
import me.zenox.superitems.network.GlowFilter;
Expand Down Expand Up @@ -83,6 +80,7 @@ private void registerListeners() {
new OtherEvent(plugin);
new InventoryListener(plugin);
new DimensionLocker(plugin);
new CraftEvent(plugin);
}

public static void registerGlobalGUIItems(){
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/me/zenox/superitems/abilities/ItemAbility.java
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,8 @@ public void run() {
Location giantLocation = arr.getLocation();
giantLocation.setY(arr.getLocation().getY()-1);
giant.teleport(giantLocation);
giant.setFireTicks(0);
giant.setVisualFire(false);

if(arr.isInBlock() && !contacted){
contacted = true;
Expand Down
157 changes: 157 additions & 0 deletions src/main/java/me/zenox/superitems/events/CraftEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package me.zenox.superitems.events;

import me.zenox.superitems.SuperItems;
import me.zenox.superitems.item.ComplexItem;
import me.zenox.superitems.item.ComplexItemStack;
import me.zenox.superitems.recipe.ComplexChoice;
import me.zenox.superitems.recipe.ComplexRecipe;
import me.zenox.superitems.recipe.ShapedComplexRecipe;
import me.zenox.superitems.recipe.ShapelessComplexRecipe;
import org.bukkit.Bukkit;
import org.bukkit.Keyed;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.inventory.CraftingInventory;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;
import java.util.Map;

public class CraftEvent implements Listener {
private SuperItems plugin;

public CraftEvent(SuperItems plugin){
this.plugin = plugin;
Bukkit.getPluginManager().registerEvents(this, plugin);
}

@EventHandler
public void prepareCraftEvent(PrepareItemCraftEvent e){
if(e.getInventory().getType() != InventoryType.WORKBENCH) return;

// Get the related complex recipe
for (ComplexRecipe recipe : ComplexRecipe.registeredRecipes) {
if (e.getRecipe() != null && recipe.getKey().equals(((Keyed) e.getRecipe()).getKey())){
if(recipe instanceof ShapedComplexRecipe){
// Shaped Logic
if (!testRecipe(recipe, e.getInventory().getMatrix())){
e.getInventory().setResult(new ItemStack(Material.AIR));
for(ComplexRecipe complexRecipe : ComplexRecipe.similarRecipeMap.get(recipe)){
if(testRecipe(complexRecipe, e.getInventory().getMatrix())) {
e.getInventory().setResult(complexRecipe.getResult().getItem());
break;
}
}
}
}
break;
} else if(recipe instanceof ShapelessComplexRecipe) {
if(testRecipe(recipe, e.getInventory().getMatrix())){
e.getInventory().setResult(recipe.getResult().getItem());
}
}
}

}

private boolean testRecipe(ComplexRecipe recipe, ItemStack[] matrix){
if(recipe instanceof ShapedComplexRecipe) {
ShapedComplexRecipe shapedRecipe = (ShapedComplexRecipe) recipe;
int index = 0;
for (String row : shapedRecipe.getShape()) {
for (char c : row.toCharArray()) {
if (!shapedRecipe.getChoiceMap().get(c).test(matrix[index])) {
return false;
}
index++;
}
}
return true;
} else {
ShapelessComplexRecipe shapelessRecipe = (ShapelessComplexRecipe) recipe;
HashMap<ComplexItem, Integer> requiredItems = new HashMap<>();
for (ComplexChoice choice : shapelessRecipe.getChoiceList()) {
requiredItems.computeIfPresent(((Map.Entry<ComplexItem, Integer>) choice.getChoices().entrySet().toArray()[0]).getKey(), ((complexItem, integer) -> integer + choice.getAmount()));
requiredItems.putIfAbsent(((Map.Entry<ComplexItem, Integer>) choice.getChoices().entrySet().toArray()[0]).getKey(), ((Map.Entry<ComplexItem, Integer>) choice.getChoices().entrySet().toArray()[0]).getValue());
}

for (ItemStack item : matrix){
if(item == null) continue;
ComplexItem cItem = ComplexItemStack.of(item).getComplexItem();
if(!requiredItems.containsKey(cItem)) return false;
requiredItems.replace(cItem, requiredItems.get(cItem) - item.getAmount());
}

for (Map.Entry<ComplexItem, Integer> entry :
requiredItems.entrySet()) {
if(entry.getValue() > 0) return false;
}

return true;
}
}

@EventHandler
public void craftEvent(InventoryClickEvent e){
if(e.getClickedInventory() == null) return;
if(e.getClickedInventory().getType() != InventoryType.WORKBENCH) return;
if(e.getSlot() != 0) return;
CraftingInventory inventory = ((CraftingInventory) e.getClickedInventory());
// Check that there is something being crafted - and identify what it is
ComplexRecipe recipe = null;

for (ComplexRecipe recipe1 : ComplexRecipe.registeredRecipes) {
if (testRecipe(recipe1, inventory.getMatrix())){
recipe = recipe1;
break;
}
}

if(recipe == null) return;

if(e.getClick().isShiftClick()){
if(recipe instanceof ShapedComplexRecipe) {
for (int i = 0; i < 9; i++) {
inventory.getMatrix()[i].setAmount(inventory.getMatrix()[i].getAmount() % ((ShapedComplexRecipe) recipe).getChoiceatIndex(i).getItemStack().getAmount());
}
} else {
e.setCancelled(true);
}
} else {
if(recipe instanceof ShapedComplexRecipe) {
for (int i = 0; i < 9; i++) {
inventory.getMatrix()[i].setAmount(inventory.getMatrix()[i].getAmount() - ((ShapedComplexRecipe) recipe).getChoiceatIndex(i).getItemStack().getAmount());
}
} else {
ShapelessComplexRecipe shapelessRecipe = ((ShapelessComplexRecipe) recipe);
HashMap<ComplexItem, Integer> requiredItems = new HashMap<>();
for (ComplexChoice choice : shapelessRecipe.getChoiceList()) {
requiredItems.computeIfPresent(((Map.Entry<ComplexItem, Integer>) choice.getChoices().entrySet().toArray()[0]).getKey(), ((complexItem, integer) -> integer + choice.getAmount()));
requiredItems.putIfAbsent(((Map.Entry<ComplexItem, Integer>) choice.getChoices().entrySet().toArray()[0]).getKey(), ((Map.Entry<ComplexItem, Integer>) choice.getChoices().entrySet().toArray()[0]).getValue());
}

for (int i = 0; i < 9; i++) {
ItemStack item = inventory.getMatrix()[i];
if(item == null) continue;
ComplexItem cItem = ComplexItemStack.of(item).getComplexItem();
if(requiredItems.get(cItem) <= 0) {
// Attempt to stop the items from duplicating
item.setAmount(item.getAmount() + 1);
continue;
}

int amountToSubtract = Math.min(item.getAmount(), requiredItems.get(cItem));
item.setAmount(item.getAmount() - amountToSubtract);

requiredItems.replace(cItem, requiredItems.get(cItem) - amountToSubtract);
}
}
}

}

}
11 changes: 10 additions & 1 deletion src/main/java/me/zenox/superitems/item/ComplexItemMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
*
*/

public class ComplexItemMeta {
public class ComplexItemMeta implements Cloneable{
public static final NamespacedKey ABILITY_ID = new NamespacedKey(SuperItems.getPlugin(), "ability");
public static final String VAR_PREFIX = "var_";
public static final String ATTRIBUTE_BASE_KEY = "base";
Expand Down Expand Up @@ -343,4 +343,13 @@ public List<Ability> getAbilities() {
public List<AttributeModifier> getModifierList() {
return modifierList;
}

@Override
protected ComplexItemMeta clone(){
try {
return ((ComplexItemMeta) super.clone());
} catch (CloneNotSupportedException e) {
throw new RuntimeException("Cloning not supported here! o_O");
}
}
}
9 changes: 8 additions & 1 deletion src/main/java/me/zenox/superitems/item/ComplexItemStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* <p>
* Planned for future implementation
*/
public class ComplexItemStack {
public class ComplexItemStack implements Cloneable{

private final ComplexItem complexItem;
private final UUID uuid;
Expand Down Expand Up @@ -151,4 +151,11 @@ public ComplexItem getComplexItem() {
public ComplexItemMeta getComplexMeta() {
return complexMeta;
}

@Override
public ComplexItemStack clone() {
ComplexItemStack clone = new ComplexItemStack(this.complexItem, this.item.getAmount());
clone.complexMeta = clone.complexMeta.clone();
return clone;
}
}
1 change: 0 additions & 1 deletion src/main/java/me/zenox/superitems/item/ItemRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,6 @@ public static List<Recipe> registerRecipes() {
Bukkit.addRecipe(recipe);
} catch (IllegalStateException e) {
if (recipe instanceof Keyed) {
//Util.logToConsole("Found duplicate recipe, re-adding.");
Bukkit.removeRecipe(((Keyed) recipe).getKey());
Bukkit.addRecipe(recipe);
}
Expand Down
34 changes: 26 additions & 8 deletions src/main/java/me/zenox/superitems/recipe/ComplexChoice.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import com.google.common.collect.ImmutableMap;
import me.zenox.superitems.item.ComplexItem;
import me.zenox.superitems.item.ComplexItemStack;
import me.zenox.superitems.item.ItemRegistry;
import me.zenox.superitems.item.VanillaItem;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.Objects;

public class ComplexChoice implements RecipeChoice {
public class ComplexChoice {

private Map<ComplexItem, Integer> choices;

Expand All @@ -26,6 +26,21 @@ public ComplexChoice(@NotNull ComplexItem complexItem, Integer amount) {
this(Map.of(complexItem, amount));
}

public ComplexChoice(@NotNull Material material, Integer amount) {
this(Map.of(VanillaItem.of(material), amount));
}

public ComplexChoice(@NotNull ComplexItem complexItem) {
this(Map.of(complexItem, 1));
}

public ComplexChoice(@NotNull ComplexItemStack itemStack){
this(Map.of(itemStack.getComplexItem(), itemStack.getItem().getAmount()));
}
public ComplexChoice(@NotNull Material material) {
this(Map.of(VanillaItem.of(material), 1));
}

public ComplexChoice(@NotNull Map<ComplexItem, Integer> choices) {
Preconditions.checkArgument(choices != null, "choices");
Preconditions.checkArgument(!choices.isEmpty(), "Must have at least one choice");
Expand All @@ -37,9 +52,13 @@ public ComplexChoice(@NotNull Map<ComplexItem, Integer> choices) {
}

@NotNull
@Override
public ItemStack getItemStack() {
return ItemRegistry.byId(((Map.Entry<String, Integer>) choices.entrySet().toArray()[0]).getKey()).getItemStack(((Map.Entry<String, Integer>) choices.entrySet().toArray()[0]).getValue());
return new ComplexItemStack(((Map.Entry<ComplexItem, Integer>) choices.entrySet().toArray()[0]).getKey(), ((Map.Entry<ComplexItem, Integer>) choices.entrySet().toArray()[0]).getValue()).getItem();
}

@NotNull
public int getAmount(){
return ((Map.Entry<ComplexItem, Integer>) choices.entrySet().toArray()[0]).getValue();
}

public Map<ComplexItem, Integer> getChoices() {
Expand All @@ -57,14 +76,13 @@ public ComplexChoice clone() {
}
}

@Override
public boolean test(@NotNull ItemStack t) {
ComplexItem cItem = ComplexItemStack.of(t).getComplexItem();
for (Map.Entry<ComplexItem, Integer> match : choices.entrySet()) {
if (ComplexItemStack.of(t).getComplexItem().equals(match) && (t.getAmount() == match.getValue() || match.getValue() == -1)) {
if (cItem.equals(match.getKey()) && (t.getAmount() >= match.getValue() || match.getValue() == -1)) {
return true;
}
}

return false;
}

Expand Down
21 changes: 21 additions & 0 deletions src/main/java/me/zenox/superitems/recipe/ComplexRecipe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.zenox.superitems.recipe;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import me.zenox.superitems.item.ComplexItemStack;
import org.bukkit.NamespacedKey;

import java.util.ArrayList;
import java.util.List;

public interface ComplexRecipe {
List<ComplexRecipe> registeredRecipes = new ArrayList<>();
Multimap<ComplexRecipe, ComplexRecipe> similarRecipeMap = ArrayListMultimap.create();

NamespacedKey getKey();
ComplexItemStack getResult();
ComplexRecipe register();

boolean similar(ComplexRecipe other);

}
Loading