Skip to content

Commit

Permalink
Add horse and spawqers mechanics
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbit4 committed Aug 22, 2024
1 parent a1a7348 commit b417618
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = 'pl.norbit'
version = 'v1.2.0'
version = 'v1.3.0'

repositories {
mavenCentral()
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/pl/norbit/survivaltweaks/SurvivalTweaks.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public void onEnable() {
pluginManager.registerEvents(new PlayerDeadListener(), this);
pluginManager.registerEvents(new FireballListener(), this);
pluginManager.registerEvents(new PlayerTotemVoidListener(), this);
pluginManager.registerEvents(new SpawnerBreakListener(), this);
pluginManager.registerEvents(new PlayerTpListener(), this);

getCommand("survivaltweaks").setExecutor(new MainCommand());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public static boolean isEnabled(Mechanic mechanic) {
case PLAYER_HEAD -> Config.isPlayerHeadEnabled();
case VOID_TOTEM -> Config.isVoidTotemEnabled();
case CUSTOM_DEATH_MESSAGES -> Config.isCustomDeathMessageEnabled();
case MINE_SPAWNERS -> Config.isMineSpawnersEnabled();
case HORSE_TP -> Config.isHorseTpEnabled();
};
}

Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ public enum Mechanic {
FIREBALL,
PLAYER_HEAD,
VOID_TOTEM,
CUSTOM_DEATH_MESSAGES
CUSTOM_DEATH_MESSAGES,
MINE_SPAWNERS,
HORSE_TP
}

0 comments on commit b417618

Please sign in to comment.