diff --git a/src/main/java/com/hbm/tileentity/turret/TileEntityTurretBaseNT.java b/src/main/java/com/hbm/tileentity/turret/TileEntityTurretBaseNT.java index ef43786c1..b8217f1d3 100644 --- a/src/main/java/com/hbm/tileentity/turret/TileEntityTurretBaseNT.java +++ b/src/main/java/com/hbm/tileentity/turret/TileEntityTurretBaseNT.java @@ -3,6 +3,7 @@ package com.hbm.tileentity.turret; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.function.BiFunction; import com.hbm.blocks.BlockDummyable; import com.hbm.entity.logic.EntityBomber; @@ -10,6 +11,7 @@ import com.hbm.entity.missile.EntityMissileBaseAdvanced; import com.hbm.entity.missile.EntityMissileCustom; import com.hbm.entity.missile.EntitySiegeDropship; import com.hbm.entity.projectile.EntityBulletBase; +import com.hbm.entity.train.EntityRailCarBase; import com.hbm.handler.BulletConfigSyncingUtil; import com.hbm.handler.BulletConfiguration; import com.hbm.handler.CasingEjector; @@ -24,6 +26,7 @@ import com.hbm.packet.PacketDispatcher; import com.hbm.particle.SpentCasing; import com.hbm.tileentity.IGUIProvider; import com.hbm.tileentity.TileEntityMachineBase; +import com.hbm.util.CompatExternal; import api.hbm.energy.IEnergyUser; import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint; @@ -592,8 +595,20 @@ public abstract class TileEntityTurretBaseNT extends TileEntityMachineBase imple if(e.isDead || !e.isEntityAlive()) return false; - + for(Class c : CompatExternal.turretTargetBlacklist) if(c.isAssignableFrom(e.getClass())) return false; + + for(Class c : CompatExternal.turretTargetCondition.keySet()) { + if(c.isAssignableFrom(e.getClass())) { + BiFunction lambda = CompatExternal.turretTargetCondition.get(c); + if(lambda != null) { + int result = lambda.apply(e, this); + if(result == -1) return false; + if(result == 1) return true; + } + } + } + List wl = getWhitelist(); if(wl != null) { @@ -613,6 +628,7 @@ public abstract class TileEntityTurretBaseNT extends TileEntityMachineBase imple if(e instanceof IAnimals) return true; if(e instanceof INpc) return true; + for(Class c : CompatExternal.turretTargetFriendly) if(c.isAssignableFrom(e.getClass())) return true; } if(targetMobs) { @@ -621,6 +637,7 @@ public abstract class TileEntityTurretBaseNT extends TileEntityMachineBase imple if(e instanceof EntityDragon) return false; if(e instanceof EntityDragonPart) return true; if(e instanceof IMob) return true; + for(Class c : CompatExternal.turretTargetHostile) if(c.isAssignableFrom(e.getClass())) return true; } if(targetMachines) { @@ -628,16 +645,19 @@ public abstract class TileEntityTurretBaseNT extends TileEntityMachineBase imple if(e instanceof EntityMissileBaseAdvanced) return true; if(e instanceof EntityMissileCustom) return true; if(e instanceof EntityMinecart) return true; + if(e instanceof EntityRailCarBase) return true; if(e instanceof EntityBomber) return true; if(e instanceof EntitySiegeDropship) return true; + for(Class c : CompatExternal.turretTargetMachine) if(c.isAssignableFrom(e.getClass())) return true; } - if(targetPlayers && e instanceof EntityPlayer) { + if(targetPlayers ) { if(e instanceof FakePlayer) return false; - return true; + if(e instanceof EntityPlayer) return true; + for(Class c : CompatExternal.turretTargetPlayer) if(c.isAssignableFrom(e.getClass())) return true; } return false; diff --git a/src/main/java/com/hbm/util/CompatExternal.java b/src/main/java/com/hbm/util/CompatExternal.java index 00d1425ee..46f1fab61 100644 --- a/src/main/java/com/hbm/util/CompatExternal.java +++ b/src/main/java/com/hbm/util/CompatExternal.java @@ -1,15 +1,24 @@ package com.hbm.util; import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; +import java.util.function.BiFunction; import com.hbm.blocks.BlockDummyable; import com.hbm.inventory.fluid.FluidType; import com.hbm.inventory.fluid.tank.FluidTank; import com.hbm.tileentity.machine.TileEntityDummy; +import com.hbm.tileentity.turret.TileEntityTurretSentry; import api.hbm.energy.IEnergyUser; import api.hbm.fluid.IFluidUser; import net.minecraft.block.Block; +import net.minecraft.entity.Entity; +import net.minecraft.entity.passive.EntityChicken; +import net.minecraft.entity.passive.EntityCow; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; @@ -125,4 +134,60 @@ public class CompatExternal { return list; } + + public static Set turretTargetPlayer = new HashSet(); + public static Set turretTargetFriendly = new HashSet(); + public static Set turretTargetHostile = new HashSet(); + public static Set turretTargetMachine = new HashSet(); + + /** + * Registers a class for turret targeting + * @param clazz is the class that should be targeted. + * @param type determines what setting the turret needs to have enabled to target this class. 0 is player, 1 is friendly, 2 is hostile and 3 is machine. + */ + public static void registerTurretTargetSimple(Class clazz, int type) { + + switch(type) { + case 0: turretTargetPlayer.add(clazz); break; + case 1: turretTargetFriendly.add(clazz); break; + case 2: turretTargetHostile.add(clazz); break; + case 3: turretTargetMachine.add(clazz); break; + } + } + + public static Set turretTargetBlacklist = new HashSet(); + + /** + * Registers a class to be fully ignored by turrets + * @param clazz is the class that should be ignored. + */ + public static void registerTurretTargetBlacklist(Class clazz) { + turretTargetBlacklist.add(clazz); + } + + public static HashMap> turretTargetCondition = new HashMap(); + + /** + * Registers a BiFunction lambda for more complex targeting compatibility + * @param clazz is the class that this rule should apply to + * @param bi is the lambda. The function should return 0 to continue with other targeting checks (i.e. do nothing), -1 to ignore this entity or 1 to target it. + * The params for this lambda are the entity and the turret in question. The type for the turret is omitted on purpose as to not require any reference of the tile entity + * class on the side of whoever is adding compat, allowing the compat class to be used entirely with reflection. + */ + public static void registerTurretTargetingCondition(Class clazz, BiFunction bi) { + turretTargetBlacklist.add(clazz); + } + + public static void compatExamples() { + // Makes all cows be targeted by turrets if player mode is active in addition to the existing rules. Applies to all entities that inherit EntityCow. + CompatExternal.registerTurretTargetSimple(EntityCow.class, 0); + // Makes all chickens ignored by turrets, also applies to entities that inherit EntityChicken like ducks. + CompatExternal.registerTurretTargetBlacklist(EntityChicken.class); + // An example for more complex turret behavior. Turrets will always target players named "Target Practice", and sentry turrets will never target players. + CompatExternal.registerTurretTargetingCondition(EntityPlayer.class, (entity, turret) -> { + if(entity.getCommandSenderName().equals("Target Practice")) return 1; + if(turret instanceof TileEntityTurretSentry) return -1; + return 0; + }); + } }