-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ plugins { | |
} | ||
|
||
group = 'pl.norbit' | ||
version = 'v1.2.0' | ||
version = 'v1.3.0' | ||
|
||
repositories { | ||
mavenCentral() | ||
|
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
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
91 changes: 91 additions & 0 deletions
91
src/main/java/pl/norbit/survivaltweaks/mechanics/listeners/SpawnerBreakListener.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package pl.norbit.survivaltweaks.mechanics.listeners; | ||
|
||
import org.bukkit.Material; | ||
import org.bukkit.World; | ||
import org.bukkit.block.Block; | ||
import org.bukkit.block.CreatureSpawner; | ||
import org.bukkit.enchantments.Enchantment; | ||
import org.bukkit.entity.EntityType; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.block.BlockBreakEvent; | ||
import org.bukkit.event.block.BlockPlaceEvent; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.meta.BlockStateMeta; | ||
import pl.norbit.survivaltweaks.mechanics.MechanicsLoader; | ||
import pl.norbit.survivaltweaks.mechanics.model.Mechanic; | ||
|
||
public class SpawnerBreakListener implements Listener { | ||
|
||
@EventHandler | ||
public void onBlockBreak(BlockBreakEvent e) { | ||
if(MechanicsLoader.isDisabled(Mechanic.MINE_SPAWNERS)){ | ||
return; | ||
} | ||
|
||
Block b = e.getBlock(); | ||
|
||
if(b.getType() != Material.SPAWNER){ | ||
return; | ||
} | ||
|
||
Player p = e.getPlayer(); | ||
ItemStack tool = p.getInventory().getItemInMainHand(); | ||
|
||
if (!tool.containsEnchantment(Enchantment.SILK_TOUCH)) { | ||
return; | ||
} | ||
World w = b.getWorld(); | ||
|
||
CreatureSpawner spawner = (CreatureSpawner) b.getState(); | ||
EntityType entityType = spawner.getSpawnedType(); | ||
|
||
ItemStack spawnerItem = getSpawnerItem(entityType); | ||
|
||
w.dropItemNaturally(b.getLocation(), spawnerItem); | ||
b.setType(Material.AIR); | ||
|
||
e.setCancelled(true); | ||
} | ||
|
||
@EventHandler | ||
public void onBlockPlace(BlockPlaceEvent e) { | ||
if(MechanicsLoader.isDisabled(Mechanic.MINE_SPAWNERS)){ | ||
return; | ||
} | ||
|
||
Block b = e.getBlock(); | ||
|
||
if(b.getType() != Material.SPAWNER){ | ||
return; | ||
} | ||
|
||
ItemStack item = e.getItemInHand(); | ||
BlockStateMeta meta = (BlockStateMeta) item.getItemMeta(); | ||
CreatureSpawner spawner = (CreatureSpawner) meta.getBlockState(); | ||
EntityType entityType = spawner.getSpawnedType(); | ||
|
||
if(entityType == null){ | ||
return; | ||
} | ||
|
||
CreatureSpawner placedSpawner = (CreatureSpawner) b.getState(); | ||
placedSpawner.setSpawnedType(entityType); | ||
placedSpawner.update(); | ||
} | ||
|
||
private ItemStack getSpawnerItem(EntityType entityType){ | ||
if(entityType == null){ | ||
return new ItemStack(Material.SPAWNER); | ||
} | ||
|
||
ItemStack spawnerItem = new ItemStack(Material.SPAWNER); | ||
BlockStateMeta meta = (BlockStateMeta) spawnerItem.getItemMeta(); | ||
CreatureSpawner spawner = (CreatureSpawner) meta.getBlockState(); | ||
spawner.setSpawnedType(entityType); | ||
meta.setBlockState(spawner); | ||
spawnerItem.setItemMeta(meta); | ||
return spawnerItem; | ||
} | ||
} |
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