Skip to content

Commit

Permalink
Block ItemScript recipe craft cheating
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Sep 11, 2013
1 parent feefc08 commit d92be7c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dItem.java
Expand Up @@ -743,7 +743,6 @@ public String getAttribute(Attribute attribute) {
// -->
if (attribute.startsWith("scriptname")) // Note: Update this when the id: is stored less stupidly!
if (getItemStack().hasItemMeta() && getItemStack().getItemMeta().hasLore()) {
List<String> loreList = new ArrayList<String>();
for (String itemLore : getItemStack().getItemMeta().getLore())
if (itemLore.startsWith("§0id:"))
return new Element(itemLore.substring(5)).getAttribute(attribute.fulfill(1));
Expand Down
Expand Up @@ -23,36 +23,41 @@ public class ItemScriptContainer extends ScriptContainer {
dNPC npc = null;
dPlayer player = null;
public boolean bound = false;
List<dItem> recipe = null;

public ItemScriptContainer(ConfigurationSection configurationSection, String scriptContainerName) {
super(configurationSection, scriptContainerName);
ItemScriptHelper.item_scripts.put(getName(), this);
// Set Recipe
if (contains("RECIPE")) {
List<dItem> materials = new ArrayList<dItem>();
recipe = new ArrayList<dItem>();
for (String recipeRow : getStringList("RECIPE")) {
recipeRow = TagManager.tag(player, npc, recipeRow);
String[] row = recipeRow.split("\\|", 3);
for (String material : row) {
materials.add(materials.size(), dItem.valueOf(material));
recipe.add(recipe.size(), dItem.valueOf(material));
}
}
ShapedRecipe recipe = new ShapedRecipe(getItemFrom().getItemStack());
recipe.shape("abc", "def", "ghi");
ShapedRecipe shapedRecipe = new ShapedRecipe(getItemFrom().getItemStack());
shapedRecipe.shape("abc", "def", "ghi");
char x = 'a';
for (dItem material : materials) {
for (dItem material : recipe) {
if (!material.getItemStack().getType().name().equals("AIR"))
recipe.setIngredient(x, material.getItemStack().getData());
shapedRecipe.setIngredient(x, material.getItemStack().getData());
x++;
}
Bukkit.getServer().addRecipe(recipe);
Bukkit.getServer().addRecipe(shapedRecipe);
}
}

public dItem getItemFrom() {
return getItemFrom(null, null);
}

public List<dItem> getRecipe() {
return recipe;
}

public dItem getItemFrom(dPlayer player, dNPC npc) {
// Try to use this script to make an item.
dItem stack = null;
Expand Down
Expand Up @@ -3,6 +3,7 @@
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.ScriptBuilder;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.commands.core.DetermineCommand;
import net.aufdemrand.denizen.scripts.queues.core.InstantQueue;
import net.aufdemrand.denizen.utilities.DenizenAPI;
Expand Down Expand Up @@ -148,10 +149,33 @@ public void craftItem(CraftItemEvent event) {
// Run a script on craft of an item script
if (isItemScript(event.getRecipe().getResult())) {
if (!(event.getWhoClicked() instanceof Player)) return;

dItem item = new dItem(event.getRecipe().getResult());
ItemScriptContainer script = null;
for (String itemLore : item.getItemStack().getItemMeta().getLore())
if (itemLore.startsWith("§0id:")) // Note: Update this when the id: is stored less stupidly!
script = (ItemScriptContainer) ScriptRegistry.getScriptContainerAs(itemLore.substring(5), ItemScriptContainer.class);

if (script == null) {
dB.echoDebug("Tried to craft non-existant script!");
return;
}

for (int i = 0;i < 9;i++) {
if (!script.getRecipe().get(i).identify().split(":")[0].equalsIgnoreCase(
(new dItem(event.getInventory().getMatrix()[i])).identify().split(":")[0])) { // This probably can be compared more efficiently...
dB.echoDebug("Ignoring craft attempt using "
+ (new dItem(event.getInventory().getMatrix()[i])).identify().split(":")[0]
+ " instead of " + script.getRecipe().get(i).identify().split(":")[0]);
event.setCancelled(true);
return;
}
}

Map<String, Object> context = new HashMap<String, Object>();
String determination = doEvents(Arrays.asList
("craft"),
null, (Player) event.getWhoClicked(), context);
String determination = doEvents(Arrays.asList("craft"),
null, (Player) event.getWhoClicked(), context);

if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
}
Expand Down

0 comments on commit d92be7c

Please sign in to comment.