Added the Piston Relay. Very much just a proof of concept right now, no texture or anything.

This commit is contained in:
Vasco Lavos committed 2014-02-20 21:43:15 +00:00
1 parent 7ebf2ccfa5
commit 06f7c3827c
6 files changed
+196

No files matched your search

@@ -0,0 +1,182 @@
/**
* This class was created by <Vazkii>. It's distributed as
* part of the Botania Mod. Get the Source Code in github:
* https://github.com/Vazkii/Botania
*
* Botania is Open Source and distributed under a
* Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License
* (http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_GB)
*
* File Created @ [Feb 20, 2014, 4:57:36 PM (GMT)]
*/
package vazkii.botania.common.block;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import vazkii.botania.api.IWandable;
import vazkii.botania.common.lib.LibBlockIDs;
import vazkii.botania.common.lib.LibBlockNames;
import cpw.mods.fml.common.ITickHandler;
import cpw.mods.fml.common.TickType;
import cpw.mods.fml.common.registry.TickRegistry;
import cpw.mods.fml.relauncher.Side;
public class BlockPistonRelay extends BlockMod implements IWandable {
public BlockPistonRelay() {
super(LibBlockIDs.idPistonRelay, Material.pumpkin);
setUnlocalizedName(LibBlockNames.PISTON_RELAY);
TickRegistry.registerTickHandler(new InternalTickHandler(), Side.SERVER);
}
@Override
public int idDropped(int par1, Random par2Random, int par3) {
return 0;
}
@Override
public void breakBlock(World par1World, int par2, int par3, int par4, int par5, int par6) {
mapCoords(par1World.provider.dimensionId, par2, par3, par4, 2);
}
public static String getCoordsAsString(int world, int x, int y, int z) {
return world + ":" + x + ":" + y + ":" + z;
}
static void mapCoords(int world, int x, int y, int z, int time) {
InternalTickHandler.coordsToCheck.put(getCoordsAsString(world, x, y, z), time);
}
static void decrCoords(String key) {
int time = getTimeInCoords(key);
if(time <= 0)
InternalTickHandler.removeThese.add(key);
else InternalTickHandler.coordsToCheck.put(key, time - 1);
}
static int getTimeInCoords(String key) {
return InternalTickHandler.coordsToCheck.get(key);
}
static int getBlockIDAt(String key) {
MinecraftServer server = MinecraftServer.getServer();
if(server == null)
return 0;
String[] tokens = key.split(":");
int worldId = Integer.parseInt(tokens[0]), x = Integer.parseInt(tokens[1]), y = Integer.parseInt(tokens[2]), z = Integer.parseInt(tokens[3]);
World world = server.worldServerForDimension(worldId);
return world.getBlockId(x, y, z);
}
static int getBlockMetaAt(String key) {
MinecraftServer server = MinecraftServer.getServer();
if(server == null)
return 0;
String[] tokens = key.split(":");
int worldId = Integer.parseInt(tokens[0]), x = Integer.parseInt(tokens[1]), y = Integer.parseInt(tokens[2]), z = Integer.parseInt(tokens[3]);
World world = server.worldServerForDimension(worldId);
return world.getBlockMetadata(x, y, z);
}
@Override
public boolean onUsedByWand(EntityPlayer player, ItemStack stack, World world, int x, int y, int z, int side) {
InternalTickHandler.playerPositions.put(player.username, getCoordsAsString(world.provider.dimensionId, x, y, z));
return true;
}
public static class InternalTickHandler implements ITickHandler {
public static Map<String, String> playerPositions = new HashMap();
public static Map<String, String> mappedPositions = new HashMap();
static List<String> removeThese = new ArrayList();
static Map<String, Integer> coordsToCheck = new HashMap();
@Override
public void tickStart(EnumSet<TickType> type, Object... tickData) {
// NO-OP
}
@Override
public void tickEnd(EnumSet<TickType> type, Object... tickData) {
for(String s : coordsToCheck.keySet()) {
int id = getBlockIDAt(s);
decrCoords(s);
if(id == 36) {
int meta = getBlockMetaAt(s);
ForgeDirection dir = ForgeDirection.getOrientation(meta & ~8);
MinecraftServer server = MinecraftServer.getServer();
if(server != null && getTimeInCoords(s) == 0) {
String newPos;
{
String[] tokens = s.split(":");
int worldId = Integer.parseInt(tokens[0]), x = Integer.parseInt(tokens[1]), y = Integer.parseInt(tokens[2]), z = Integer.parseInt(tokens[3]);
World world = server.worldServerForDimension(worldId);
world.setBlock(x + dir.offsetX, y + dir.offsetY, z + dir.offsetZ, ModBlocks.pistonRelay.blockID);
newPos = getCoordsAsString(world.provider.dimensionId, x + dir.offsetX, y + dir.offsetY, z + dir.offsetZ);
}
if(mappedPositions.containsKey(s)) {
String pos = mappedPositions.get(s);
String[] tokens = pos.split(":");
int worldId = Integer.parseInt(tokens[0]), x = Integer.parseInt(tokens[1]), y = Integer.parseInt(tokens[2]), z = Integer.parseInt(tokens[3]);
World world = server.worldServerForDimension(worldId);
int srcId = world.getBlockId(x, y, z);
int srcMeta = world.getBlockMetadata(x, y, z);
TileEntity tile = world.getBlockTileEntity(x, y, z);
if(tile == null && Block.blocksList[srcId] != null && !Block.blocksList[srcId].isAirBlock(world, x, y, z)) {
Material destMat = world.getBlockMaterial(x + dir.offsetX, y + dir.offsetY, z + dir.offsetZ);
if(world.isAirBlock(x + dir.offsetX, y + dir.offsetY, z + dir.offsetZ) || destMat.isReplaceable()) {
world.setBlock(x, y, z, 0);
world.setBlock(x + dir.offsetX, y + dir.offsetY, z + dir.offsetZ, srcId, srcMeta, 1 | 2);
mappedPositions.put(s, getCoordsAsString(world.provider.dimensionId, x + dir.offsetX, y + dir.offsetY, z + dir.offsetZ));
}
}
pos = mappedPositions.get(s);
mappedPositions.remove(s);
mappedPositions.put(newPos, pos);
}
}
}
}
for(String s : removeThese)
coordsToCheck.remove(s);
removeThese.clear();
}
@Override
public EnumSet<TickType> ticks() {
return EnumSet.of(TickType.SERVER);
}
@Override
public String getLabel() {
return LibBlockNames.PISTON_RELAY;
}
}
}
@@ -43,6 +43,7 @@ public final class ModBlocks {
public static Block runeAltar;
public static Block unstableBlock;
public static Block pylon;
public static Block pistonRelay;
public static void init() {
flower = new BlockModFlower();
@@ -55,6 +56,7 @@ public final class ModBlocks {
runeAltar = new BlockRuneAltar();
unstableBlock = new BlockUnstable();
pylon = new BlockPylon();
pistonRelay = new BlockPistonRelay();
for(int i = 0; i < 16; i++)
OreDictionary.registerOre(LibOreDict.FLOWER[i], new ItemStack(flower.blockID, 1, i));
@@ -46,6 +46,7 @@ public final class ConfigHandler {
LibBlockIDs.idRuneAltar = loadBlock(LibBlockNames.RUNE_ALTAR, LibBlockIDs.idRuneAltar);
LibBlockIDs.idUnstableBlock = loadBlock(LibBlockNames.UNSTABLE_BLOCK, LibBlockIDs.idUnstableBlock);
LibBlockIDs.idPylon = loadBlock(LibBlockNames.PYLON, LibBlockIDs.idPylon);
LibBlockIDs.idPistonRelay = loadBlock(LibBlockNames.PISTON_RELAY, LibBlockIDs.idPistonRelay);
// Item IDs
LibItemIDs.idLexicon = loadItem(LibItemNames.LEXICON, LibItemIDs.idLexicon);
@@ -27,6 +27,7 @@ import net.minecraft.world.World;
import vazkii.botania.api.IWandable;
import vazkii.botania.client.core.helper.IconHelper;
import vazkii.botania.common.Botania;
import vazkii.botania.common.block.BlockPistonRelay;
import vazkii.botania.common.core.helper.ItemNBTHelper;
import vazkii.botania.common.lib.LibItemIDs;
import vazkii.botania.common.lib.LibItemNames;
@@ -49,6 +50,14 @@ public class ItemTwigWand extends Item16Colors {
int id = par3World.getBlockId(par4, par5, par6);
if(Block.blocksList[id] instanceof IWandable)
return ((IWandable) Block.blocksList[id]).onUsedByWand(par2EntityPlayer, par1ItemStack, par3World, par4, par5, par6, par7);
else if(BlockPistonRelay.InternalTickHandler.playerPositions.containsKey(par2EntityPlayer.username)) {
String bindPos = BlockPistonRelay.InternalTickHandler.playerPositions.get(par2EntityPlayer.username);
String currentPos = BlockPistonRelay.getCoordsAsString(par3World.provider.dimensionId, par4, par5, par6);
BlockPistonRelay.InternalTickHandler.playerPositions.remove(par2EntityPlayer.username);
BlockPistonRelay.InternalTickHandler.mappedPositions.put(bindPos, currentPos);
}
return false;
}
@@ -23,5 +23,6 @@ public final class LibBlockIDs {
public static int idRuneAltar = 1962;
public static int idUnstableBlock = 1963;
public static int idPylon = 1964;
public static int idPistonRelay = 1965;
}
@@ -25,6 +25,7 @@ public final class LibBlockNames {
public static final String RUNE_ALTAR = LibResources.PREFIX_MOD + "runeAltar";
public static final String UNSTABLE_BLOCK = LibResources.PREFIX_MOD + "unstableBlock";
public static final String PYLON = LibResources.PREFIX_MOD + "pylon";
public static final String PISTON_RELAY = LibResources.PREFIX_MOD + "pistonRelay";
public static final String SPECIAL_FLOWER_PREFIX = FLOWER + ".";