Craftable Spawners

This commit is contained in:
Brianna O'Keefe 2019-02-19 09:51:05 -05:00
parent 8207be16f7
commit 76c3db539f
7 changed files with 152 additions and 7 deletions

View File

@ -562,4 +562,15 @@ public interface SpawnerData {
*/
List<SpawnCondition> getConditions();
boolean isCraftable();
void setCraftable(boolean craftable);
String getRecipe();
void setRecipe(String recipe);
List<String> getRecipeIngredients();
void setRecipeIngredients(List<String> recipeIngredients);
}

View File

@ -1,6 +1,7 @@
package com.songoda.epicspawners.api.utils;
import java.util.Collection;
import java.util.List;
import com.songoda.epicspawners.api.particles.ParticleDensity;
import com.songoda.epicspawners.api.particles.ParticleEffect;
@ -28,6 +29,12 @@ public interface SpawnerDataBuilder {
*/
SpawnerDataBuilder displayName(String name);
SpawnerDataBuilder craftable(boolean craftable);
SpawnerDataBuilder recipe(String recipe);
SpawnerDataBuilder recipeIngredients(List<String> recipeIngredients);
/**
* Define the unique id for this spawner.
*

View File

@ -6,10 +6,8 @@ import com.songoda.epicspawners.api.EpicSpawnersAPI;
import com.songoda.epicspawners.api.particles.ParticleDensity;
import com.songoda.epicspawners.api.particles.ParticleEffect;
import com.songoda.epicspawners.api.particles.ParticleType;
import com.songoda.epicspawners.api.spawner.Spawner;
import com.songoda.epicspawners.api.spawner.SpawnerData;
import com.songoda.epicspawners.api.spawner.SpawnerManager;
import com.songoda.epicspawners.api.spawner.SpawnerStack;
import com.songoda.epicspawners.api.spawner.condition.SpawnCondition;
import com.songoda.epicspawners.api.utils.ClaimableProtectionPluginHook;
import com.songoda.epicspawners.api.utils.ProtectionPluginHook;
@ -35,7 +33,6 @@ import com.songoda.epicspawners.spawners.spawner.ESpawner;
import com.songoda.epicspawners.spawners.spawner.ESpawnerManager;
import com.songoda.epicspawners.spawners.spawner.ESpawnerStack;
import com.songoda.epicspawners.storage.Storage;
import com.songoda.epicspawners.storage.StorageItem;
import com.songoda.epicspawners.storage.StorageRow;
import com.songoda.epicspawners.storage.types.StorageMysql;
import com.songoda.epicspawners.storage.types.StorageYaml;
@ -44,10 +41,7 @@ import com.songoda.epicspawners.tasks.SpawnerSpawnTask;
import com.songoda.epicspawners.utils.*;
import com.songoda.epicspawners.utils.gui.AbstractGUI;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.*;
import org.bukkit.block.Biome;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.command.ConsoleCommandSender;
@ -57,6 +51,7 @@ import org.bukkit.entity.EntityType;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.PluginManager;
@ -298,6 +293,10 @@ public class EpicSpawnersPlugin extends JavaPlugin implements EpicSpawners {
if (hologram != null)
hologram.loadHolograms();
//Register Crafting Recipe
System.out.println("[" + getDescription().getName() + "] Loading Crafting Recipes");
this.enabledRecipe();
// Save data initially so that if the person reloads again fast they don't lose all their data.
this.saveToFile();
}
@ -337,6 +336,9 @@ public class EpicSpawnersPlugin extends JavaPlugin implements EpicSpawners {
.convertRatio(currentSection.getString("Convert-Ratio"))
.inShop(currentSection.getBoolean("In-Shop"))
.pickupCost(currentSection.getDouble("Pickup-Cost"))
.craftable(currentSection.getBoolean("Craftable"))
.recipe(currentSection.getString("Recipe-Layout"))
.recipeIngredients(currentSection.getStringList("Recipe-Ingredients"))
.shopPrice(currentSection.getDouble("Shop-Price"))
.killGoal(currentSection.getInt("CustomGoal"))
.upgradeCostEconomy(currentSection.getInt("Custom-ECO-Cost"))
@ -462,6 +464,9 @@ public class EpicSpawnersPlugin extends JavaPlugin implements EpicSpawners {
currentSection.set("Custom-XP-Cost", spawnerData.getUpgradeCostExperience());
currentSection.set("Tick-Rate", spawnerData.getTickRate());
currentSection.set("Pickup-cost", spawnerData.getPickupCost());
currentSection.set("Craftable", spawnerData.isCraftable());
currentSection.set("Recipe-Layout", spawnerData.getRecipe());
currentSection.set("Recipe-Ingredients", spawnerData.getRecipeIngredients());
currentSection.set("Spawn-Effect", spawnerData.getParticleEffect().name());
currentSection.set("Spawn-Effect-Particle", spawnerData.getSpawnEffectParticle().name());
@ -554,6 +559,54 @@ public class EpicSpawnersPlugin extends JavaPlugin implements EpicSpawners {
return this.registerProtectionHook(hookSupplier.get());
}
private void enabledRecipe() {
top:
for (SpawnerData spawnerData : spawnerManager.getAllSpawnerData()) {
if (!spawnerData.isCraftable()) continue;
String recipe = spawnerData.getRecipe();
String type = spawnerData.getIdentifyingName().toUpperCase().replace(" ", "_").replace("MUSHROOM_COW", "MOOSHROOM");
ShapedRecipe spawnerRecipe = new ShapedRecipe(new NamespacedKey(this, "SPAWNER_RECIPE_" + type), newSpawnerItem(spawnerData, 1));
if (recipe.length() != 9) return;
String[] split = Methods.splitStringEvery(recipe, 3);
spawnerRecipe.shape(split[0], split[1], split[2]);
List<String> ingredients = spawnerData.getRecipeIngredients();
if (ingredients.isEmpty()) return;
for (String ingredient : ingredients) {
try {
if (!ingredient.contains(",")) return;
String[] s = ingredient.split(",");
char letter = s[0].trim().toCharArray()[0];
String materialStr = s[1].trim();
Material material;
if (materialStr.equals("SPAWN_EGG")) {
try {
material = Material.valueOf(type + "_SPAWN_EGG");
} catch (Exception ignored) {
continue top;
}
} else {
material = Material.valueOf(materialStr);
}
spawnerRecipe.setIngredient(letter, material);
} catch (Exception e) {
e.printStackTrace();
}
}
getServer().addRecipe(spawnerRecipe);
}
}
private void processDefault(String value) {
FileConfiguration spawnerConfig = spawnerFile.getConfig();
@ -621,6 +674,9 @@ public class EpicSpawnersPlugin extends JavaPlugin implements EpicSpawners {
spawnerConfig.addDefault("Entities." + type + ".Spawner-Spawn-Particle", "FIRE");
spawnerConfig.addDefault("Entities." + type + ".Particle-Amount", "NORMAL");
spawnerConfig.addDefault("Entities." + type + ".Particle-Effect-Boosted-Only", true);
spawnerConfig.addDefault("Entities." + type + ".Craftable", false);
spawnerConfig.addDefault("Entities." + type + ".Recipe-Layout", "AAAABAAAA");
spawnerConfig.addDefault("Entities." + type + ".Recipe-Ingredients", Arrays.asList("A, IRON_BARS", "B, SPAWN_EGG"));
if (entityType == EntityType.SLIME) {
spawnerConfig.addDefault("Entities." + type + ".Conditions.Biomes", Biome.SWAMP);

View File

@ -34,6 +34,10 @@ public class ESpawnerData implements SpawnerData {
private String displayName;
private Material displayItem = null;
private boolean craftable = false;
private String recipe = "AAAABAAAA";
private List<String> recipeIngredients = Arrays.asList("A, IRON_BARS", "B, SPAWN_EGG");
private String tickRate = "800:200";
private ParticleEffect particleEffect = ParticleEffect.HALO;
@ -420,6 +424,35 @@ public class ESpawnerData implements SpawnerData {
return Collections.unmodifiableList(spawnConditions);
}
@Override
public boolean isCraftable() {
return craftable;
}
@Override
public void setCraftable(boolean craftable) {
this.craftable = craftable;
}
@Override
public String getRecipe() {
return recipe;
}
@Override
public void setRecipe(String recipe) {
this.recipe = recipe;
}
@Override
public List<String> getRecipeIngredients() {
return recipeIngredients;
}
@Override
public void setRecipeIngredients(List<String> recipeIngredients) {
this.recipeIngredients = recipeIngredients;
}
@Override
public int hashCode() {

View File

@ -13,6 +13,7 @@ import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public final class ESpawnerDataBuilder implements SpawnerDataBuilder {
@ -29,6 +30,24 @@ public final class ESpawnerDataBuilder implements SpawnerDataBuilder {
return this;
}
@Override
public SpawnerDataBuilder craftable(boolean craftable) {
this.spawnerData.setCraftable(craftable);
return this;
}
@Override
public SpawnerDataBuilder recipe(String recipe) {
this.spawnerData.setRecipe(recipe);
return this;
}
@Override
public SpawnerDataBuilder recipeIngredients(List<String> recipeIngredients) {
this.spawnerData.setRecipeIngredients(recipeIngredients);
return this;
}
@Override
public SpawnerDataBuilder uuid(int uuid) {
this.spawnerData.setUUID(uuid);

View File

@ -324,6 +324,21 @@ public class Methods {
return has;
}
public static String[] splitStringEvery(String s, int interval) {
int arrayLength = (int) Math.ceil(((s.length() / (double)interval)));
String[] result = new String[arrayLength];
int j = 0;
int lastIndex = result.length - 1;
for (int i = 0; i < lastIndex; i++) {
result[i] = s.substring(j, j + interval);
j += interval;
} //Add the last bit
result[lastIndex] = s.substring(j);
return result;
}
/**
* Makes the specified Unix Epoch time human readable as per the format settings in the Arconix config.
*

View File

@ -277,6 +277,10 @@ public class SettingsManager implements Listener {
this.option = option;
}
public List<String> getStringList() {
return EpicSpawnersPlugin.getInstance().getConfig().getStringList(setting);
}
public boolean getBoolean() {
return EpicSpawnersPlugin.getInstance().getConfig().getBoolean(setting);
}