Make the bone-to-pet-wolf transformation item customizable.

A new section in global-settings called pet-items is used to define pet item transformation items. By default, bones are transformed into wolves, but the item type can now be changed to support using actual bones in classes.

This refactors the pet spawning into its own class to reduce some of the massive responsibility of ArenaImpl.

This closes #467
This commit is contained in:
Andreas Troelsen
2018-07-03 18:13:36 +02:00
parent f4f74c5b87
commit 63d2c552a6
5 changed files with 87 additions and 39 deletions
@@ -42,7 +42,6 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse;
import org.bukkit.entity.Player;
import org.bukkit.entity.Wolf;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
@@ -135,6 +134,8 @@ public class ArenaImpl implements Arena
private StepFactory playerJoinArena;
private StepFactory playerSpecArena;
private SpawnsPets spawnsPets;
/**
* Primary constructor. Requires a name and a world.
*/
@@ -231,6 +232,8 @@ public class ArenaImpl implements Arena
this.histories = new HashMap<>();
this.playerJoinArena = PlayerJoinArena.create(this);
this.playerSpecArena = PlayerSpecArena.create(this);
this.spawnsPets = plugin.getArenaMaster().getSpawnsPets();
}
@@ -538,7 +541,7 @@ public class ArenaImpl implements Arena
running = true;
// Spawn pets (must happen after 'running = true;')
spawnPets();
spawnsPets.spawn(this);
// Spawn mounts
spawnMounts();
@@ -922,42 +925,6 @@ public class ArenaImpl implements Arena
}
}
private void spawnPets() {
for (Player p : arenaPlayers) {
// Skip players who are either null or offline
if (p == null || !p.isOnline()) continue;
// Skip the My Items class
ArenaClass ac = arenaPlayerMap.get(p).getArenaClass();
if (ac == null || ac.getConfigName().equals("My Items")) continue;
// Grab the inventory
PlayerInventory inv = p.getInventory();
if (inv == null) continue;
// Find the first slot containing bones
int bone = inv.first(Material.BONE);
if (bone == -1) continue;
// Get the amount of pets to spawn
int amount = inv.getItem(bone).getAmount();
// Spawn each pet
for (int i = 0; i < amount; i++) {
Wolf wolf = (Wolf) world.spawnEntity(p.getLocation(), EntityType.WOLF);
wolf.setTamed(true);
wolf.setOwner(p);
wolf.setHealth(wolf.getMaxHealth());
if (settings.getBoolean("hellhounds"))
wolf.setFireTicks(32768);
monsterManager.addPet(wolf);
}
// Remove the bones
inv.setItem(bone, null);
}
}
private void spawnMounts() {
for (Player p : arenaPlayers) {
// Skip players who are either null or offline
@@ -9,6 +9,7 @@ import com.garbagemule.MobArena.things.Thing;
import com.garbagemule.MobArena.util.config.ConfigUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
@@ -39,6 +40,7 @@ public class ArenaMasterImpl implements ArenaMaster
private Map<String, ArenaClass> classes;
private Set<String> allowedCommands;
private SpawnsPets spawnsPets;
private boolean enabled;
@@ -55,6 +57,7 @@ public class ArenaMasterImpl implements ArenaMaster
this.classes = new HashMap<>();
this.allowedCommands = new HashSet<>();
this.spawnsPets = new SpawnsPets(Material.BONE);
this.enabled = config.getBoolean("global-settings.enabled", true);
}
@@ -257,6 +260,20 @@ public class ArenaMasterImpl implements ArenaMaster
for (String part : parts) {
allowedCommands.add(part.trim().toLowerCase());
}
loadPetItems(section);
}
private void loadPetItems(ConfigurationSection settings) {
String wolf = settings.getString("pet-items.wolf", "");
Material wolfMaterial = Material.getMaterial(wolf.toUpperCase());
if (wolfMaterial == null && !wolf.isEmpty()) {
plugin.getLogger().warning("Unknown item type for wolf pet item: " + wolf);
}
spawnsPets = new SpawnsPets(wolfMaterial);
}
/**
@@ -560,6 +577,10 @@ public class ArenaMasterImpl implements ArenaMaster
plugin.saveConfig();
}
public SpawnsPets getSpawnsPets() {
return spawnsPets;
}
public void reloadConfig() {
boolean wasEnabled = isEnabled();
if (wasEnabled) setEnabled(false);
@@ -0,0 +1,55 @@
package com.garbagemule.MobArena;
import com.garbagemule.MobArena.framework.Arena;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Wolf;
import org.bukkit.inventory.PlayerInventory;
public class SpawnsPets {
private final Material wolfMaterial;
SpawnsPets(Material wolfMaterial) {
this.wolfMaterial = wolfMaterial;
}
void spawn(Arena arena) {
arena.getPlayersInArena()
.forEach(player -> spawnPets(player, arena));
}
private void spawnPets(Player player, Arena arena) {
if (player == null || !player.isOnline()) {
return;
}
ArenaClass ac = arena.getArenaPlayer(player).getArenaClass();
if (ac == null || ac.getConfigName().equals("My Items")) {
return;
}
spawnWolfPets(player, arena);
}
private void spawnWolfPets(Player player, Arena arena) {
if (wolfMaterial == null) {
return;
}
PlayerInventory inv = player.getInventory();
int index = inv.first(wolfMaterial);
if (index == -1) {
return;
}
int amount = inv.getItem(index).getAmount();
for (int i = 0; i < amount; i++) {
Wolf wolf = (Wolf) arena.getWorld().spawnEntity(player.getLocation(), EntityType.WOLF);
wolf.setTamed(true);
wolf.setOwner(player);
arena.getMonsterManager().addPet(wolf);
}
inv.setItem(index, null);
}
}
@@ -3,6 +3,7 @@ package com.garbagemule.MobArena.framework;
import com.garbagemule.MobArena.ArenaClass;
import com.garbagemule.MobArena.Messenger;
import com.garbagemule.MobArena.MobArena;
import com.garbagemule.MobArena.SpawnsPets;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity;
@@ -122,6 +123,8 @@ public interface ArenaMaster
void removeArenaNode(Arena arena);
SpawnsPets getSpawnsPets();
/*/////////////////////////////////////////////////////////////////////////
+3 -1
View File
@@ -1,4 +1,6 @@
enabled: true
allowed-commands: /list, /pl
update-notification: true
prefix: '&a[MobArena] '
prefix: '&a[MobArena] '
pet-items:
wolf: bone