Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merged changes and fixing up a few things

  • Loading branch information...
commit d31a2dd7c5b50e134eeac9f8e13691e77999c1af 2 parents 1b645ac + 061d310
@ricochet1k ricochet1k authored
View
2  src/configs/config.yml → configs/config.yml
@@ -4,4 +4,4 @@ repair-iron: true
repair-stone: true
repair-leather: true
repair-wood: true
-
+control-block: wool
View
0  src/configs/dirt.yml → configs/dirt.yml
File renamed without changes
View
0  src/configs/example-chest.yml → configs/example-chest.yml
File renamed without changes
View
0  src/configs/example-torch.yml → configs/example-torch.yml
File renamed without changes
View
0  src/configs/glowstone.yml → configs/glowstone.yml
File renamed without changes
View
0  src/configs/netherrack.yml → configs/netherrack.yml
File renamed without changes
View
0  src/configs/soul_sand.yml → configs/soul_sand.yml
File renamed without changes
View
2  plugin.yml
@@ -1,3 +1,3 @@
name: CraftDispenser
main: com.echo28.bukkit.craftdispenser.CraftDispenser
-version: 1.1
+version: 1.2
View
47 src/com/echo28/bukkit/craftdispenser/CraftDispenser.java
@@ -4,30 +4,33 @@
import java.io.IOException;
import java.util.logging.Logger;
+import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
-import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
-import com.echo28.bukkit.craftdispenser.crafts.Config;
+import com.echo28.bukkit.craftdispenser.crafts.CraftConfig;
import com.echo28.bukkit.craftdispenser.crafts.CraftRepair;
/**
* CraftDispenser for Bukkit
*
+ * Huge thanks to revcompgeek for his hard work
+ *
* @author Nodren
+ * @author revcompgeek
*/
public class CraftDispenser extends JavaPlugin
{
private final CraftDispenserBlockListener blockListener = new CraftDispenserBlockListener(this);
- private final Logger log = Logger.getLogger("CraftDispenser");
+ private final Logger log = Logger.getLogger("Minecraft");
public Boolean repairDiamond = true;
public Boolean repairGold = true;
@@ -36,6 +39,10 @@
public Boolean repairLeather = true;
public Boolean repairWood = true;
+ private Material controlBlock = Material.WOOL;
+
+ private CraftConfig configCraft;
+ private CraftRepair repairCraft;
public CraftDispenser(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File folder, File plugin, ClassLoader cLoader)
{
@@ -60,6 +67,7 @@ public CraftDispenser(PluginLoader pluginLoader, Server instance, PluginDescript
repairStone = getConfiguration().getBoolean("repair-stone", true);
repairLeather = getConfiguration().getBoolean("repair-leather", true);
repairWood = getConfiguration().getBoolean("repair-wood", true);
+ controlBlock = Material.matchMaterial(getConfiguration().getString("control-block", "wool"));
}
public void onDisable()
@@ -71,29 +79,15 @@ public void onEnable()
{
PluginManager pm = getServer().getPluginManager();
pm.registerEvent(Event.Type.REDSTONE_CHANGE, blockListener, Priority.Highest, this);
+ loadCrafts();
log.info(getDescription().getName() + " " + getDescription().getVersion() + " loaded.");
}
-
- public static void dispenseItems(Block block, ItemStack dispenseItem) {
- dispenseItems(block, new ItemStack[] {dispenseItem});
- }
-
- public static void dispenseItems(Block block, ItemStack[] dispenseItems) {
- org.bukkit.craftbukkit.block.CraftDispenser cd = new org.bukkit.craftbukkit.block.CraftDispenser(block);
- ItemStack[] contents = cd.getInventory().getContents();
- ItemStack[] dispenseContents = new ItemStack[9];
-
- int totalItems = 0;
- for (int i = 0; i < dispenseItems.length; i++) {
- totalItems += dispenseItems[i].getAmount();
- dispenseContents[i] = dispenseItems[i];
- }
-
- cd.getInventory().setContents(dispenseContents);
- for (int i = 0; i < totalItems; i++)
- cd.dispense();
- cd.getInventory().setContents(contents);
+
+ private void loadCrafts()
+ {
+ configCraft = new CraftConfig(this);
+ repairCraft = new CraftRepair(this);
}
public boolean isNextToDispenser(Block block)
@@ -124,15 +118,14 @@ public Boolean isValidCraftDispenser(Block block)
Block b = block.getFace(BlockFace.DOWN);
if (b != null)
{
- if (b.getType().name() == "WOOL") { return true; }
+ if (controlBlock.equals(b.getType())) { return true; }
}
return false;
}
-
public void runCrafts(Block block)
{
- if (new Config(this, block).make()) { return; }
- if (new CraftRepair(this, block).make()) { return; }
+ if (repairCraft.make(block)) { return; }
+ if (configCraft.make(block)) { return; }
}
}
View
3  src/com/echo28/bukkit/craftdispenser/ItemSpec.java
@@ -42,7 +42,7 @@ public ItemStack createItemStack() {
return itemstacks;
}
- public static ItemSpec[] parseItemList(List<String> itemsList)
+ public static ItemSpec[] parseItems(List<String> itemsList)
{
ItemSpec[] items = new ItemSpec[itemsList.size()];
@@ -80,7 +80,6 @@ public static ItemSpec parseItem(String itemStr) {
if (mat != null)
ret.id = mat.getId();
-
try {
ret.data = Byte.parseByte(m.group(2));
ret.damage = Short.parseShort(m.group(3));
View
76 src/com/echo28/bukkit/craftdispenser/crafts/Config.java
@@ -1,76 +0,0 @@
-package com.echo28.bukkit.craftdispenser.crafts;
-
-import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.bukkit.block.Block;
-import org.bukkit.util.config.Configuration;
-
-import com.echo28.bukkit.craftdispenser.CraftDispenser;
-import com.echo28.bukkit.craftdispenser.ItemSpec;
-
-
-public class Config extends Craft
-{
- private List<Configuration> configs = new ArrayList<Configuration>();
-
- public Config(CraftDispenser plugin, Block block)
- {
- super(plugin, block);
-
- loadCustom();
- }
-
- private void loadCustom()
- {
- for (File file : plugin.getDataFolder().listFiles())
- {
- if (file.getName().equalsIgnoreCase("config.yml"))
- continue;
-
- Configuration config = new Configuration(file);
- config.load();
- configs.add(config);
- }
- }
-
- @Override
- public boolean make()
- {
- for (Configuration config : configs)
- {
- if (checkConfig(config))
- {
- ItemSpec[] items = ItemSpec.parseItemList(config.getStringList("craft", null));
-
- CraftDispenser.dispenseItems(block, ItemSpec.createItemStacks(items));
-
-
- ItemSpec[] outputItems = ItemSpec.parseItemList(config.getStringList("output-items", null));
- if (outputItems != null && outputItems.length == 9) {
- for (int i = 0; i < 9; i++) {
- ItemSpec item = outputItems[i];
- if (item.id != 0 && item.id != -1) {
- inventory.setItem(i, item.createItemStack());
- }
- }
- }
-
- return true;
- }
- }
- return false;
- }
-
- private boolean checkConfig(Configuration config)
- {
- if (config.getProperty("input-items-vertical") != null) {
- return checkVerticalItems(ItemSpec.parseItemList(config.getStringList("input-items-vertical", null)));
- }
- if (config.getProperty("input-items") != null) {
- return checkCustomItems(ItemSpec.parseItemList(config.getStringList("input-items", null)));
- }
- return false;
- }
-}
View
80 src/com/echo28/bukkit/craftdispenser/crafts/Craft.java
@@ -1,5 +1,7 @@
package com.echo28.bukkit.craftdispenser.crafts;
+import java.util.logging.Logger;
+
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Dispenser;
@@ -16,30 +18,40 @@
protected Block block = null;
protected Dispenser dispenser = null;
protected Inventory inventory;
+ protected final Logger log = Logger.getLogger("minecraft");
- public Craft(CraftDispenser plugin, Block block)
+ public Craft(CraftDispenser plugin)
{
this.plugin = plugin;
- this.block = (Block) block;
+ }
+
+ public void load(Block block)
+ {
+ this.block = block;
this.dispenser = (Dispenser) this.block.getState();
this.inventory = this.dispenser.getInventory();
}
+ public boolean make(Block block)
+ {
+ load(block);
+ return make();
+ }
+
abstract public boolean make();
-
+
public int countAir()
{
int airCount = 0;
for (int i = 0; i < 9; i++)
{
- if (inventory.getItem(i).getType() == Material.AIR)
- airCount += 1;
+ if (inventory.getItem(i).getType() == Material.AIR) airCount += 1;
}
return airCount;
}
protected boolean checkVerticalItems(ItemSpec[] items)
- {
+ {
if (countAir() != (9 - items.length))
return false;
@@ -49,48 +61,48 @@ protected boolean checkVerticalItems(ItemSpec[] items)
if(items[0].matchesStack(inventory.getItem(i)) &&
items[1].matchesStack(inventory.getItem(i+3))) {
newSubtractItem(i, items[0].amount);
- newSubtractItem(i+3, items[1].amount);
+ newSubtractItem(i+3, items[1].amount);
return true;
}
}
- return false;
+ return false;
} else if (items.length == 3) {
for (int i = 0; i < 3; i++){
if(items[0].matchesStack(inventory.getItem(i)) &&
items[1].matchesStack(inventory.getItem(i+3)) &&
items[2].matchesStack(inventory.getItem(i+6))) {
newSubtractItem(i, items[0].amount);
- newSubtractItem(i+3, items[1].amount);
- newSubtractItem(i+6, items[2].amount);
+ newSubtractItem(i + 3, items[1].amount);
+ newSubtractItem(i + 6, items[2].amount);
return true;
}
}
return false;
}
-
+
return false;
- }
+ }
- protected boolean checkCustomItems(ItemSpec[] items)
+ protected boolean checkCustomItems(ItemSpec[] items)
{
int[] slotsToSubtract = new int[9];
for (int i = 0; i < 9; i++)
- {
+ {
ItemSpec item = items[i];
if (item.id == 0)
continue;
if (item.matchesStack(inventory.getItem(i)))
- slotsToSubtract[i] = item.amount;
+ slotsToSubtract[i] = item.amount;
else
return false;
}
-
+
newSubtractItems(slotsToSubtract);
return true;
}
-
+
public void newSubtractItems(int[] slots)
{
for (int slot = 0; slot < 9; slot++)
@@ -104,7 +116,8 @@ public void newSubtractItem(int slot, int howMuch)
if (howMuch == 0) return;
ItemStack items = inventory.getItem(slot);
int amount = items.getAmount() - howMuch;
- if (amount < 0) {
+ if (amount < 0)
+ {
System.out.printf("%d %d %s", amount, howMuch, items.toString());
new Exception().printStackTrace();
items = null;
@@ -113,19 +126,32 @@ else if (amount == 0)
items = null;
else
items.setAmount(amount);
-
+
inventory.setItem(slot, items);
}
- /*public void emptyBucket(int slot)
+ public void dispenseItems(Block block, ItemStack dispenseItem)
{
- ItemStack items;
- items = inventory.getItem(slot);
- if ((items.getType() == Material.LAVA_BUCKET) || (items.getType() == Material.WATER_BUCKET) || (items.getType() == Material.MILK_BUCKET))
+ dispenseItems(block, new ItemStack[]
+ { dispenseItem });
+ }
+
+ public void dispenseItems(Block block, ItemStack[] dispenseItems)
+ {
+ Dispenser cd = (Dispenser) block.getState();
+ ItemStack[] contents = cd.getInventory().getContents();
+ ItemStack[] dispenseContents = new ItemStack[9];
+
+ int totalItems = 0;
+ for (int i = 0; i < dispenseItems.length; i++)
{
- items = new ItemStack(Material.BUCKET, 1);
- inventory.setItem(slot, items);
+ totalItems += dispenseItems[i].getAmount();
+ dispenseContents[i] = dispenseItems[i];
}
- }*/
-
+
+ cd.getInventory().setContents(dispenseContents);
+ for (int i = 0; i < totalItems; i++)
+ cd.dispense();
+ cd.getInventory().setContents(contents);
+ }
}
View
72 src/com/echo28/bukkit/craftdispenser/crafts/CraftConfig.java
@@ -0,0 +1,72 @@
+package com.echo28.bukkit.craftdispenser.crafts;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.bukkit.util.config.Configuration;
+
+import com.echo28.bukkit.craftdispenser.CraftDispenser;
+import com.echo28.bukkit.craftdispenser.ItemSpec;
+
+
+public class CraftConfig extends Craft
+{
+ private List<Configuration> configs = new ArrayList<Configuration>();
+
+ public CraftConfig(CraftDispenser plugin)
+ {
+ super(plugin);
+
+ loadCustom();
+ }
+
+ private void loadCustom()
+ {
+ for (File file : plugin.getDataFolder().listFiles())
+ {
+ if (file.getName().equalsIgnoreCase("config.yml")) continue;
+
+ Configuration config = new Configuration(file);
+ config.load();
+ configs.add(config);
+ }
+ }
+
+ @Override
+ public boolean make()
+ {
+ for (Configuration config : configs)
+ {
+ if (checkConfig(config))
+ {
+ ItemSpec[] items = ItemSpec.parseItems(config.getStringList("craft", null));
+
+ dispenseItems(block, ItemSpec.createItemStacks(items));
+
+ ItemSpec[] outputItems = ItemSpec.parseItems(config.getStringList("output-items", null));
+ if (outputItems != null && outputItems.length == 9)
+ {
+ for (int i = 0; i < 9; i++)
+ {
+ ItemSpec item = outputItems[i];
+ if (item.id != 0 && item.id != -1)
+ {
+ inventory.setItem(i, item.createItemStack());
+ }
+ }
+ }
+
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private boolean checkConfig(Configuration config)
+ {
+ if (config.getProperty("input-items-vertical") != null) { return checkVerticalItems(ItemSpec.parseItems(config.getStringList("input-items-vertical", null))); }
+ if (config.getProperty("input-items") != null) { return checkCustomItems(ItemSpec.parseItems(config.getStringList("input-items", null))); }
+ return false;
+ }
+}
View
100 src/com/echo28/bukkit/craftdispenser/crafts/CraftRepair.java
@@ -1,7 +1,6 @@
package com.echo28.bukkit.craftdispenser.crafts;
import org.bukkit.Material;
-import org.bukkit.block.Block;
import org.bukkit.inventory.ItemStack;
import com.echo28.bukkit.craftdispenser.CraftDispenser;
@@ -10,67 +9,57 @@
public class CraftRepair extends Craft
{
- public CraftRepair(CraftDispenser plugin, Block block)
+ public CraftRepair(CraftDispenser plugin)
{
- super(plugin, block);
+ super(plugin);
}
-
- public boolean make() {
- if (plugin.repairDiamond) {
- if (makeTools(Material.DIAMOND,
- new Material[] {Material.DIAMOND_PICKAXE, Material.DIAMOND_SPADE,
- Material.DIAMOND_AXE, Material.DIAMOND_SWORD, Material.DIAMOND_HOE}))
- return true;
- if (makeArmors(Material.DIAMOND, new Material[] {Material.DIAMOND_HELMET, Material.DIAMOND_CHESTPLATE,
- Material.DIAMOND_LEGGINGS, Material.DIAMOND_BOOTS}))
- return true;
+
+ public boolean make()
+ {
+ if (plugin.repairDiamond)
+ {
+ if (makeTools(Material.DIAMOND, new Material[]
+ { Material.DIAMOND_PICKAXE, Material.DIAMOND_SPADE, Material.DIAMOND_AXE, Material.DIAMOND_SWORD, Material.DIAMOND_HOE })) return true;
+ if (makeArmors(Material.DIAMOND, new Material[]
+ { Material.DIAMOND_HELMET, Material.DIAMOND_CHESTPLATE, Material.DIAMOND_LEGGINGS, Material.DIAMOND_BOOTS })) return true;
}
-
- if (plugin.repairGold) {
- if (makeTools(Material.GOLD_INGOT,
- new Material[] {Material.GOLD_PICKAXE, Material.GOLD_SPADE,
- Material.GOLD_AXE, Material.GOLD_SWORD, Material.GOLD_HOE}))
- return true;
- if (makeArmors(Material.GOLD_INGOT, new Material[] {Material.GOLD_HELMET, Material.GOLD_CHESTPLATE,
- Material.GOLD_LEGGINGS, Material.GOLD_BOOTS}))
- return true;
+
+ if (plugin.repairGold)
+ {
+ if (makeTools(Material.GOLD_INGOT, new Material[]
+ { Material.GOLD_PICKAXE, Material.GOLD_SPADE, Material.GOLD_AXE, Material.GOLD_SWORD, Material.GOLD_HOE })) return true;
+ if (makeArmors(Material.GOLD_INGOT, new Material[]
+ { Material.GOLD_HELMET, Material.GOLD_CHESTPLATE, Material.GOLD_LEGGINGS, Material.GOLD_BOOTS })) return true;
}
-
- if (plugin.repairIron) {
- if (makeTools(Material.IRON_INGOT,
- new Material[] {Material.IRON_PICKAXE, Material.IRON_SPADE,
- Material.IRON_AXE, Material.IRON_SWORD, Material.IRON_HOE}))
- return true;
- if (makeArmors(Material.IRON_INGOT, new Material[] {Material.IRON_HELMET, Material.IRON_CHESTPLATE,
- Material.IRON_LEGGINGS, Material.IRON_BOOTS}))
- return true;
+
+ if (plugin.repairIron)
+ {
+ if (makeTools(Material.IRON_INGOT, new Material[]
+ { Material.IRON_PICKAXE, Material.IRON_SPADE, Material.IRON_AXE, Material.IRON_SWORD, Material.IRON_HOE })) return true;
+ if (makeArmors(Material.IRON_INGOT, new Material[]
+ { Material.IRON_HELMET, Material.IRON_CHESTPLATE, Material.IRON_LEGGINGS, Material.IRON_BOOTS })) return true;
}
-
-
- if (plugin.repairLeather) {
- if (makeArmors(Material.LEATHER, new Material[] {Material.LEATHER_HELMET, Material.LEATHER_CHESTPLATE,
- Material.LEATHER_LEGGINGS, Material.LEATHER_BOOTS}))
- return true;
+
+ if (plugin.repairLeather)
+ {
+ if (makeArmors(Material.LEATHER, new Material[]
+ { Material.LEATHER_HELMET, Material.LEATHER_CHESTPLATE, Material.LEATHER_LEGGINGS, Material.LEATHER_BOOTS })) return true;
}
-
-
- if (plugin.repairStone) {
- if (makeTools(Material.COBBLESTONE,
- new Material[] {Material.STONE_PICKAXE, Material.STONE_SPADE,
- Material.STONE_AXE, Material.STONE_SWORD, Material.STONE_HOE}))
- return true;
+
+ if (plugin.repairStone)
+ {
+ if (makeTools(Material.COBBLESTONE, new Material[]
+ { Material.STONE_PICKAXE, Material.STONE_SPADE, Material.STONE_AXE, Material.STONE_SWORD, Material.STONE_HOE })) return true;
}
-
-
- if (plugin.repairWood) {
- if (makeTools(Material.WOOD,
- new Material[] {Material.WOOD_PICKAXE, Material.WOOD_SPADE,
- Material.WOOD_AXE, Material.WOOD_SWORD, Material.WOOD_HOE}))
- return true;
+
+ if (plugin.repairWood)
+ {
+ if (makeTools(Material.WOOD, new Material[]
+ { Material.WOOD_PICKAXE, Material.WOOD_SPADE, Material.WOOD_AXE, Material.WOOD_SWORD, Material.WOOD_HOE })) return true;
}
-
+
return false;
- }
+ }
private boolean makeTools(Material type, Material[] tools) {
for (Material tool : tools) {
@@ -78,18 +67,19 @@ private boolean makeTools(Material type, Material[] tools) {
new ItemSpec(type.getId()),
new ItemSpec(tool.getId()),
new ItemSpec(Material.STICK.getId()) })) {
- CraftDispenser.dispenseItems(block, new ItemStack(tool, 1));
+ dispenseItems(block, new ItemStack(tool, 1));
return true;
}
}
return false;
}
+
private boolean makeArmors(Material type, Material[] armors) {
for (Material armor : armors) {
if (checkVerticalItems(new ItemSpec[] {
new ItemSpec(type.getId()),
new ItemSpec(armor.getId()) })) {
- CraftDispenser.dispenseItems(block, new ItemStack(armor, 1));
+ dispenseItems(block, new ItemStack(armor, 1));
return true;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.