mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-12-17 16:49:06 +00:00
Added maven framework support. Started working on NPCs #19
This commit is contained in:
@@ -1,31 +1,13 @@
|
||||
package net.simon987.plantplugin;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.game.GameObject;
|
||||
import net.simon987.server.game.InventoryHolder;
|
||||
import net.simon987.server.game.Updatable;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class Plant extends GameObject implements Updatable, InventoryHolder{
|
||||
|
||||
public class Plant extends GameObject implements InventoryHolder {
|
||||
private static final char MAP_INFO = 0x4000;
|
||||
public static final int ID = 2;
|
||||
|
||||
/**
|
||||
* Grow time (see config.properties)
|
||||
*/
|
||||
private static final int GROW_TIME = GameServer.INSTANCE.getConfig().getInt("plant_grow_time");
|
||||
|
||||
/**
|
||||
* Game time of the creation of this Plant
|
||||
*/
|
||||
private long creationTime;
|
||||
|
||||
/**
|
||||
* Whether the plant is grown or not
|
||||
*/
|
||||
private boolean grown;
|
||||
|
||||
/**
|
||||
* Yield of the plant, in biomass units
|
||||
*/
|
||||
@@ -51,43 +33,12 @@ public class Plant extends GameObject implements Updatable, InventoryHolder{
|
||||
json.put("id", getObjectId());
|
||||
json.put("x", getX());
|
||||
json.put("y", getY());
|
||||
json.put("creationTime", creationTime);
|
||||
json.put("grown", grown);
|
||||
json.put("biomassCount", biomassCount);
|
||||
json.put("style", style);
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called every tick
|
||||
*/
|
||||
@Override
|
||||
public void update() {
|
||||
if (!grown) {
|
||||
//Check grow
|
||||
if (creationTime + GROW_TIME <= GameServer.INSTANCE.getGameUniverse().getTime()) {
|
||||
grown = true;
|
||||
biomassCount = GameServer.INSTANCE.getConfig().getInt("plant_yield");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public long getCreationTime() {
|
||||
return creationTime;
|
||||
}
|
||||
|
||||
public void setCreationTime(long creationTime) {
|
||||
this.creationTime = creationTime;
|
||||
}
|
||||
|
||||
public boolean isGrown() {
|
||||
return grown;
|
||||
}
|
||||
|
||||
public void setGrown(boolean grown) {
|
||||
this.grown = grown;
|
||||
}
|
||||
|
||||
public int getBiomassCount() {
|
||||
return biomassCount;
|
||||
@@ -105,17 +56,15 @@ public class Plant extends GameObject implements Updatable, InventoryHolder{
|
||||
this.style = style;
|
||||
}
|
||||
|
||||
public static Plant deserialize(JSONObject json){
|
||||
public static Plant deserialize(JSONObject json) {
|
||||
|
||||
Plant plant = new Plant();
|
||||
|
||||
plant.setObjectId((int)(long)json.get("id"));
|
||||
plant.setX((int)(long)json.get("x"));
|
||||
plant.setY((int)(long)json.get("y"));
|
||||
plant.grown = (boolean)json.get("grown");
|
||||
plant.creationTime = (long)json.get("creationTime");
|
||||
plant.style = (int)(long)json.get("style");
|
||||
plant.biomassCount = (int)(long)json.get("biomassCount");
|
||||
plant.setObjectId((int) (long) json.get("id"));
|
||||
plant.setX((int) (long) json.get("x"));
|
||||
plant.setY((int) (long) json.get("y"));
|
||||
plant.style = (int) (long) json.get("style");
|
||||
plant.biomassCount = (int) (long) json.get("biomassCount");
|
||||
|
||||
return plant;
|
||||
}
|
||||
@@ -134,7 +83,7 @@ public class Plant extends GameObject implements Updatable, InventoryHolder{
|
||||
|
||||
@Override
|
||||
public boolean canTakeItem(int item) {
|
||||
return item == ITM_BIOMASS && grown && biomassCount >= 1;
|
||||
return item == ITM_BIOMASS && biomassCount >= 1;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -148,9 +97,9 @@ public class Plant extends GameObject implements Updatable, InventoryHolder{
|
||||
public void takeItem(int item) {
|
||||
|
||||
if (item == ITM_BIOMASS) {
|
||||
if (grown && biomassCount > 1) {
|
||||
if (biomassCount > 1) {
|
||||
biomassCount--;
|
||||
} else if (grown) {
|
||||
} else {
|
||||
//Delete plant
|
||||
setDead(true);
|
||||
}
|
||||
@@ -10,7 +10,6 @@ import org.json.simple.JSONObject;
|
||||
public class PlantPlugin extends ServerPlugin implements GameObjectDeserializer {
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
listeners.add(new WorldCreationListener());
|
||||
@@ -20,9 +19,9 @@ public class PlantPlugin extends ServerPlugin implements GameObjectDeserializer
|
||||
@Override
|
||||
public GameObject deserializeObject(JSONObject object) {
|
||||
|
||||
int objType = (int)(long)object.get("type");
|
||||
int objType = (int) (long) object.get("type");
|
||||
|
||||
if(objType == Plant.ID) {
|
||||
if (objType == Plant.ID) {
|
||||
|
||||
return Plant.deserialize(object);
|
||||
}
|
||||
@@ -21,9 +21,9 @@ public class WorldCreationListener implements GameEventListener {
|
||||
@Override
|
||||
public void handle(GameEvent event) {
|
||||
|
||||
ArrayList<Plant> plants = generatePlants(((WorldGenerationEvent)event).getWorld());
|
||||
ArrayList<Plant> plants = generatePlants(((WorldGenerationEvent) event).getWorld());
|
||||
|
||||
((WorldGenerationEvent)event).getWorld().getGameObjects().addAll(plants);
|
||||
((WorldGenerationEvent) event).getWorld().getGameObjects().addAll(plants);
|
||||
|
||||
}
|
||||
|
||||
@@ -79,7 +79,6 @@ public class WorldCreationListener implements GameEventListener {
|
||||
plant.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
|
||||
plant.setStyle(0); //TODO: set style depending on difficulty level? or random? from config?
|
||||
plant.setBiomassCount(plant_yield);
|
||||
plant.setCreationTime(0); // Plants generated by the world generator always have creationTime of 0
|
||||
plant.setX(p.x);
|
||||
plant.setY(p.y);
|
||||
plant.setWorld(world);
|
||||
Reference in New Issue
Block a user