From 41aa8385103c71c2d8155d3719457c410abba481 Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 12 Nov 2013 15:12:44 -0500 Subject: [PATCH] Setup a registration system for sentry guns --- src/dark/api/ISentryGun.java | 9 ++ .../core/prefab/sentry/BlockSentryGun.java | 115 ++++++++++++++++++ .../core/prefab/sentry/EntitySentryGun.java | 40 ++++++ .../core/prefab/sentry/ItemBlockSentry.java | 30 +++++ .../prefab/sentry/TileEntityTerminal.java | 6 + 5 files changed, 200 insertions(+) create mode 100644 src/dark/api/ISentryGun.java create mode 100644 src/dark/core/prefab/sentry/BlockSentryGun.java create mode 100644 src/dark/core/prefab/sentry/EntitySentryGun.java create mode 100644 src/dark/core/prefab/sentry/ItemBlockSentry.java create mode 100644 src/dark/core/prefab/sentry/TileEntityTerminal.java diff --git a/src/dark/api/ISentryGun.java b/src/dark/api/ISentryGun.java new file mode 100644 index 000000000..30d5aa319 --- /dev/null +++ b/src/dark/api/ISentryGun.java @@ -0,0 +1,9 @@ +package dark.api; + +/** Applied to tile entities that are sentry guns + * + * @author DarkGuardsman */ +public interface ISentryGun +{ + +} diff --git a/src/dark/core/prefab/sentry/BlockSentryGun.java b/src/dark/core/prefab/sentry/BlockSentryGun.java new file mode 100644 index 000000000..46656d738 --- /dev/null +++ b/src/dark/core/prefab/sentry/BlockSentryGun.java @@ -0,0 +1,115 @@ +package dark.core.prefab.sentry; + +import java.util.Set; + +import com.builtbroken.common.Pair; + +import net.minecraft.block.Block; +import net.minecraft.block.material.Material; +import net.minecraft.tileentity.TileEntity; +import dark.core.common.DarkMain; +import dark.core.prefab.machine.BlockMachine; +import dark.core.registration.ModObjectRegistry; + +/** Actual block that is the sentry gun. Mainly a place holder as the sentry guns need something to + * exist threw that is not an entity. + * + * @author DarkGuardsman */ +public class BlockSentryGun extends BlockMachine +{ + static TileEntitySentry[] sentryGuns = new TileEntitySentry[16]; + static String[] sentryGunNames = new String[16]; + static int registeredGuns = 0; + static int[] sentryBlockIds = new int[1]; + + public BlockSentryGun(int v) + { + super(DarkMain.CONFIGURATION, "DMSentryGun" + v, Material.iron); + this.setResistance(100); + this.setHardness(100); + } + + /** Make sure to let others know your claimed IDs or add configs so we don't run into issues. As + * well the slot is the meta data value the gun will be created with. Any number over 15 will + * create another block then use its 16 meta values. */ + public static void registerSentry(int slot, String name, Class clazz) + { + try + { + //Expands the sentry gun list as needed; + if (slot > sentryGuns.length) + { + int b = (slot / 16) + 1; + sentryBlockIds = new int[b]; + TileEntitySentry[] guns = new TileEntitySentry[b * 16]; + for (int s = 0; s < sentryGuns.length; s++) + { + guns[s] = sentryGuns[s]; + } + sentryGuns = guns; + } + if (clazz != null && sentryGuns[slot] == null) + { + int b = (slot / 16); + sentryBlockIds[b] = -1; + sentryGuns[slot] = clazz.newInstance(); + sentryGunNames[slot] = name; + registeredGuns++; + } + else if (sentryGuns[slot] != null) + { + throw new IllegalArgumentException("Sentry gun slot " + slot + " is already occupied by " + sentryGuns[slot] + " when adding " + clazz); + } + + } + catch (InstantiationException e) + { + e.printStackTrace(); + } + catch (IllegalAccessException e) + { + e.printStackTrace(); + } + catch (IllegalArgumentException e) + { + throw e; + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + /** Called by the core when this block is about to be created. If there are no registered sentry + * guns then this block doesn't exist. */ + public static void createAndRegister() + { + if (registeredGuns > 0) + { + for (int b = 0; b < sentryBlockIds.length; b++) + { + if (sentryBlockIds[b] == -1) + { + Block block = ModObjectRegistry.createNewBlock("DMSentryGun" + b, DarkMain.MOD_ID, BlockSentryGun.class); + sentryBlockIds[b] = block.blockID; + } + } + for (int t = 0; t < sentryGuns.length; t++) + { + + } + } + } + + @Override + public void getTileEntities(int blockID, Set>> list) + { + for (int t = 0; t < sentryGuns.length; t++) + { + if (sentryGuns[t] != null) + { + list.add(new Pair("DMSentry_" + sentryGunNames[t], sentryGuns[t].getClass())); + } + } + } +} diff --git a/src/dark/core/prefab/sentry/EntitySentryGun.java b/src/dark/core/prefab/sentry/EntitySentryGun.java new file mode 100644 index 000000000..37c5db55a --- /dev/null +++ b/src/dark/core/prefab/sentry/EntitySentryGun.java @@ -0,0 +1,40 @@ +package dark.core.prefab.sentry; + +import net.minecraft.entity.Entity; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.world.World; + +/** Since we are going to need an entity for the sentries to take damage with we are actually going + * to do more with the entity than redirect damage. + * + * @author DarkGuardsman */ +public class EntitySentryGun extends Entity +{ + + public EntitySentryGun(World par1World) + { + super(par1World); + } + + @Override + protected void entityInit() + { + // TODO Auto-generated method stub + + } + + @Override + protected void readEntityFromNBT(NBTTagCompound nbttagcompound) + { + // TODO Auto-generated method stub + + } + + @Override + protected void writeEntityToNBT(NBTTagCompound nbttagcompound) + { + // TODO Auto-generated method stub + + } + +} diff --git a/src/dark/core/prefab/sentry/ItemBlockSentry.java b/src/dark/core/prefab/sentry/ItemBlockSentry.java new file mode 100644 index 000000000..40f4b14b4 --- /dev/null +++ b/src/dark/core/prefab/sentry/ItemBlockSentry.java @@ -0,0 +1,30 @@ +package dark.core.prefab.sentry; + +import net.minecraft.block.Block; +import net.minecraft.item.ItemStack; +import dark.core.prefab.ItemBlockHolder; + +/** Item block that is the unplaced sentry gun. All sentry gun data is held by NBT to allow the sentry + * gun to exist without metadata limits. + * + * @author DarkGuardsman */ +public class ItemBlockSentry extends ItemBlockHolder +{ + public ItemBlockSentry(int id) + { + super(id); + } + + @Override + public int getMetadata(int damage) + { + return 0; + } + + @Override + public String getUnlocalizedName(ItemStack itemStack) + { + return Block.blocksList[this.getBlockID()].getUnlocalizedName() + "." + itemStack.getItemDamage(); + } + +} diff --git a/src/dark/core/prefab/sentry/TileEntityTerminal.java b/src/dark/core/prefab/sentry/TileEntityTerminal.java new file mode 100644 index 000000000..1e232c4a8 --- /dev/null +++ b/src/dark/core/prefab/sentry/TileEntityTerminal.java @@ -0,0 +1,6 @@ +package dark.core.prefab.sentry; + +public class TileEntityTerminal +{ + +}