Browse Source

refactoring: created new method getRandomItem()

remotes/origin/Julia
Juliakn66 2 years ago
parent
commit
2d13de8429
  1. 43
      src/main/java/org/bitbiome/commands/LookaroundCommand.java

43
src/main/java/org/bitbiome/commands/LookaroundCommand.java

@ -33,27 +33,8 @@ public class LookaroundCommand implements CommandAPI{
foundItems.removeAll(foundItems); foundItems.removeAll(foundItems);
ArrayList<Mob> foundMobs = location.getMobList(); ArrayList<Mob> foundMobs = location.getMobList();
foundMobs.removeAll(foundMobs); foundMobs.removeAll(foundMobs);
foundItems = getRandomItem(randomNumberItems, random, items, foundItems);
for (int i=0; i<randomNumberItems; i++){
String s1 = items.getString(random.nextInt(items.length()));
String resourceName = "./../../../items.json";
InputStream is = JsonParser.class.getResourceAsStream(resourceName);
if (is == null) {
throw new NullPointerException("Cannot find resource file " + resourceName);
}
JSONTokener tokener = new JSONTokener(is);
JSONArray jp3 = new JSONArray(tokener);
JSONObject jp2= jp3.getJSONObject(0);
for (int j=1; j<jp3.length(); j++ ){
if(jp3.getJSONObject(j).getString("name").equals(s1)){
jp2 = jp3.getJSONObject(j);
break;
}
}
Item randomItem = new Item (jp2.getString("name"),jp2.getBoolean("doesDamage"),jp2.getFloat("damage"),1);
foundItems.add(randomItem);
}
for (int i=0; i<randomNumberMobs; i++){ for (int i=0; i<randomNumberMobs; i++){
JSONObject jp2 = mobs.getJSONObject(random.nextInt(mobs.length())); JSONObject jp2 = mobs.getJSONObject(random.nextInt(mobs.length()));
Mob randomMob = new Mob (jp2.getString("name"),jp2.getBoolean("isFriendly"),jp2.getFloat("hp"),jp2.getFloat("damage")); Mob randomMob = new Mob (jp2.getString("name"),jp2.getBoolean("isFriendly"),jp2.getFloat("hp"),jp2.getFloat("damage"));
@ -103,6 +84,28 @@ public class LookaroundCommand implements CommandAPI{
} }
return locations.getJSONObject(0); return locations.getJSONObject(0);
} }
public ArrayList<Item> getRandomItem(int randomNumberItems, Random random, JSONArray items, ArrayList<Item> foundItems ) {
for (int i=0; i<randomNumberItems; i++){
String s1 = items.getString(random.nextInt(items.length()));
String resourceName = "./../../../items.json";
InputStream is = JsonParser.class.getResourceAsStream(resourceName);
if (is == null) {
throw new NullPointerException("Cannot find resource file " + resourceName);
}
JSONTokener tokener = new JSONTokener(is);
JSONArray jp3 = new JSONArray(tokener);
JSONObject jp2= jp3.getJSONObject(0);
for (int j=1; j<jp3.length(); j++ ){
if(jp3.getJSONObject(j).getString("name").equals(s1)){
jp2 = jp3.getJSONObject(j);
break;
}
}
Item randomItem = new Item (jp2.getString("name"),jp2.getBoolean("doesDamage"),jp2.getFloat("damage"),1);
foundItems.add(randomItem);
}
return foundItems;
}
} }
Loading…
Cancel
Save