Skip to content

Commit

Permalink
Merge pull request #2 from Namrufus/master
Browse files Browse the repository at this point in the history
Pull latest
  • Loading branch information
erocs committed May 14, 2013
2 parents db09841 + 36c4cd2 commit 247d3a9
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 98 deletions.
Binary file modified RealisticBiomes.jar
Binary file not shown.
Binary file modified bin/com/untamedears/realisticbiomes/RealisticBiomes.class
Binary file not shown.
Binary file modified bin/com/untamedears/realisticbiomes/listener/GrowListener.class
Binary file not shown.
Binary file modified bin/com/untamedears/realisticbiomes/persist/PlantChunk.class
Binary file not shown.
Binary file modified bin/com/untamedears/realisticbiomes/persist/PlantManager$1.class
Binary file not shown.
Binary file modified bin/com/untamedears/realisticbiomes/persist/PlantManager.class
Binary file not shown.
Binary file modified bin/com/untamedears/realisticbiomes/persist/WorldID.class
Binary file not shown.
9 changes: 6 additions & 3 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ realistic_biomes:
crop:
# the base rate of this plant as a fraction of the standard rate
base_rate: 0.015
# the rate at which plants will grow in an artificial environment, disregarding all other factors
# greenhouse is represented by immediate proximity of a glowstone block with light level 14
greenhouse_rate: 0.12
# if this flag is true, the growth rate of the grower is modulated by sunlight levels
# ie sunlight level = 15 means full growth, while sunlight level 0 means 0 growth
# sunlight level is disregarded is this flag is false
Expand Down Expand Up @@ -63,7 +66,7 @@ realistic_biomes:
# is unloaded or the server is off. It sets the time to grow to maturity in units of real-life hours
# if this parameter is set, then the base_rate is ignored, though the other multipliers will still be
# in effect
persistent_growth_period: 24
persistent_growth_period: 12
biomes:
cold: 0.25
hot_dry: 0.25
Expand All @@ -73,7 +76,7 @@ realistic_biomes:

CARROT:
inherit: crop
persistent_growth_period: 24
persistent_growth_period: 12
biomes:
hot_dry: 0.1
hot_rainy: 1.0
Expand All @@ -83,7 +86,7 @@ realistic_biomes:

POTATO:
inherit: crop
persistent_growth_period: 24
persistent_growth_period: 12
biomes:
mountain: 1.0
cold: 0.25
Expand Down
44 changes: 43 additions & 1 deletion src/com/untamedears/realisticbiomes/GrowthConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.untamedears.realisticbiomes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -9,13 +10,17 @@
import org.bukkit.block.Biome;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.util.Vector;

