Fix Issues #4, #5, #6.

master
sigonasr2 6 years ago
parent 20a4f905a1
commit d2c2165534
  1. 2
      MCRandomizer/src/scramble/plugin/FurnaceRecipeStore.java
  2. 255
      MCRandomizer/src/scramble/plugin/Template.java
  3. 38
      MCRandomizer/src/scramble/plugin/listener/ExampleListener.java

@ -29,5 +29,5 @@ public class FurnaceRecipeStore implements RecipeStore{
public void setResultItem(String item) {
this.finalItem=item;
}
//6,12,11,102
}

@ -4,6 +4,7 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
@ -12,6 +13,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
@ -67,6 +69,7 @@ public class Template
public static ItemStack randomMelonItem;
public static int recipe_count=0;
public static boolean finished=false;
public static int villager_count=0;
PluginDescriptionFile pdfFile;
@ -129,7 +132,7 @@ public class Template
String[] splitter = s2.split(",");
List<Material> splitter2 = new ArrayList();
List<Material> splitter2 = new ArrayList<Material>();
for (int i = 1; i < splitter.length; i++) {
Material m = Material.getMaterial(splitter[i]);
@ -301,86 +304,166 @@ public class Template
breedingTable.put(canBreed.get(0), canBreed.get(0));
}
}
boolean IsAlphabeticallyAfter(String name, String name2, String compare2, String compare22) {
for (int i=0;i<name.length();i++) {
if (i<name.length() && i<name2.length()) {
if (name.charAt(i)<name2.charAt(i)) {
return false;
} else
if (name.charAt(i)>name2.charAt(i)) {
return true;
}
} else {
break;
}
}
if (name.length()>name2.length()) {
return true;
} else
if (name.length()<name2.length()) {
return false;
} else {
name=compare2;
name2=compare22;
for (int i=0;i<name.length();i++) {
if (i<name.length() && i<name2.length()) {
if (name.charAt(i)<name2.charAt(i)) {
return false;
} else
if (name.charAt(i)>name2.charAt(i)) {
return true;
}
}
}
return false;
}
}
public void onEnable()
{
craftingrecipes = new ArrayList();
smeltingrecipes = new ArrayList();
shufflelist = new ArrayList();
archivedshufflelist = new ArrayList();
shufflelist2 = new ArrayList();
recipeStorage = new ArrayList();
// Bukkit.getLogger().setLevel(Level.WARNING);
Bukkit.resetRecipes();
craftingrecipes = new ArrayList<String>();
smeltingrecipes = new ArrayList<String>();
shufflelist = new ArrayList<String>();
archivedshufflelist = new ArrayList<String>();
shufflelist2 = new ArrayList<ItemStack>();
recipeStorage = new ArrayList<RecipeStore>();
plugin = (Template)getPlugin(Template.class);
r = new Random(Bukkit.getWorld("world").getSeed());
List<Recipe> defaultRecipes = new ArrayList();
Bukkit.getLogger().info("Random seed is "+Bukkit.getWorld("world").getSeed());
LoadVillagerCount();
List<Recipe> defaultRecipes = new ArrayList<Recipe>();
Iterator<Recipe> it = Bukkit.recipeIterator();
while (it.hasNext()) {
Recipe r = (Recipe)it.next();
defaultRecipes.add(r);
shufflelist2.add(r.getResult());
Bukkit.getLogger().info("Found a recipe for " + r.getResult());
Recipe r = it.next();
boolean added=false;
/*if (r instanceof BlastingRecipe ||
r instanceof CampfireRecipe ||
r instanceof SmokingRecipe ||
r instanceof StonecuttingRecipe) {*/
for (int i=0;i<shufflelist2.size();i++) {
if (!IsAlphabeticallyAfter(r.getResult().getType().name(),shufflelist2.get(i).getType().name(),r.getClass().getName(),defaultRecipes.get(i).getClass().getName())) {
//Bukkit.getLogger().info(r.getResult().getType().name()+" is not alphabetically after "+shufflelist2.get(i).getType().name()+". Inserting at position "+i);
shufflelist2.add(i,r.getResult());
defaultRecipes.add(i,r);
added=true;
break;
}
}
if (!added) {
if (shufflelist2.size()>0) {
//Bukkit.getLogger().info(r.getResult().getType().name()+" is alphabetically after "+shufflelist2.get(shufflelist2.size()-1).getType().name()+". Inserting at the end.");
}
shufflelist2.add(r.getResult());
defaultRecipes.add(r);
}
//Bukkit.getLogger().info("Found a recipe for " + r.getResult());
//THIS LIST IS NOT SORTED. REQUIRES MANUAL SORTING.
//}
}
for (int i=0;i<shufflelist2.size();i++) {
Bukkit.getLogger().info("Found a recipe for "+shufflelist2.get(i)+"|"+defaultRecipes.get(i).getResult());
}
Bukkit.clearRecipes();
HashMap<String, Integer> recipeTypeMap = new HashMap();
HashMap<String, Integer> recipeTypeMap = new HashMap<String, Integer>();
int blastingrecipe = 0;
int campfirerecipe = 0;
int smokingrecipe = 0;
int stonecuttingrecipe = 0;
for (int i=0;i<defaultRecipes.size();i++) {
Bukkit.getLogger().info(i+": "+defaultRecipes.get(i).getResult() + " - "+defaultRecipes.get(i).getClass());
}
while (shufflelist2.size() > 0) {
Recipe rr = (Recipe)defaultRecipes.get(r.nextInt(defaultRecipes.size()));
int recipe_id = r.nextInt(defaultRecipes.size());
Recipe rr = defaultRecipes.get(recipe_id);
Bukkit.getLogger().info("Adding recipe for "+shufflelist2.get(0)+" at recipe "+recipe_id);
boolean modified=true;
try {
if ((rr instanceof BlastingRecipe)) {
BlastingRecipe br = (BlastingRecipe)rr;
BlastingRecipe newbr = new BlastingRecipe(br.getKey(), (ItemStack)shufflelist2.get(0), br.getInputChoice(), br.getExperience(), br.getCookingTime());
Bukkit.addRecipe(newbr);
}
else if ((rr instanceof CampfireRecipe)) {
CampfireRecipe br = (CampfireRecipe)rr;
CampfireRecipe newbr = new CampfireRecipe(br.getKey(), (ItemStack)shufflelist2.get(0), br.getInputChoice(), br.getExperience(), br.getCookingTime());
Bukkit.addRecipe(newbr);
}
/*else if ((rr instanceof FurnaceRecipe)) {
FurnaceRecipe br = (FurnaceRecipe)rr;
FurnaceRecipe newbr = new FurnaceRecipe(br.getKey(), (ItemStack)shufflelist2.get(0), br.getInputChoice(), br.getExperience(), br.getCookingTime());
Bukkit.addRecipe(newbr);
}*/
else if ((rr instanceof MerchantRecipe)) {
MerchantRecipe br = (MerchantRecipe)rr;
MerchantRecipe newbr = new MerchantRecipe((ItemStack)shufflelist2.get(0), br.getUses(), br.getMaxUses(), true, br.getVillagerExperience(), br.getPriceMultiplier());
Bukkit.addRecipe(newbr);
}
/*else if ((rr instanceof ShapedRecipe)) {
ShapedRecipe br = (ShapedRecipe)rr;
ShapedRecipe newbr = new ShapedRecipe((ItemStack)shufflelist2.get(0));
newbr.shape(br.getShape());
for (Character c : br.getChoiceMap().keySet())
{
newbr.setIngredient(c.charValue(), (RecipeChoice)br.getChoiceMap().get(c));
}
Bukkit.addRecipe(newbr);
}
else if ((rr instanceof ShapelessRecipe)) {
ShapelessRecipe br = (ShapelessRecipe)rr;
ShapelessRecipe newbr = new ShapelessRecipe((ItemStack)shufflelist2.get(0));
for (ItemStack i : br.getIngredientList()) {
newbr.addIngredient(i.getType());
}
Bukkit.addRecipe(newbr);
}*/
else if ((rr instanceof SmokingRecipe)) {
SmokingRecipe br = (SmokingRecipe)rr;
SmokingRecipe newbr = new SmokingRecipe(br.getKey(), (ItemStack)shufflelist2.get(0), br.getInputChoice(), br.getExperience(), br.getCookingTime());
Bukkit.addRecipe(newbr);
}
else if ((rr instanceof StonecuttingRecipe)) {
StonecuttingRecipe br = (StonecuttingRecipe)rr;
StonecuttingRecipe newbr = new StonecuttingRecipe(br.getKey(), (ItemStack)shufflelist2.get(0), br.getInputChoice());
Bukkit.addRecipe(newbr);
} else {
modified=false;
}
}
catch (IllegalStateException localIllegalStateException) {}
//int amt = Bukkit.getRecipesFor(shufflelist2.get(0)).size();
if ((rr instanceof BlastingRecipe)) {
BlastingRecipe br = (BlastingRecipe)rr;
BlastingRecipe newbr = new BlastingRecipe(new NamespacedKey(plugin,"blasting_recipe"+shufflelist2.size()), shufflelist2.get(0), br.getInput().getType(), br.getExperience(), br.getCookingTime());
Bukkit.addRecipe(newbr);
Bukkit.getLogger().info(" BLASTING");
blastingrecipe++;
}
else if ((rr instanceof CampfireRecipe)) {
CampfireRecipe br = (CampfireRecipe)rr;
CampfireRecipe newbr = new CampfireRecipe(new NamespacedKey(plugin,"campfire_recipe"+shufflelist2.size()), shufflelist2.get(0), br.getInput().getType(), br.getExperience(), br.getCookingTime());
Bukkit.addRecipe(newbr);
Bukkit.getLogger().info(" CAMPFIRE");
campfirerecipe++;
}
/*else if ((rr instanceof FurnaceRecipe)) {
FurnaceRecipe br = (FurnaceRecipe)rr;
FurnaceRecipe newbr = new FurnaceRecipe(br.getKey(), (ItemStack)shufflelist2.get(0), br.getInputChoice(), br.getExperience(), br.getCookingTime());
Bukkit.addRecipe(newbr);
}*/
else if ((rr instanceof MerchantRecipe)) {
MerchantRecipe br = (MerchantRecipe)rr;
MerchantRecipe newbr = new MerchantRecipe(shufflelist2.get(0), br.getUses(), br.getMaxUses(), true, br.getVillagerExperience(), br.getPriceMultiplier());
Bukkit.addRecipe(newbr);
}
/*else if ((rr instanceof ShapedRecipe)) {
ShapedRecipe br = (ShapedRecipe)rr;
ShapedRecipe newbr = new ShapedRecipe((ItemStack)shufflelist2.get(0));
newbr.shape(br.getShape());
for (Character c : br.getChoiceMap().keySet())
{
newbr.setIngredient(c.charValue(), (RecipeChoice)br.getChoiceMap().get(c));
}
Bukkit.addRecipe(newbr);
}
else if ((rr instanceof ShapelessRecipe)) {
ShapelessRecipe br = (ShapelessRecipe)rr;
ShapelessRecipe newbr = new ShapelessRecipe((ItemStack)shufflelist2.get(0));
for (ItemStack i : br.getIngredientList()) {
newbr.addIngredient(i.getType());
}
Bukkit.addRecipe(newbr);
}*/
else if ((rr instanceof SmokingRecipe)) {
SmokingRecipe br = (SmokingRecipe)rr;
SmokingRecipe newbr = new SmokingRecipe(new NamespacedKey(plugin,"smoking_recipe"+shufflelist2.size()), shufflelist2.get(0), br.getInput().getType(), br.getExperience(), br.getCookingTime());
Bukkit.addRecipe(newbr);
Bukkit.getLogger().info(" SMOKING");
smokingrecipe++;
}
else if ((rr instanceof StonecuttingRecipe)) {
StonecuttingRecipe br = (StonecuttingRecipe)rr;
StonecuttingRecipe newbr = new StonecuttingRecipe(new NamespacedKey(plugin,"stonecutting_recipe"+shufflelist2.size()), shufflelist2.get(0), br.getInput().getType());
Bukkit.addRecipe(newbr);
Bukkit.getLogger().info(" STONECUTTING");
stonecuttingrecipe++;
} else {
Bukkit.getLogger().info(" UNMODIFIED ("+rr.getClass().getName()+")");
modified=false;
}
if (modified) {
recipeTypeMap.put(rr.getClass().getName(), Integer.valueOf(recipeTypeMap.containsKey(rr.getClass().getName()) ? ((Integer)recipeTypeMap.get(rr.getClass().getName())).intValue() + 1 : 1));
@ -392,10 +475,16 @@ public class Template
setBreedingTable();
randomMelonItem = new ItemStack(Material.getMaterial(Template.archivedshufflelist.get(Template.r.nextInt(Template.archivedshufflelist.size()))));
for (String s : recipeTypeMap.keySet()) {
Bukkit.getLogger().info(" Randomized " + recipeTypeMap.get(s) + " " + s + " recipes.");
}
Bukkit.getLogger().info("");
Bukkit.getLogger().info("Blasting: "+blastingrecipe);
Bukkit.getLogger().info("Campefire: "+campfirerecipe);
Bukkit.getLogger().info("Smoking: "+smokingrecipe);
Bukkit.getLogger().info("Stonecutting: "+stonecuttingrecipe);
PluginManager pm = getServer().getPluginManager();
@ -405,4 +494,30 @@ public class Template
finished=true;
}
public static void LoadVillagerCount() {
File f = new File("villager_count");
if (f.exists()) {
try {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
villager_count = Integer.parseInt(br.readLine());
Bukkit.getLogger().info(villager_count+" Villagers loaded.");
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void IncreaseAndSaveVillagerCount() {
File f = new File("villager_count");
try {
FileWriter fw = new FileWriter(f);
fw.write((++villager_count)+"");
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

@ -28,6 +28,7 @@ import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.event.inventory.FurnaceSmeltEvent;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Merchant;
import org.bukkit.inventory.MerchantRecipe;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.RecipeChoice;
@ -61,18 +62,18 @@ public class ExampleListener implements Listener {
public String GenerateRandomName() {
StringBuilder name = new StringBuilder();
boolean consonant=Template.r.nextInt(2)==0;
int strlength = Template.r.nextInt(Template.randomizeAmount(2)+5);
boolean first=false;
Random namer = new Random(Template.villager_count);
boolean consonant=namer.nextInt(2)==0;
int strlength = namer.nextInt(Template.randomizeAmount(namer,2)+5)+1;
while ((strlength--)>0) {
if (consonant) {
String letter = consonants[Template.r.nextInt(consonants.length)];
String letter = consonants[namer.nextInt(consonants.length)];
name.append(letter);
if (Template.r.nextInt(4)==0 && Math.abs(name.length()-strlength)<=3) {
if (namer.nextInt(4)==0 && Math.abs(name.length()-strlength)<=3) {
name.append(letter);
}
} else {
name.append(vowels[Template.r.nextInt(vowels.length)]);
name.append(vowels[namer.nextInt(vowels.length)]);
}
consonant=!consonant;
}
@ -88,8 +89,14 @@ public class ExampleListener implements Listener {
//Bukkit.getLogger().info(ev.getEntity()+" getting "+ev.getRecipe());
if (ev.getEntity().getCustomName()==null || ev.getEntity().getCustomName().length()==0) {
ev.getEntity().setCustomName(GenerateRandomName());
Template.IncreaseAndSaveVillagerCount();
}
Merchant mm = (Merchant)ev.getEntity();
//Bukkit.getLogger().info(""+mm.getRecipeCount());
Random ff = new Random(ev.getEntity().getCustomName().hashCode());
for (int i=0;i<mm.getRecipeCount();i++) {
ff.nextInt();
}
MerchantRecipe prev = ev.getRecipe();
MerchantRecipe newRecipe = new MerchantRecipe(new ItemStack(Material.getMaterial(Template.archivedshufflelist.get(ff.nextInt(Template.archivedshufflelist.size()))),Template.randomizeAmount(ff,2)*Template.randomizeAmount(ff,2)),
prev.getUses(),prev.getMaxUses(),true,prev.getVillagerExperience(),prev.getPriceMultiplier());
@ -132,11 +139,20 @@ public class ExampleListener implements Listener {
Bukkit.getLogger().info("entloc: "+ent.getLocation());
Bukkit.getLogger().info("template_breedingtable: "+Template.breedingTable);
Bukkit.getLogger().info("enttype: "+ent.getType());
Entity baby = ent.getWorld().spawnEntity(ent.getLocation(), Template.breedingTable.get(ent.getType()));
Bukkit.getLogger().info("baby: "+baby);
if (baby instanceof Ageable) {
Ageable baby_ent = (Ageable)baby;
baby_ent.setBaby();
if (Template.breedingTable.containsKey(ent.getType())) {
Entity baby = ent.getWorld().spawnEntity(ent.getLocation(), Template.breedingTable.get(ent.getType()));
Bukkit.getLogger().info("baby: "+baby);
if (baby!=null && baby instanceof Ageable) {
Ageable baby_ent = (Ageable)baby;
baby_ent.setBaby();
}
} else {
Entity baby = ent.getWorld().spawnEntity(ent.getLocation(), ent.getType());
Bukkit.getLogger().info("baby: "+baby);
if (baby!=null && baby instanceof Ageable) {
Ageable baby_ent = (Ageable)baby;
baby_ent.setBaby();
}
}
}

Loading…
Cancel
Save