Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field pests #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ realistic_biomes:
# the relative z-level at which to start looking for soil blocks, in this example the column will start
# 2 blocks below the crop block, ie beneath both itself and the farmland block
soil_layer_offset: 2
pest_probability: 0.001

# wheat
CROPS:
Expand Down
13 changes: 13 additions & 0 deletions src/com/untamedears/realisticbiomes/GrowthConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.logging.Logger;

import org.bukkit.Material;
Expand Down Expand Up @@ -43,6 +44,8 @@ public class GrowthConfig extends BaseConfig {
private double soilBonusPerLevel;
// the z levels below the actual growth event location in which to start looking for the correct soil
private int soilLayerOffset;

private double pestProbability;

// conversion used for persistence calculations
private static final int SEC_PER_HOUR = 60 * 60;
Expand Down Expand Up @@ -97,6 +100,8 @@ public static GrowthConfig get(ConfigurationSection conf, GrowthConfig parent, M
soilMaxLayers = 0;
soilBonusPerLevel = 0.0;
soilLayerOffset = 1;

pestProbability = 0.0;
}

// make a copy of the given configuration
Expand Down Expand Up @@ -154,6 +159,9 @@ public static GrowthConfig get(ConfigurationSection conf, GrowthConfig parent, M
if (config.isSet("soil_layer_offset"))
soilLayerOffset = config.getInt("soil_layer_offset");

if (config.isSet("pest_probability"))
pestProbability = config.getDouble("pest_probability");

if (config.isSet("biomes"))
loadBiomes(config.getConfigurationSection("biomes"), biomeAliases);
}
Expand Down Expand Up @@ -273,4 +281,9 @@ public double getRate(Block block) {

return rate;
}

public boolean testPest() {
Random gen = new Random();
return gen.nextDouble() < pestProbability;
}
}
2 changes: 2 additions & 0 deletions src/com/untamedears/realisticbiomes/RealisticBiomes.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

import com.untamedears.realisticbiomes.listener.FieldPestListener;
import com.untamedears.realisticbiomes.listener.GrowListener;
import com.untamedears.realisticbiomes.listener.PlayerListener;
import com.untamedears.realisticbiomes.listener.SpawnListener;
Expand Down Expand Up @@ -344,6 +345,7 @@ private void registerEvents() {
pm.registerEvents(new GrowListener(this, materialGrowth), this);
pm.registerEvents(new SpawnListener(materialGrowth, fishDrops), this);
pm.registerEvents(new PlayerListener(this, materialGrowth), this);
pm.registerEvents(new FieldPestListener(this, materialGrowth), this);
}
catch(Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.untamedears.realisticbiomes.listener;

import java.util.Map;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Silverfish;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.potion.PotionEffectType;

import com.untamedears.realisticbiomes.GrowthConfig;
import com.untamedears.realisticbiomes.RealisticBiomes;

public class FieldPestListener implements Listener {
private RealisticBiomes plugin;
private Map<Object, GrowthConfig> growthConfigs;

public FieldPestListener(RealisticBiomes plugin, Map<Object, GrowthConfig> growthConfigs) {
this.plugin = plugin;
this.growthConfigs = growthConfigs;
}

@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
// right click on a growing crop with a stick: get information about that crop
Block block = event.getBlock();
Material material = block.getType();

GrowthConfig growthConfig = growthConfigs.get(material);
if (growthConfig != null && growthConfig.testPest()) {
// Spawn a relatively harmless field pest

Silverfish pest = (Silverfish) event.getBlock().getWorld().spawnEntity(block.getLocation(), EntityType.SILVERFISH);
pest.setHealth(1.0);
pest.addPotionEffect(PotionEffectType.SLOW.createEffect(20 * 60 * 15, 1));
pest.addPotionEffect(PotionEffectType.WEAKNESS.createEffect(20 * 60 * 15, 1));
if (event.getPlayer() != null) {
pest.setTarget(event.getPlayer());
}
}
}
}