Parcourir la source

First commit.

First commit.
aurilisdev il y a 6 ans
commit
c9e1cc2620
4 fichiers modifiés avec 123 ajouts et 0 suppressions
  1. 4 0
      .gitignore
  2. 21 0
      LICENSE
  3. 7 0
      src/plugin.yml
  4. 91 0
      src/timber/core/Plugin.java

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+/bin/
+.classpath
+.project
+.settings/org.eclipse.jdt.core.prefs

+ 21 - 0
LICENSE

@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2019 aurilisdev
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.

+ 7 - 0
src/plugin.yml

@@ -0,0 +1,7 @@
+main: timber.core.Plugin
+name: Timber
+version: 1.0.1
+website: http://aurilisdev.com/
+author: aurilisdev
+api-version: 1.13
+description: With this plugin you can cut down an entire tree by breaking the bottom part!

+ 91 - 0
src/timber/core/Plugin.java

@@ -0,0 +1,91 @@
+package timber.core;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.LinkedList;
+
+import org.bukkit.GameMode;
+import org.bukkit.Location;
+import org.bukkit.Material;
+import org.bukkit.block.Block;
+import org.bukkit.entity.Player;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.Listener;
+import org.bukkit.event.block.BlockBreakEvent;
+import org.bukkit.inventory.ItemStack;
+import org.bukkit.inventory.meta.Damageable;
+import org.bukkit.inventory.meta.ItemMeta;
+import org.bukkit.plugin.java.JavaPlugin;
+
+public class Plugin extends JavaPlugin implements Listener {
+	@SuppressWarnings("deprecation")
+	public static HashSet<Material>	logMaterials	= new HashSet<>(
+			Arrays.asList(Material.LEGACY_LOG, Material.LEGACY_LOG_2, Material.ACACIA_LOG, Material.BIRCH_LOG, Material.DARK_OAK_LOG, Material.JUNGLE_LOG, Material.OAK_LOG, Material.SPRUCE_LOG));
+	public static HashSet<Material>	axeMaterials	= new HashSet<>(Arrays.asList(Material.DIAMOND_AXE, Material.GOLDEN_AXE, Material.IRON_AXE, Material.STONE_AXE, Material.WOODEN_AXE));
+
+	@Override
+	public void onEnable()
+	{
+		getServer().getPluginManager().registerEvents(this, this);
+	}
+
+	@EventHandler
+	public void onBlockBreak(BlockBreakEvent e)
+	{
+		Player player = e.getPlayer();
+		if (!player.isSneaking())
+		{
+			if (!player.hasPermission("timber.disallow") || player.isOp())
+			{
+				ItemStack handStack = player.getInventory().getItemInMainHand();
+				if (axeMaterials.contains(handStack.getType()))
+				{
+					Block block = e.getBlock();
+					if (logMaterials.contains(block.getType()))
+					{
+						cutDownTree(block.getLocation(), player.getGameMode() == GameMode.CREATIVE ? handStack.clone() : handStack);
+					}
+				}
+			}
+		}
+	}
+
+	private void cutDownTree(Location location, ItemStack handStack)
+	{
+		LinkedList<Block> blocks = new LinkedList<>();
+		for (int i = location.getBlockY(); i < location.getWorld().getHighestBlockYAt(location.getBlockX(), location.getBlockZ());)
+		{
+			Location l = location.add(0.0D, 1.0D, 0.0D);
+			Block block = l.getBlock();
+			if (logMaterials.contains(block.getType()))
+			{
+				blocks.add(l.getBlock());
+				l = null;
+				i++;
+			} else
+			{
+				break;
+			}
+		}
+		for (Block block : blocks)
+		{
+			if (block.breakNaturally(handStack))
+			{
+				ItemMeta meta = handStack.getItemMeta();
+				if (meta != null)
+				{
+					Damageable damage = (Damageable) meta;
+					damage.setDamage(damage.getDamage() + 1);
+					handStack.setItemMeta(meta);
+					if (handStack.getType().getMaxDurability() == damage.getDamage())
+					{
+						handStack.setType(Material.AIR);
+						return;
+					}
+				}
+			}
+		}
+		blocks = null;
+	}
+
+}