Browse Source

changed generator event again, fixed #5 and #7

mastercake10 6 years ago
parent
commit
4d7e66c78e
2 changed files with 47 additions and 27 deletions
  1. 46 26
      src/de/Linus122/customoregen/Events.java
  2. 1 1
      src/plugin.yml

+ 46 - 26
src/de/Linus122/customoregen/Events.java

@@ -2,50 +2,55 @@ package de.Linus122.customoregen;
 
 import java.util.Random;
 
-import org.bukkit.Bukkit;
 import org.bukkit.Material;
 import org.bukkit.OfflinePlayer;
 import org.bukkit.block.Block;
-import org.bukkit.entity.Player;
+import org.bukkit.block.BlockFace;
 import org.bukkit.event.EventHandler;
 import org.bukkit.event.Listener;
-import org.bukkit.event.block.BlockFormEvent;
+import org.bukkit.event.block.BlockFromToEvent;
 import org.bukkit.event.player.PlayerJoinEvent;
 
 public class Events implements Listener {
 	@SuppressWarnings("deprecation")
 	@EventHandler
-	public void onCobbleGen(BlockFormEvent event) {
+	public void onFromTo(BlockFromToEvent event) {
 		if (Main.disabledWorlds.contains(event.getBlock().getLocation().getWorld().getName())) {
 			return;
 		}
-
-		Material newBlock = event.getNewState().getType();
-		Block b = event.getBlock();
 		
-		if (newBlock.equals(Material.COBBLESTONE) || newBlock.equals(Material.STONE)) {
-
-			OfflinePlayer p = Main.getOwner(b.getLocation());
-			if(p == null) return;
-			GeneratorConfig gc = Main.getGeneratorConfigForPlayer(p);
-			if (gc == null)
-				return;
-			if (getObject(gc) == null)
-				return;
-			GeneratorItem winning = getObject(gc);
-			if (Material.getMaterial(winning.name) == null)
-				return;
-
-			if (Material.getMaterial(winning.name).equals(Material.COBBLESTONE) && winning.damage == 0) {
-				return;
+		int id = event.getBlock().getTypeId();
+		if ((id >= 8) && (id <= 11)) {
+			Block b = event.getToBlock();
+			int toid = b.getTypeId();
+			if ((toid == 0) && (generatesCobble(id, b))) {
+				OfflinePlayer p = Main.getOwner(b.getLocation());
+				if (p == null)
+					return;
+				GeneratorConfig gc = Main.getGeneratorConfigForPlayer(p);
+				if (gc == null)
+					return;
+				if (getObject(gc) == null)
+					return;
+				GeneratorItem winning = getObject(gc);
+				if (Material.getMaterial(winning.name) == null)
+					return;
+
+				if (Material.getMaterial(winning.name).equals(Material.COBBLESTONE) && winning.damage == 0) {
+					return;
+				}
+				event.setCancelled(true);
+				b.setType(Material.getMaterial(winning.name));
+				// <Block>.setData(...) is deprecated, but there is no
+				// alternative to it. #spigot
+				b.setData(winning.damage, true);
 			}
-			event.setCancelled(true);
-			b.setType(Material.getMaterial(winning.name));
-			b.setData(winning.damage, true);
 		}
+
 	}
+
 	@EventHandler
-	public void onJoin(PlayerJoinEvent e){
+	public void onJoin(PlayerJoinEvent e) {
 		Main.getGeneratorConfigForPlayer(e.getPlayer());
 	}
 
@@ -59,4 +64,19 @@ public class Events implements Listener {
 		}
 		return new GeneratorItem("COBBLESTONE", (byte) 0, 0); // DEFAULT
 	}
+
+	private final BlockFace[] faces = { BlockFace.SELF, BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.EAST,
+			BlockFace.SOUTH, BlockFace.WEST };
+
+	public boolean generatesCobble(int id, Block b) {
+		int mirrorID1 = (id == 8) || (id == 9) ? 10 : 8;
+		int mirrorID2 = (id == 8) || (id == 9) ? 11 : 9;
+		for (BlockFace face : this.faces) {
+			Block r = b.getRelative(face, 1);
+			if ((r.getTypeId() == mirrorID1) || (r.getTypeId() == mirrorID2)) {
+				return true;
+			}
+		}
+		return false;
+	}
 }

+ 1 - 1
src/plugin.yml

@@ -1,5 +1,5 @@
 name: CustomOreGen
-version: 1.2.38
+version: 1.2.39
 description: Controls the Ore-Generator
 author: Linus122
 softdepend: [ASkyBlock, AcidIsland, uSkyBlock]