summaryrefslogtreecommitdiff
path: root/src/main/java/com/encrox/zombie/Map.java
blob: 8262ba78b823dde460e6fd85d08fe6cbe60d1794 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.encrox.zombie;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.BlockVector;
import org.json.JSONArray;
import org.json.JSONObject;

import com.encrox.zombie.interactable.Chest;
import com.encrox.zombie.interactable.Lever;
import com.encrox.zombie.interactable.Powerup;

public class Map {
	
	private Schematic schematic;
	private String name;
	private int players;
	private BlockVector spawn;
	private Chest[] chests;
	private Mob[] mobs;
	private Powerup[] powerups;
	private Lever[] levers;
	
	public Map(JSONObject descriptor) {
		this.schematic = new Schematic(new File(Zombie.schematicsDirectory, descriptor.getString("file")));
		this.name = descriptor.getString("name");
		this.players = descriptor.getInt("players");
		JSONArray spawnArray = descriptor.getJSONArray("spawn");
		this.spawn = new BlockVector(spawnArray.getInt(0), spawnArray.getInt(1), spawnArray.getInt(2));
		JSONArray leversArray = descriptor.getJSONArray("levers");
		levers = new Lever[leversArray.length()];
		for(int i = 0; i<levers.length; i++) {
			JSONObject lever = leversArray.getJSONObject(i);
			JSONArray position = lever.getJSONArray("position");
			JSONArray othersLever = lever.getJSONArray("toggle");
			Lever[] others = new Lever[othersLever.length()];
			for(int x = 0; x<others.length; x++)
				others[x] = levers[othersLever.getInt(x)];
			levers[i] = new Lever(new BlockVector(position.getInt(0), position.getInt(1), position.getInt(2)), lever.getInt("cost"), others);
		}
		JSONArray mobsArray = descriptor.getJSONArray("mobs");
		mobs = new Mob[mobsArray.length()];
		for(int i = 0; i<mobs.length; i++) {
			JSONObject mob = mobsArray.getJSONObject(i);
			JSONArray position = mob.getJSONArray("position");
			JSONArray leversMob = mob.getJSONArray("levers");
			Lever[] levers = new Lever[leversMob.length()];
			for(int x = 0; x<levers.length; x++)
				levers[x] = this.levers[leversMob.getInt(x)];
			mobs[i] = new Mob(EntityType.fromId(mob.getInt("type")), new BlockVector(position.getInt(0), position.getInt(1), position.getInt(2)), mob.getString("rounds"), levers);
		}
		JSONArray chestsArray = descriptor.getJSONArray("chests");
		chests = new Chest[chestsArray.length()];
		for(int i = 0; i<chests.length; i++) {
			JSONObject chest = chestsArray.getJSONObject(i);
			JSONArray position = chest.getJSONArray("position");
			chests[i] = new Chest(new BlockVector(position.getInt(0), position.getInt(1), position.getInt(2)), chest.getInt("cost"), chest.getString("facing"));
		}
		JSONArray powerupsArray = descriptor.getJSONArray("powerups");
		powerups = new Powerup[powerupsArray.length()];
		for(int i = 0; i<powerups.length; i++) {
			JSONObject powerup = powerupsArray.getJSONObject(i);
			JSONArray position = powerup.getJSONArray("position");
			JSONObject enchantment = powerup.getJSONObject("enchantment");
			ItemStack item = new ItemStack(Material.getMaterial(powerup.getString("item")));
			String enchStr = enchantment.getString("type");
			if(!enchStr.equals("")) {
				Enchantment ench = Enchantment.getByName(enchStr);
				if(ench.canEnchantItem(item)) {
					item.addEnchantment(ench, enchantment.getInt("level"));
				} else {
					Zombie.logger.warning(Zombie.lang.getProperty("plugin_enchantment"));
				}
			}
			powerups[i] = new Powerup(new BlockVector(position.getInt(0), position.getInt(1), position.getInt(2)), item, powerup.getInt("cost"));
		}
	}
	
	public int getPlayers() {
		return players;
	}
	
	public String getName() {
		return name;
	}
	
	public Schematic getSchematic() {
		return schematic;
	}
	
	public BlockVector getSpawn() {
		return spawn;
	}
	
	public Mob[] getMobs() {
		return mobs;
	}
	
	public Chest[] getChests() {
		return chests;
	}
	
	public Powerup[] getPowerups() {
		return powerups;
	}
	
	public Lever[] getLevers() {
		return levers;
	}

}