Add mobItemDrops and mobExpDrops game rule

This commit is contained in:
Daniel Saukel 2021-08-21 23:15:15 +02:00
parent 25db2e71d2
commit 98dda3f946
6 changed files with 56 additions and 27 deletions

View File

@ -328,6 +328,18 @@ public class GameRule<V> {
* If Citizens NPCs should be copied to the native registry.
*/
public static final GameRule<Boolean> USE_NATIVE_CITIZENS_REGISTRY = new GameRule<>(Boolean.class, "useNativeCitizensRegistry", false);
/**
* If mobs shall drop experience or a whitelist of mobs that drop experience, while all others do not.
*/
public static final GameRule<Object> MOB_EXP_DROPS = new GameRule(Object.class, "mobExpDrops", false,
(api, value) -> value instanceof Boolean ? value : ConfigReader.EX_MOB_SET_READER.read(api, value)
);
/**
* If mobs shall drop items or a whitelist of mobs that drop items, while all others do not.
*/
public static final GameRule<Object> MOB_ITEM_DROPS = new GameRule(Object.class, "mobItemDrops", false,
(api, value) -> value instanceof Boolean ? value : ConfigReader.EX_MOB_SET_READER.read(api, value)
);
/**
* An array of all game rules that exist natively in DungeonsXL.

View File

@ -384,7 +384,7 @@ public class DungeonsXL extends DREPlugin implements DungeonsAPI {
externalMobProviderRegistry.add(externalMobPlugin.getIdentifier(), externalMobPlugin);
}
if (manager.getPlugin("Citizens") != null) {
CitizensMobProvider citizensMobProvider = new CitizensMobProvider();
CitizensMobProvider citizensMobProvider = new CitizensMobProvider(this);
externalMobProviderRegistry.add("CI", citizensMobProvider);
manager.registerEvents(citizensMobProvider, this);
} else {
@ -696,7 +696,7 @@ public class DungeonsXL extends DREPlugin implements DungeonsAPI {
if (mob != null) {
return mob;
} else {
return new DMob(entity, gameWorld, triggerId);
return new DMob(entity, gameWorld, caliburn.getExMob(triggerId), triggerId);
}
}

View File

@ -16,7 +16,7 @@
*/
package de.erethon.dungeonsxl.mob;
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.api.DungeonsAPI;
import de.erethon.dungeonsxl.api.dungeon.GameRule;
import de.erethon.dungeonsxl.api.mob.ExternalMobProvider;
import de.erethon.dungeonsxl.api.world.GameWorld;
@ -40,11 +40,17 @@ import org.bukkit.event.Listener;
*/
public class CitizensMobProvider implements ExternalMobProvider, Listener {
private DungeonsAPI api;
private static final String IDENTIFIER = "CI";
private DNPCRegistry registry = new DNPCRegistry();
private Set<NPC> spawnedNPCs = new HashSet<>();
public CitizensMobProvider(DungeonsAPI api) {
this.api = api;
}
/**
* @return the DungeonsXL NPC registry
*/
@ -106,7 +112,7 @@ public class CitizensMobProvider implements ExternalMobProvider, Listener {
return;
}
GameWorld gameWorld = DungeonsXL.getInstance().getGameWorld(location.getWorld());
GameWorld gameWorld = api.getGameWorld(location.getWorld());
if (gameWorld == null) {
return;
}
@ -119,7 +125,7 @@ public class CitizensMobProvider implements ExternalMobProvider, Listener {
npc.spawn(location);
spawnedNPCs.add(npc);
new DMob((LivingEntity) npc.getEntity(), gameWorld, mob);
api.wrapEntity((LivingEntity) npc.getEntity(), gameWorld, mob);
}
/* Listeners */

View File

@ -19,6 +19,7 @@ package de.erethon.dungeonsxl.mob;
import de.erethon.caliburn.mob.ExMob;
import de.erethon.caliburn.mob.VanillaMob;
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.api.dungeon.GameRule;
import de.erethon.dungeonsxl.api.event.mob.DungeonMobDeathEvent;
import de.erethon.dungeonsxl.api.event.mob.DungeonMobSpawnEvent;
import de.erethon.dungeonsxl.api.mob.DungeonMob;
@ -43,10 +44,11 @@ public class DMob implements DungeonMob {
private String trigger;
private DMob(LivingEntity entity, GameWorld gameWorld) {
public DMob(LivingEntity entity, GameWorld gameWorld, ExMob type) {
this.entity = entity;
this.type = type;
if (!isExternalMob()) {
if (!getDrops(gameWorld.getDungeon().getRules().getState(GameRule.MOB_ITEM_DROPS))) {
entity.getEquipment().setHelmetDropChance(0);
entity.getEquipment().setChestplateDropChance(0);
entity.getEquipment().setLeggingsDropChance(0);
@ -62,22 +64,11 @@ public class DMob implements DungeonMob {
DungeonMobSpawnEvent event = new DungeonMobSpawnEvent(this);
Bukkit.getPluginManager().callEvent(event);
}
public DMob(LivingEntity entity, GameWorld gameWorld, String trigger) {
this(entity, gameWorld);
this.trigger = trigger;
}
public DMob(LivingEntity entity, GameWorld gameWorld, ExMob type) {
this(entity, gameWorld);
this.type = type;
this.trigger = type.getId();
}
public DMob(LivingEntity entity, GameWorld gameWorld, ExMob type, String trigger) {
this(entity, gameWorld);
this.type = type;
this(entity, gameWorld, type);
this.trigger = trigger;
}
@ -101,8 +92,6 @@ public class DMob implements DungeonMob {
public void onDeath(DungeonsXL plugin, EntityDeathEvent event) {
LivingEntity victim = event.getEntity();
DGameWorld gameWorld = (DGameWorld) plugin.getGameWorld(victim.getWorld());
String name = null;
if (gameWorld == null) {
return;
}
@ -113,12 +102,18 @@ public class DMob implements DungeonMob {
return;
}
if (!getDrops(gameWorld.getDungeon().getRules().getState(GameRule.MOB_ITEM_DROPS))) {
event.getDrops().clear();
}
if (!getDrops(gameWorld.getDungeon().getRules().getState(GameRule.MOB_EXP_DROPS))) {
event.setDroppedExp(0);
}
String name;
if (!isExternalMob()) {
name = type.getId();
} else if (isExternalMob() && trigger != null) {
name = trigger;
} else {
name = VanillaMob.get(victim.getType()).getId();
}
@ -138,6 +133,19 @@ public class DMob implements DungeonMob {
gameWorld.removeMob(this);
}
private boolean getDrops(Object drops) {
if (drops instanceof Boolean) {
return (Boolean) drops;
} else if (drops instanceof Set) {
for (ExMob whitelisted : (Set<ExMob>) drops) {
if (type.isSubsumableUnder(whitelisted)) {
return true;
}
}
}
return false;
}
@Override
public String toString() {
return getClass().getSimpleName() + "{type=" + type + "}";

View File

@ -120,7 +120,7 @@ public class MobSign extends Windup {
initialAmount = n;
provider = attributes.length == 3 ? providers.get(attributes[2]) : null;
setRunnable(new MobSpawnTask(this, n));
setRunnable(new MobSpawnTask(api, this, n));
}
/**

View File

@ -16,7 +16,7 @@
*/
package de.erethon.dungeonsxl.sign.windup;
import de.erethon.dungeonsxl.mob.DMob;
import de.erethon.dungeonsxl.api.DungeonsAPI;
import org.bukkit.entity.LivingEntity;
import org.bukkit.scheduler.BukkitRunnable;
@ -25,10 +25,13 @@ import org.bukkit.scheduler.BukkitRunnable;
*/
public class MobSpawnTask extends BukkitRunnable {
private DungeonsAPI api;
private MobSign sign;
private int k = 1, n;
public MobSpawnTask(MobSign sign, int n) {
public MobSpawnTask(DungeonsAPI api, MobSign sign, int n) {
this.api = api;
this.sign = sign;
this.n = n;
}
@ -42,7 +45,7 @@ public class MobSpawnTask extends BukkitRunnable {
LivingEntity entity = sign.spawn();
if (entity != null) {
new DMob(entity, sign.getGameWorld(), sign.getMob());
api.wrapEntity(entity, sign.getGameWorld(), api.getCaliburn().getExMob(entity), sign.getMob());
}
if (k < n) {