Switched to proper Crafting-mechanics

master
Ruakij 4 years ago
parent 3d757a6ccf
commit c0917a1ab6

@ -3,7 +3,7 @@ package eu.ruekov.ruakij.LinkedBeaconTeleporters;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.LinkedBeaconTeleporterManager; import eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.LinkedBeaconTeleporterManager;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.listener.OnBlockBreak; import eu.ruekov.ruakij.LinkedBeaconTeleporters.listener.OnBlockBreak;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.listener.OnBlockPlace; import eu.ruekov.ruakij.LinkedBeaconTeleporters.listener.OnBlockPlace;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.listener.OnPrepareItemCraftEvent; import eu.ruekov.ruakij.LinkedBeaconTeleporters.listener.OnCraftItemEvent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
@ -33,7 +33,7 @@ public class Main extends JavaPlugin {
PluginManager pluginManager = Bukkit.getPluginManager(); PluginManager pluginManager = Bukkit.getPluginManager();
pluginManager.registerEvents(new OnBlockBreak(), this); pluginManager.registerEvents(new OnBlockBreak(), this);
pluginManager.registerEvents(new OnBlockPlace(), this); pluginManager.registerEvents(new OnBlockPlace(), this);
pluginManager.registerEvents(new OnPrepareItemCraftEvent(), this); pluginManager.registerEvents(new OnCraftItemEvent(), this);
loadConfigs(); loadConfigs();

@ -17,7 +17,7 @@ public class LinkedBeaconTeleporterItem extends LinkedBeaconTeleporter {
// Create a new LinkedBeaconTeleporter // Create a new LinkedBeaconTeleporter
super(); super();
this.item = Main.lbtManager.getItemStackFromLbtItem(this); this.item = Main.lbtManager.getItemStackFromData(this.teleporterId);
} }
public LinkedBeaconTeleporterItem(String teleporterId, ItemStack item) { public LinkedBeaconTeleporterItem(String teleporterId, ItemStack item) {
super(teleporterId); super(teleporterId);
@ -27,7 +27,7 @@ public class LinkedBeaconTeleporterItem extends LinkedBeaconTeleporter {
public LinkedBeaconTeleporterItem(String teleporterId){ public LinkedBeaconTeleporterItem(String teleporterId){
super(teleporterId); super(teleporterId);
this.item = Main.lbtManager.getItemStackFromLbtItem(this); this.item = Main.lbtManager.getItemStackFromData(teleporterId);
} }
public ItemStack item(){ public ItemStack item(){

@ -2,11 +2,14 @@ package eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.Function; import eu.ruekov.ruakij.LinkedBeaconTeleporters.Function;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.Main; import eu.ruekov.ruakij.LinkedBeaconTeleporters.Main;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapelessRecipe;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import java.util.*; import java.util.*;
@ -31,6 +34,9 @@ public class LinkedBeaconTeleporterManager {
Main.config.getStringList("item.lore") Main.config.getStringList("item.lore")
); );
// Construct & add crafting-receipes
constructAndAddRecipes();
// Load teleporters // Load teleporters
for(String teleporterId : Main.data.getKeys(false)){ for(String teleporterId : Main.data.getKeys(false)){
ConfigurationSection teleportersData = Main.data.getConfigurationSection(teleporterId); ConfigurationSection teleportersData = Main.data.getConfigurationSection(teleporterId);
@ -116,8 +122,29 @@ public class LinkedBeaconTeleporterManager {
return lbtItem; return lbtItem;
} }
void constructAndAddRecipes(){
// Create result-item
ItemStack item = getItemStackFromData("-");
item.setAmount(2);
// Create recipe
ShapelessRecipe recipe = new ShapelessRecipe(
new NamespacedKey(Main.plugin, "linked_beacon_teleporter")
, item);
recipe.addIngredient(2, Material.BEACON);
Bukkit.addRecipe(recipe);
// Empty recipe to reset beacon
ShapelessRecipe recipe2 = new ShapelessRecipe(
new NamespacedKey(Main.plugin, "linked_beacon_teleporter_reset"),
new ItemStack(Material.BEACON)
);
recipe2.addIngredient(1, Material.BEACON);
Bukkit.addRecipe(recipe2);
}
// Conversion-methods // Conversion-methods
public ItemStack getItemStackFromLbtItem(LinkedBeaconTeleporterItem lbtItem){ public ItemStack getItemStackFromData(String teleporterId){
ItemStack item = new ItemStack(Material.BEACON); ItemStack item = new ItemStack(Material.BEACON);
ItemMeta itemMeta = item.getItemMeta(); ItemMeta itemMeta = item.getItemMeta();
@ -126,10 +153,10 @@ public class LinkedBeaconTeleporterManager {
List<String> lore = Main.config.getStringList("item.lore"); List<String> lore = Main.config.getStringList("item.lore");
// Replace parameters // Replace parameters
name = name.replace("%id%", lbtItem.teleporterId); name = name.replace("%id%", teleporterId);
for(int i=0; i<lore.size(); i++){ for(int i=0; i<lore.size(); i++){
lore.set(i, lore.set(i,
lore.get(i).replace("%id%", lbtItem.teleporterId) lore.get(i).replace("%id%", teleporterId)
); );
} }

@ -0,0 +1,27 @@
package eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.listener;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.Main;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.LinkedBeaconTeleporterItem;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
public class OnCraftItemEvent {
public static void onCraftItemEvent(CraftItemEvent e, LinkedBeaconTeleporterItem lbtItem){
// Create new LinkedBeaconTeleporterItem
lbtItem = new LinkedBeaconTeleporterItem();
ItemStack item = lbtItem.item();
// Exactly 2
item.setAmount(2);
// FIXME: Changing the just-crafted-item only works for a single item, bulk-crafting (e.g. Shift-Click) will result in default-items getting crafted!
e.setCurrentItem(item);
}
}

@ -1,36 +0,0 @@
package eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.listener;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.LinkedBeaconTeleporterItem;
import org.bukkit.Material;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.inventory.CraftingInventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
public class OnPrepareItemCraftEvent {
public static void onPrepareItemCraftEvent(PrepareItemCraftEvent e){
ItemStack[] matrix = e.getInventory().getMatrix();
// Check if one of provided items is already LinkedBeaconTeleporterItem and use first if found
LinkedBeaconTeleporterItem lbtItem = null;
/*for (ItemStack item : matrix) {
if(item == null) continue;
lbtItem = LinkedBeaconTeleporterItem.getFromItemStack(item);
if(lbtItem != null) break;
}*/
if(lbtItem == null){
// If none is found, create new LinkedBeaconTeleporterItem and set as crafting-result
lbtItem = new LinkedBeaconTeleporterItem();
}
ItemStack item = lbtItem.item();
// Exactly 2
item.setAmount(2);
// Set as crafting-result
e.getInventory().setResult(item);
}
}

@ -0,0 +1,29 @@
package eu.ruekov.ruakij.LinkedBeaconTeleporters.listener;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.Main;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.LinkedBeaconTeleporterItem;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
public class OnCraftItemEvent implements Listener {
@EventHandler(priority = EventPriority.LOW)
public void onCraftItemEvent(CraftItemEvent e){
Recipe recipe = e.getRecipe();
ItemStack itemResult = recipe.getResult();
// Check if it has LinkedBeaconTeleporter-Data
LinkedBeaconTeleporterItem lbtItem = Main.lbtManager.getLbtItemFromItemStack(itemResult);
if(lbtItem != null){
eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.listener.OnCraftItemEvent.onCraftItemEvent(e, lbtItem);
}
}
}

@ -1,31 +0,0 @@
package eu.ruekov.ruakij.LinkedBeaconTeleporters.listener;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.inventory.CraftingInventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
public class OnPrepareItemCraftEvent implements Listener {
@EventHandler(priority = EventPriority.LOW)
public void onPrepareItemCraftEvent(PrepareItemCraftEvent e){
ItemStack[] matrix = e.getInventory().getMatrix();
// Check if 2 beacons are in the crafting-slots
int beaconCount = 0;
for (ItemStack item : matrix) {
if(item == null) continue;
if(item.getType() == Material.BEACON)
beaconCount++;
}
if(beaconCount == 2){
// Exactly 2 beacons found
eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.listener.OnPrepareItemCraftEvent.onPrepareItemCraftEvent(e);
}
}
}
Loading…
Cancel
Save