public class GrowthConfig {
// a rate of growth between 0 and 1
// this usually represents a chance,
// for a crop, it would be the chance to grow per tick
// for an animal, it would be the chance to spawn after mating
private double baseRate;
// this rate overrides all other settings if the plant is under artificial light (adjacent to glowstone)
private double greenhouseRate;
private boolean isGreenhouseEnabled;

// flag that denotes if this crop's growth is persisted
private boolean isPersistent;
Expand All @@ -36,13 +41,26 @@ public class GrowthConfig {
// map from biome to the modulated growth rate per biome
private Map<Biome, Double> biomeMultipliers;

// conversion used for persistence calculations
private static final int MS_PER_HOUR = 1000 * 60 * 60;

// ------------------------------------------------------------------------

public static Logger LOG = Logger.getLogger("RealisticBiomes");

// ========================================================================
// Initialization

// relative locations of visible adjacent blocks
@SuppressWarnings("serial")
private static List<Vector> adjacentBlocks = new ArrayList<Vector>(){{
this.add(new Vector(0,1,0)); // up
this.add(new Vector(-1,0,0)); // west
this.add(new Vector(1,0,0)); // east
this.add(new Vector(0,0,-1)); // north
this.add(new Vector(0,0,1)); // south
}};

public static GrowthConfig get(ConfigurationSection conf, GrowthConfig parent, Map<String, Biome[]>biomeAliases) {
GrowthConfig growth = new GrowthConfig(parent);

Expand All @@ -52,6 +70,8 @@ public static GrowthConfig get(ConfigurationSection conf, GrowthConfig parent, M
// create a new default configuration
GrowthConfig() {
baseRate = 1.0;
greenhouseRate = 0.66;
isGreenhouseEnabled = false;
isPersistent = false;

needsSunlight = false;
Expand All @@ -68,6 +88,8 @@ public static GrowthConfig get(ConfigurationSection conf, GrowthConfig parent, M
// make a copy of the given configuration
GrowthConfig(GrowthConfig parent) {
baseRate = parent.baseRate;
greenhouseRate = parent.greenhouseRate;
isGreenhouseEnabled = parent.isGreenhouseEnabled;

needsSunlight = parent.needsSunlight;
openSkyBonus = parent.openSkyBonus;
Expand All @@ -87,6 +109,11 @@ public static GrowthConfig get(ConfigurationSection conf, GrowthConfig parent, M
if (config.isSet("base_rate"))
baseRate = config.getDouble("base_rate");

if (config.isSet("greenhouse_rate")) {
isGreenhouseEnabled = true;
greenhouseRate = config.getDouble("greenhouse_rate");
}

isPersistent = false;
if (config.isSet("persistent_growth_period")) {
isPersistent = true;
Expand Down Expand Up @@ -153,11 +180,26 @@ public boolean isPersistent() {

// given a block (a location), find the growth rate using these rules
public double getRate(Block block) {

// if it's fully lit, use greenhouse rate and disregard everything else
if(isGreenhouseEnabled && block.getLightFromBlocks() == 14) {
// make sure it's a glowstone/lamp
for( Vector vec : adjacentBlocks ) {
Material mat = block.getLocation().add(vec).getBlock().getType();
if( mat == Material.GLOWSTONE || mat == Material.REDSTONE_LAMP_ON ) {
if (isPersistent) {
return 1.0 / (greenhouseRate * persistentRate * MS_PER_HOUR);
}
return greenhouseRate;
}
}
}

// rate = baseRate * sunlightLevel * biome * (1.0 + soilBonus)
double rate = baseRate;
// if persistent, the growth rate is measured in growth/millisecond
if (isPersistent)
rate = 1.0 / (persistentRate * (1000.0*60.0*60.0/*milliseconds per hour*/));
rate = 1.0 / (persistentRate * MS_PER_HOUR);

// modulate the rate by the amount of sunlight recieved by this plant
if (needsSunlight) {
Expand Down
8 changes: 3 additions & 5 deletions src/com/untamedears/realisticbiomes/RealisticBiomes.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
import java.util.List;
import java.util.logging.Logger;

import org.bukkit.Chunk;
import org.bukkit.Material;
import org.bukkit.TreeType;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.entity.EntityType;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

import com.untamedears.realisticbiomes.listener.GrowListener;
import com.untamedears.realisticbiomes.listener.PlayerListener;
Expand Down Expand Up @@ -202,7 +200,7 @@ else if (materialName.startsWith("entity_")) {

public void onDisable() {
LOG.info("[RealisticBiomes] saving plant growth data.");
plantManager.saveAll();
plantManager.saveAllAndStop();
plantManager = null;
LOG.info("[RealisticBiomes] is now disabled.");
}
Expand Down
16 changes: 13 additions & 3 deletions src/com/untamedears/realisticbiomes/listener/GrowListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.TreeType;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockGrowEvent;
Expand Down Expand Up @@ -67,15 +68,21 @@ public void onBlockGrow(BlockGrowEvent event) {
* Event handler for {@link StructureGrowEvent}. Checks tree growth for proper conditions
* @param event The {@link StructureGrowEvent} being handled
*/
@EventHandler(ignoreCancelled = true)
@EventHandler(priority = EventPriority.HIGH)
public void onStructureGrow(StructureGrowEvent event) {
// disable bonemeal
if (event.isFromBonemeal()) {
event.setCancelled(true);
return;
}

TreeType t = event.getSpecies();
Block b = event.getLocation().getBlock();
event.setCancelled(!willGrow(t, b));
}

/**
* Event handler for {@link PlayerInteractEvent}. Cancels all uses of Bonemeal as an item.
* Event handler for {@link PlayerInteractEvent}. Cancels all uses of Bonemeal as an item on crops registered in the config.
* @param event The {@link PlayerInteractEvent} being handled
*/
@EventHandler(ignoreCancelled = true)
Expand All @@ -85,7 +92,10 @@ public void onPlayerInteract(PlayerInteractEvent event) {
ItemStack item = event.getPlayer().getItemInHand();
// Ink Sack with data 15 == Bone Meal
if (item.getTypeId() == 351 && item.getData().getData() == 15) {
event.setCancelled(true);
Material material = event.getClickedBlock().getType();
if (material != Material.SAPLING && growthMap.containsKey(material)) {
event.setCancelled(true);
}
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/com/untamedears/realisticbiomes/persist/PlantChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class PlantChunk {
private static PreparedStatement savePlantsStmt = null;
private static PreparedStatement getLastChunkIdStmt = null;

public PlantChunk(RealisticBiomes plugin, Connection conn, int index) {
public PlantChunk(RealisticBiomes plugin, Connection readConn, Connection writeConn, int index) {
this.plugin = plugin;
plants = null;
this.index = index;
Expand All @@ -40,16 +40,16 @@ public PlantChunk(RealisticBiomes plugin, Connection conn, int index) {

if (deleteOldDataStmt == null) {
try {
deleteOldDataStmt = conn.prepareStatement("DELETE FROM plant WHERE chunkid = ?1");
deleteOldDataStmt = writeConn.prepareStatement("DELETE FROM plant WHERE chunkid = ?1");

loadPlantsStmt = conn.prepareStatement("SELECT w, x, y, z, date, growth FROM plant WHERE chunkid = ?1");
loadPlantsStmt = readConn.prepareStatement("SELECT w, x, y, z, date, growth FROM plant WHERE chunkid = ?1");

addChunkStmt = conn.prepareStatement("INSERT INTO chunk (w, x, z) VALUES (?, ?, ?)");
getLastChunkIdStmt = conn.prepareStatement("SELECT last_insert_rowid()");
addChunkStmt = writeConn.prepareStatement("INSERT INTO chunk (w, x, z) VALUES (?, ?, ?)");
getLastChunkIdStmt = writeConn.prepareStatement("SELECT last_insert_rowid()");

savePlantsStmt = conn.prepareStatement("INSERT INTO plant (chunkid, w, x, y, z, date, growth) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)");
savePlantsStmt = writeConn.prepareStatement("INSERT INTO plant (chunkid, w, x, y, z, date, growth) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)");

deleteChunkStmt = conn.prepareStatement("DELETE FROM chunk WHERE id = ?1");
deleteChunkStmt = writeConn.prepareStatement("DELETE FROM chunk WHERE id = ?1");
} catch (SQLException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -91,7 +91,7 @@ public Plant get(Coords coords) {
return plants.get(coords);
}

public boolean load(Connection conn, Coords coords) {
public boolean load(Coords coords) {
// if the data is being loaded, it is known that this chunk is in the database
inDatabase = true;

Expand Down Expand Up @@ -145,7 +145,7 @@ public boolean load(Connection conn, Coords coords) {
return true;
}

public void unload(Connection conn, Coords chunkCoords) {
public void unload(Coords chunkCoords) {
if (!loaded)
return;

Expand Down
Loading

0 comments on commit 247d3a9

Please sign in to comment.