Created
November 4, 2014 02:46
-
-
Save Rustywolf/303c9d43b4f8f87ea6b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rusty.is.the.best; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.Map.Entry; | |
import org.bukkit.Bukkit; | |
import org.bukkit.Location; | |
import org.bukkit.Material; | |
import org.bukkit.block.Block; | |
import org.bukkit.block.BlockFace; | |
import org.bukkit.craftbukkit.v1_7_R4.CraftWorld; | |
import org.bukkit.craftbukkit.v1_7_R4.util.CraftMagicNumbers; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.EventPriority; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.block.BlockPhysicsEvent; | |
import org.bukkit.event.block.BlockPlaceEvent; | |
import org.bukkit.plugin.java.JavaPlugin; | |
public class SugarDupeFix extends JavaPlugin implements Listener, Runnable { | |
private HashMap<Location, Long> locations = new HashMap<>(); | |
@Override | |
public void onEnable() { | |
Bukkit.getPluginManager().registerEvents(this, this); | |
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, this, 1, 10); | |
} | |
@Override | |
public void run() { | |
Iterator<Entry<Location, Long>> it = locations.entrySet().iterator(); | |
while (it.hasNext()) { | |
Entry<Location, Long> entry = it.next(); | |
if (System.currentTimeMillis() - entry.getValue() > 1000) { | |
it.remove(); | |
} | |
} | |
} | |
private final BlockFace[] faces = new BlockFace[]{ | |
BlockFace.UP, BlockFace.DOWN, BlockFace.EAST, BlockFace.WEST, BlockFace.NORTH, BlockFace.SOUTH | |
}; | |
@EventHandler | |
public void onBlockPhysics(BlockPhysicsEvent event) { | |
if (event.getChangedType() == Material.WOODEN_DOOR || event.getChangedType() == Material.IRON_DOOR_BLOCK) { | |
if (event.getBlock().getType() == Material.SUGAR_CANE_BLOCK || event.getBlock().getType() == Material.CACTUS) { | |
Material updaterMaterial = event.getChangedType(); | |
Block targetBlock = event.getBlock(); | |
ArrayList<Block> potentialBlocks = new ArrayList<>(); | |
for (BlockFace bf : faces) { | |
Block testBlock = event.getBlock().getRelative(bf); | |
if (testBlock.getType() == event.getChangedType()) { | |
potentialBlocks.add(testBlock); | |
} | |
} | |
event.setCancelled(true); | |
Bukkit.getScheduler().runTaskLater(this, () -> { | |
for (Block b : potentialBlocks) { | |
if (locations.containsKey(b.getLocation())) { | |
System.out.println("true"); | |
return; | |
} | |
} | |
net.minecraft.server.v1_7_R4.Block nmsBlock = CraftMagicNumbers.getBlock(targetBlock); | |
nmsBlock.doPhysics(((CraftWorld) targetBlock.getWorld()).getHandle(), targetBlock.getX(), targetBlock.getY(), targetBlock.getZ(), CraftMagicNumbers.getBlock(updaterMaterial)); | |
System.out.println("true2"); | |
}, 1); | |
} | |
} | |
} | |
@EventHandler(priority = EventPriority.MONITOR) | |
public void onBlockPlace(BlockPlaceEvent event) { | |
Location blockLoc = event.getBlock().getLocation(); | |
if (event.isCancelled() && (event.getBlock().getType() == Material.WOODEN_DOOR || event.getBlock().getType() == Material.IRON_DOOR_BLOCK) && blockLoc.getBlockY() < blockLoc.getWorld().getMaxHeight() - 1) { | |
locations.put(blockLoc, System.currentTimeMillis()); | |
locations.put(blockLoc.clone().add(0, 1, 0), System.currentTimeMillis()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment