Add dispenser tree planting.

master
kalle 2020-08-18 12:16:45 +02:00
parent c5a3944b48
commit 52814d05d5
3 changed files with 85 additions and 5 deletions

View File

@ -0,0 +1,75 @@
package nl.kallestruik.vanillatweaks.DispenserTweaks;
import nl.kallestruik.vanillatweaks.config;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.type.Dispenser;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockDispenseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayList;
import java.util.List;
public class DispenserTweaksHandler implements Listener {
private static List<Material> saplings = new ArrayList<>();
private static List<Material> treePlantable = new ArrayList<>();
public DispenserTweaksHandler() {
saplings.add(Material.OAK_SAPLING);
saplings.add(Material.BIRCH_SAPLING);
saplings.add(Material.JUNGLE_SAPLING);
saplings.add(Material.SPRUCE_SAPLING);
saplings.add(Material.DARK_OAK_SAPLING);
saplings.add(Material.ACACIA_SAPLING);
treePlantable.add(Material.DIRT);
treePlantable.add(Material.GRASS_BLOCK);
treePlantable.add(Material.COARSE_DIRT);
treePlantable.add(Material.PODZOL);
}
@EventHandler
public void onDispenserDispense(BlockDispenseEvent event) {
if (event.getBlock().getType() != Material.DISPENSER)
return;
if (config.DISPENSERS_CAN_PLACE_SAPLINGS) {
if (saplings.contains(event.getItem().getType())) {
Dispenser dispenser = ((Dispenser) event.getBlock().getBlockData());
BlockFace facing = dispenser.getFacing();
Block targetBlock;
if (facing == BlockFace.UP) {
targetBlock = event.getBlock().getWorld().getBlockAt(event.getBlock().getLocation().add(0, 2, 0));
} else {
targetBlock = event.getBlock().getWorld().getBlockAt(event.getBlock().getLocation().add(facing.getModX(), facing.getModY(), facing.getModZ()));
}
if (targetBlock.getType() == Material.AIR
&& treePlantable.contains(event.getBlock().getWorld().getBlockAt(targetBlock.getLocation().add(0, -1, 0)).getType())) {
targetBlock.setType(event.getItem().getType());
event.setCancelled(true);
Inventory dispenserInventory = ((org.bukkit.block.Dispenser) event.getBlock().getState()).getInventory();
int slot = 0;
for (ItemStack is : dispenserInventory.getContents()) {
if (is != null && is.getType() == event.getItem().getType())
break;
slot++;
}
ItemStack newItemStack = dispenserInventory.getItem(slot);
newItemStack.setAmount(newItemStack.getAmount() - 1);
dispenserInventory.setItem(slot, newItemStack);
event.getBlock().getWorld().spawnParticle(Particle.VILLAGER_HAPPY, targetBlock.getLocation(), 5);
}
}
}
}
}

View File

@ -2,6 +2,7 @@ package nl.kallestruik.vanillatweaks;
import nl.kallestruik.vanillatweaks.ArmorStandSwapping.ArmorStandSwappingHandler; import nl.kallestruik.vanillatweaks.ArmorStandSwapping.ArmorStandSwappingHandler;
import nl.kallestruik.vanillatweaks.CraftingTweaks.CraftingTweaks; import nl.kallestruik.vanillatweaks.CraftingTweaks.CraftingTweaks;
import nl.kallestruik.vanillatweaks.DispenserTweaks.DispenserTweaksHandler;
import nl.kallestruik.vanillatweaks.HoeHarvesting.HoeHarvestingHandler; import nl.kallestruik.vanillatweaks.HoeHarvesting.HoeHarvestingHandler;
import nl.kallestruik.vanillatweaks.LilypadGrowing.LilypadGrowingHandler; import nl.kallestruik.vanillatweaks.LilypadGrowing.LilypadGrowingHandler;
import nl.kallestruik.vanillatweaks.NetherSpongeDrying.NetherSpongeHandler; import nl.kallestruik.vanillatweaks.NetherSpongeDrying.NetherSpongeHandler;
@ -19,7 +20,7 @@ public final class VanillaTweaks extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
try { try {
// Config loading // Load the config from disk.
config.load(new File(this.getDataFolder(), c.CONFIG_FILE_NAME)); config.load(new File(this.getDataFolder(), c.CONFIG_FILE_NAME));
if (config.TOGGLE_TRAMPLE_ENABLED) { if (config.TOGGLE_TRAMPLE_ENABLED) {
@ -52,10 +53,10 @@ public final class VanillaTweaks extends JavaPlugin {
getServer().getPluginManager().registerEvents(new LilypadGrowingHandler(), this); getServer().getPluginManager().registerEvents(new LilypadGrowingHandler(), this);
} }
getServer().getPluginManager().registerEvents(new DispenserTweaksHandler(), this);
} catch (IOException | InvalidConfigurationException | NullPointerException e) { } catch (IOException | InvalidConfigurationException | NullPointerException e) {
util.printException(e); Util.printException(e);
} }
} }
@ -64,7 +65,7 @@ public final class VanillaTweaks extends JavaPlugin {
try { try {
TrampleHandler.saveTrampleEnabled(new File(this.getDataFolder(), c.TRAMPLE_ENABLED_FILE_NAME)); TrampleHandler.saveTrampleEnabled(new File(this.getDataFolder(), c.TRAMPLE_ENABLED_FILE_NAME));
} catch (IOException | InvalidConfigurationException | NullPointerException e) { } catch (IOException | InvalidConfigurationException | NullPointerException e) {
util.printException(e); Util.printException(e);
} }
} }
} }

View File

@ -38,11 +38,13 @@ public class config {
public static boolean LILYPAD_GROWING_ENABLED; public static boolean LILYPAD_GROWING_ENABLED;
public static boolean DISPENSERS_CAN_PLACE_SAPLINGS;
public static void load(File file) throws IOException, InvalidConfigurationException { public static void load(File file) throws IOException, InvalidConfigurationException {
if (!file.getParentFile().exists()) if (!file.getParentFile().exists())
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
if (!file.exists()) if (!file.exists())
util.ExportResource(c.CONFIG_FILE_NAME, file); Util.exportResource(c.CONFIG_FILE_NAME, file);
YamlConfiguration config = new YamlConfiguration(); YamlConfiguration config = new YamlConfiguration();
config.load(file); config.load(file);
@ -75,5 +77,7 @@ public class config {
LILYPAD_GROWING_ENABLED = config.getBoolean("lilypad-growing.enabled"); LILYPAD_GROWING_ENABLED = config.getBoolean("lilypad-growing.enabled");
DISPENSERS_CAN_PLACE_SAPLINGS = config.getBoolean("dispenser-tweaks.place-saplings");
} }
} }