Reimplement into classes and stuff.

This commit is contained in:
James McKay committed 2015-10-05 18:07:24 +11:00
1 parent 6129717798
commit faad24ef08
15 files changed
+460 -30

No files matched your search

+4 -4
View File
@@ -5,7 +5,7 @@ import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraftforge.common.MinecraftForge;
import portablejim.bbw.core.BlockEvents;
import portablejim.bbw.core.items.UnbreakableWand;
import portablejim.bbw.core.items.ItemUnbreakableWand;
/**
* Author: Portablejim
@@ -15,13 +15,13 @@ public class BetterBuildersWandsMod {
public static final String MODID = "BetterBuildersWands";
public static final String VERSION = "0.1";
public static UnbreakableWand unbreakableWand;
public static ItemUnbreakableWand itemUnbreakableWand;
@EventHandler
public void preInit(FMLPreInitializationEvent event) {
unbreakableWand = new UnbreakableWand();
itemUnbreakableWand = new ItemUnbreakableWand();
GameRegistry.registerItem(unbreakableWand, "unbreakableWand");
GameRegistry.registerItem(itemUnbreakableWand, "unbreakableWand");
}
@EventHandler
@@ -1,7 +1,27 @@
package portablejim.bbw.basics;
/**
* Created by james on 3/10/15.
* Enum for the directions that the wands can extend.
*/
public enum EnumLock {
NORTHSOUTH(1),
VERTICAL(2),
VERTICALEASTWEST(3),
EASTWEST(4),
HORIZONTAL(5),
VERTICALNORTHSOUTH(6),
NOLOCK(7);
public final int mask;
EnumLock(int mask) {
this.mask = mask;
}
public static EnumLock fromMask(int inputMask) {
EnumLock locks[] = { NORTHSOUTH, VERTICAL, VERTICALEASTWEST, EASTWEST, HORIZONTAL, VERTICALNORTHSOUTH, NOLOCK };
int safeMask = inputMask & 7;
return locks[safeMask];
}
}
@@ -1,8 +1,81 @@
package portablejim.bbw.basics;
import net.minecraftforge.common.util.ForgeDirection;
/**
* Created by james on 3/10/15.
* Point to represent 3D space. Functions to move in that space.
*/
public class Point3d {
public static int UP = 1;
public static int DOWN = -1;
public static int EAST = 1;
public static int WEST = -1;
public static int SOUTH = 1;
public static int NORTH = -1;
public int x, y, z;
public Point3d(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public Point3d move(ForgeDirection direction) {
int newX = x, newY = y, newZ = z;
switch(direction) {
case UP:
newY += 1;
break;
case DOWN:
newY -= 1;
break;
case NORTH:
newZ -= 1;
break;
case SOUTH:
newZ += 1;
break;
case EAST:
newX += 1;
break;
case WEST:
newX -= 1;
break;
}
return new Point3d(newX, newY, newZ);
}
public Point3d move(int dx, int dy, int dz) {
int newX = x, newY = y, newZ = z;
newX += dx;
newY += dy;
newZ += dz;
return new Point3d(newX, newY, newZ);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point3d that = (Point3d) o;
return x == that.x && y == that.y && z == that.z;
}
@Override
public int hashCode() {
int hashX = (x << 22);
int hashY = (y << 22) >> 10;
int hashZ = (z << 22) >> 20;
return hashX + hashY + hashZ;
}
@Override
public String toString() {
return String.format("(%d,%d,%d)", x, y, z);
}
}
@@ -10,6 +10,6 @@ import net.minecraftforge.client.event.DrawBlockHighlightEvent;
public class BlockEvents {
@SubscribeEvent
public void blockHighlightEvent(DrawBlockHighlightEvent event) {
FMLLog.info("Happened!" + event.target.toString());
//FMLLog.info("Happened!" + event.target.toString());
}
}
@@ -0,0 +1,38 @@
package portablejim.bbw.core;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.Constants;
import portablejim.bbw.IWand;
import portablejim.bbw.basics.EnumLock;
/**
* Common functions across wands.
*/
public class WandUtility {
public static void setMode(ItemStack item, EnumLock mode) {
NBTTagCompound tagCompound = new NBTTagCompound();
if(item.hasTagCompound()) {
tagCompound = item.getTagCompound();
}
NBTTagCompound bbwCompond = new NBTTagCompound();
if(tagCompound.hasKey("bbw", Constants.NBT.TAG_COMPOUND)) {
bbwCompond = tagCompound.getCompoundTag("bbw");
}
short shortMask = (short) (mode.mask & 7);
bbwCompond.setShort("mask", shortMask);
}
public static EnumLock getMode(ItemStack item) {
if(item != null && item.getItem() != null && item.getItem() instanceof IWand) {
NBTTagCompound itemBaseNBT = item.getTagCompound();
if(itemBaseNBT != null && itemBaseNBT.hasKey("bbw", Constants.NBT.TAG_COMPOUND)) {
NBTTagCompound itemNBT = itemBaseNBT.getCompoundTag("bbw");
int mask = itemNBT.hasKey("mask", Constants.NBT.TAG_SHORT) ? itemNBT.getShort("mask") : EnumLock.NOLOCK.mask;
return EnumLock.fromMask(mask);
}
}
return EnumLock.NOLOCK;
}
}
@@ -1,24 +1,106 @@
package portablejim.bbw.core;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.util.ForgeDirection;
import portablejim.bbw.IWand;
import portablejim.bbw.basics.Point3d;
import portablejim.bbw.shims.IPlayerShim;
import portablejim.bbw.shims.IWorldShim;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
/**
* Created by james on 3/10/15.
* Does the heavy work of working out the blocks to place and places them.
*/
public class WandWorker {
public WandWorker() {
private final IWand wand;
private final IPlayerShim player;
private final IWorldShim world;
HashSet<Point3d> allCandidates = new HashSet<Point3d>();
public WandWorker(IWand wand, IPlayerShim player, IWorldShim world) {
this.wand = wand;
this.player = player;
this.world = world;
}
public ArrayList<Point3d> getBlockPositionList(IWand wand, IPlayerShim player) {
return null;
public ItemStack getEquivalentItemStack(Point3d blockPos) {
Block block = world.getBlock(blockPos);
return block == null ? null : new ItemStack(block.getItem(world.getWorld(), blockPos.x, blockPos.y, blockPos.z), 1, block.getDamageValue(world.getWorld(), blockPos.x, blockPos.y, blockPos.z));
}
public void placeBlocks(ArrayList<Point3d> blockPosList, IPlayerShim player) {
public LinkedList<Point3d> getBlockPositionList(Point3d blockLookedAt, ForgeDirection placeDirection, int maxBlocks) {
LinkedList<Point3d> candidates = new LinkedList<Point3d>();
LinkedList<Point3d> toPlace = new LinkedList<Point3d>();
Block targetBlock = world.getBlock(blockLookedAt);
Point3d startingPoint = blockLookedAt.move(placeDirection);
candidates.add(startingPoint);
while(candidates.size() > 0 && toPlace.size() < maxBlocks) {
Point3d currentCandidate = candidates.removeFirst();
Point3d supportingPoint = currentCandidate.move(placeDirection.getOpposite());
Block candidateSupportingBlock = world.getBlock(supportingPoint);
if(world.blockIsAir(currentCandidate)
&& targetBlock.equals(candidateSupportingBlock)
&& allCandidates.add(currentCandidate)) {
toPlace.add(currentCandidate);
switch (placeDirection) {
case DOWN:
case UP:
candidates.add(currentCandidate.move(ForgeDirection.NORTH));
candidates.add(currentCandidate.move(ForgeDirection.EAST));
candidates.add(currentCandidate.move(ForgeDirection.SOUTH));
candidates.add(currentCandidate.move(ForgeDirection.WEST));
candidates.add(currentCandidate.move(ForgeDirection.NORTH).move(ForgeDirection.EAST));
candidates.add(currentCandidate.move(ForgeDirection.NORTH).move(ForgeDirection.WEST));
candidates.add(currentCandidate.move(ForgeDirection.SOUTH).move(ForgeDirection.EAST));
candidates.add(currentCandidate.move(ForgeDirection.SOUTH).move(ForgeDirection.WEST));
break;
case NORTH:
case SOUTH:
candidates.add(currentCandidate.move(ForgeDirection.UP));
candidates.add(currentCandidate.move(ForgeDirection.EAST));
candidates.add(currentCandidate.move(ForgeDirection.DOWN));
candidates.add(currentCandidate.move(ForgeDirection.WEST));
candidates.add(currentCandidate.move(ForgeDirection.UP).move(ForgeDirection.EAST));
candidates.add(currentCandidate.move(ForgeDirection.UP).move(ForgeDirection.WEST));
candidates.add(currentCandidate.move(ForgeDirection.DOWN).move(ForgeDirection.EAST));
candidates.add(currentCandidate.move(ForgeDirection.DOWN).move(ForgeDirection.WEST));
break;
case WEST:
case EAST:
candidates.add(currentCandidate.move(ForgeDirection.UP));
candidates.add(currentCandidate.move(ForgeDirection.NORTH));
candidates.add(currentCandidate.move(ForgeDirection.DOWN));
candidates.add(currentCandidate.move(ForgeDirection.SOUTH));
candidates.add(currentCandidate.move(ForgeDirection.UP).move(ForgeDirection.NORTH));
candidates.add(currentCandidate.move(ForgeDirection.UP).move(ForgeDirection.SOUTH));
candidates.add(currentCandidate.move(ForgeDirection.DOWN).move(ForgeDirection.NORTH));
candidates.add(currentCandidate.move(ForgeDirection.DOWN).move(ForgeDirection.SOUTH));
}
}
}
return toPlace;
}
public void placeBlocks(LinkedList<Point3d> blockPosList, Point3d originalBlock) {
for(Point3d blockPos : blockPosList) {
boolean blockPlaceSuccess = world.copyBlock(originalBlock, blockPos);
if(blockPlaceSuccess) {
boolean takeFromInventory = player.useItem(getEquivalentItemStack(originalBlock));
if(!takeFromInventory) {
world.setBlockToAir(blockPos);
}
}
}
}
}
@@ -0,0 +1,7 @@
package portablejim.bbw.core.items;
/**
* Identifies classes that should be treated as a wand.
*/
public interface IWandItem {
}
@@ -0,0 +1,59 @@
package portablejim.bbw.core.items;
import com.sun.jmx.remote.internal.ArrayQueue;
import cpw.mods.fml.common.FMLLog;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import portablejim.bbw.basics.Point3d;
import portablejim.bbw.core.WandWorker;
import portablejim.bbw.core.wands.UnbreakingWand;
import portablejim.bbw.shims.BasicPlayerShim;
import portablejim.bbw.shims.BasicWorldShim;
import portablejim.bbw.shims.CreativePlayerShim;
import portablejim.bbw.shims.IPlayerShim;
import portablejim.bbw.shims.IWorldShim;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
/**
* The actual item: Simple wand with no durability. Similar to current Builder's Wand.
*/
public class ItemUnbreakableWand extends Item implements IWandItem{
public ItemUnbreakableWand() {
super();
this.setUnlocalizedName("BetterBuildersWands:unbreakableWand");
this.setTextureName("minecraft:stick");
this.setCreativeTab(CreativeTabs.tabTools);
}
public boolean onItemUse(ItemStack itemstack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ) {
IPlayerShim playerShim = new BasicPlayerShim(player);
if(player.capabilities.isCreativeMode) {
playerShim = new CreativePlayerShim();
}
IWorldShim worldShim = new BasicWorldShim(world);
UnbreakingWand unbreakingWand = new UnbreakingWand(itemstack);
WandWorker worker = new WandWorker(unbreakingWand, playerShim, worldShim);
Point3d clickedPos = new Point3d(x, y, z);
ItemStack targetItemstack = worker.getEquivalentItemStack(clickedPos);
int numBlocks = Math.min(unbreakingWand.getMaxBlocks(), playerShim.countItems(targetItemstack));
FMLLog.info("Max blocks: %d (%d|%d", numBlocks, unbreakingWand.getMaxBlocks(), playerShim.countItems(targetItemstack));
LinkedList<Point3d> blocks = worker.getBlockPositionList(clickedPos, ForgeDirection.getOrientation(side), numBlocks);
worker.placeBlocks(blocks, clickedPos);
return true;
}
}
@@ -1,16 +0,0 @@
package portablejim.bbw.core.items;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
/**
* Created by james on 3/10/15.
*/
public class UnbreakableWand extends Item {
public UnbreakableWand() {
super();
this.setUnlocalizedName("BetterBuildersWands:unbreakableWand");
this.setTextureName("minecraft:stick");
this.setCreativeTab(CreativeTabs.tabTools);
}
}
@@ -0,0 +1,32 @@
package portablejim.bbw.core.wands;
import net.minecraft.item.ItemStack;
import portablejim.bbw.IWand;
import portablejim.bbw.basics.EnumLock;
import portablejim.bbw.core.WandUtility;
/**
* Simple wand that doesn't break / have durability.
*/
public class UnbreakingWand implements IWand {
private ItemStack wandItem;
public UnbreakingWand(ItemStack wandItem) {
this.wandItem = wandItem;
}
@Override
public EnumLock getMode() {
return WandUtility.getMode(wandItem);
}
@Override
public int getMaxBlocks() {
return 32;
}
@Override
public boolean placeBlock() {
return true;
}
}
@@ -0,0 +1,52 @@
package portablejim.bbw.shims;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
/**
* Wrap a player to provide basic functions.
*/
public class BasicPlayerShim implements IPlayerShim {
private EntityPlayer player;
public BasicPlayerShim(EntityPlayer player) {
this.player = player;
}
@Override
public int countItems(ItemStack itemStack) {
int total = 0;
if(itemStack == null || player.inventory == null || player.inventory.mainInventory == null) {
return 0;
}
for(ItemStack inventoryStack : player.inventory.mainInventory) {
if(inventoryStack != null && itemStack.isItemEqual(inventoryStack)) {
total += Math.max(0, inventoryStack.stackSize);
}
}
return total;
}
@Override
public boolean useItem(ItemStack itemStack) {
if(itemStack == null || player.inventory == null || player.inventory.mainInventory == null) {
return false;
}
// Reverse direction to leave hotbar to last.
for(int i = player.inventory.mainInventory.length - 1; i >= 0; i--) {
ItemStack inventoryStack = player.inventory.mainInventory[i];
if(inventoryStack != null && itemStack.isItemEqual(inventoryStack)) {
inventoryStack.stackSize -= 1;
if(inventoryStack.stackSize == 0) {
player.inventory.setInventorySlotContents(i, null);
}
return true;
}
}
return false;
}
}
@@ -0,0 +1,49 @@
package portablejim.bbw.shims;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.world.World;
import portablejim.bbw.basics.Point3d;
/**
* Wrap a world to provide basic functions.
*/
public class BasicWorldShim implements IWorldShim {
private World world;
public BasicWorldShim(World world) {
this.world = world;
}
@Override
public Block getBlock(Point3d point) {
if(world != null) {
return world.getBlock(point.x, point.y, point.z);
}
return null;
}
@Override
public boolean blockIsAir(Point3d point) {
Block block = world.getBlock(point.x, point.y, point.z);
return block.isAir(world, point.x, point.y, point.z);
}
@Override
public World getWorld() {
return world;
}
@Override
public boolean copyBlock(Point3d originalBlock, Point3d blockPos) {
Block block = world.getBlock(originalBlock.x, originalBlock.y, originalBlock.z);
int meta = world.getBlockMetadata(originalBlock.x, originalBlock.y, originalBlock.z);
return world.setBlock(blockPos.x, blockPos.y, blockPos.z, block, meta, 3);
}
@Override
public void setBlockToAir(Point3d blockPos) {
world.setBlock(blockPos.x, blockPos.y, blockPos.z, Blocks.air);
}
}
@@ -0,0 +1,18 @@
package portablejim.bbw.shims;
import net.minecraft.item.ItemStack;
/**
* Wrap functions for a creative player.
*/
public class CreativePlayerShim implements IPlayerShim {
@Override
public int countItems(ItemStack itemStack) {
return Integer.MAX_VALUE;
}
@Override
public boolean useItem(ItemStack itemStack) {
return true;
}
}
@@ -1,7 +1,11 @@
package portablejim.bbw.shims;
import net.minecraft.item.ItemStack;
/**
* Created by james on 3/10/15.
* Wrap player functions.
*/
public interface IPlayerShim {
int countItems(ItemStack itemStack);
boolean useItem(ItemStack itemStack);
}
@@ -1,7 +1,19 @@
package portablejim.bbw.shims;
import net.minecraft.block.Block;
import net.minecraft.world.World;
import portablejim.bbw.basics.Point3d;
/**
* Created by james on 3/10/15.
* Wrap functions to do with the world.
*/
public interface IWorldShim {
Block getBlock(Point3d point);
boolean blockIsAir(Point3d point);
World getWorld();
boolean copyBlock(Point3d originalBlock, Point3d blockPos);
void setBlockToAir(Point3d blockPos);
}