Begin refactor, make CarvingVariation and CarvingGroup interfaces

This commit is contained in:
tterrag1098 committed 2015-01-15 00:03:43 -05:00
1 parent b268ebeca0
commit 155bc00f0e
12 files changed
+206 -75

No files matched your search

@@ -1045,7 +1045,6 @@ public enum Features {
ice_pillar.carverHelper.addVariation("tile.icePillar.5.desc", 5, "icepillar/carved"); ice_pillar.carverHelper.addVariation("tile.icePillar.5.desc", 5, "icepillar/carved");
ice_pillar.carverHelper.addVariation("tile.icePillar.6.desc", 6, "icepillar/ornamental"); ice_pillar.carverHelper.addVariation("tile.icePillar.6.desc", 6, "icepillar/ornamental");
ice_pillar.carverHelper.register(ice_pillar, "ice_pillar"); ice_pillar.carverHelper.register(ice_pillar, "ice_pillar");
Carving.chisel.setGroupClass("ice_pillar", "ice");
} }
@Override @Override
@@ -1465,7 +1464,6 @@ public enum Features {
marble_pillar.carverHelper.addVariation("tile.marblePillar.15.desc", 15, "marblepillar/ornamental"); marble_pillar.carverHelper.addVariation("tile.marblePillar.15.desc", 15, "marblepillar/ornamental");
} }
marble_pillar.carverHelper.register(marble_pillar, "marble_pillar"); marble_pillar.carverHelper.register(marble_pillar, "marble_pillar");
Carving.chisel.setGroupClass("marble_pillar", "marble");
BlockCarvableSlab marble_pillar_slab = (BlockCarvableSlab) new BlockCarvableSlab(marble_pillar).setCreativeTab(ChiselTabs.tabStoneChiselBlocks).setHardness(2.0F).setResistance(10F) BlockCarvableSlab marble_pillar_slab = (BlockCarvableSlab) new BlockCarvableSlab(marble_pillar).setCreativeTab(ChiselTabs.tabStoneChiselBlocks).setHardness(2.0F).setResistance(10F)
.setStepSound(Block.soundTypeStone); .setStepSound(Block.soundTypeStone);
@@ -1639,7 +1637,6 @@ public enum Features {
packedice_pillar.carverHelper.addVariation("tile.packedicePillar.5.desc", 5, "icepillar/carved"); packedice_pillar.carverHelper.addVariation("tile.packedicePillar.5.desc", 5, "icepillar/carved");
packedice_pillar.carverHelper.addVariation("tile.packedicePillar.6.desc", 6, "icepillar/ornamental"); packedice_pillar.carverHelper.addVariation("tile.packedicePillar.6.desc", 6, "icepillar/ornamental");
packedice_pillar.carverHelper.register(packedice_pillar, "packedice_pillar"); packedice_pillar.carverHelper.register(packedice_pillar, "packedice_pillar");
Carving.chisel.setGroupClass("packedice_pillar", "packedice");
} }
}, },
@@ -0,0 +1,17 @@
package com.cricketcraft.chisel.api.carving;
public class CarvingUtils {
/**
* The default implementation of {@link ICarvingVariation#compareTo(ICarvingVariation)}
*
* @param v1
* The first {@link ICarvingVariation variation}. Usually 'this'.
* @param v2
* The second {@link ICarvingVariation variation}. Usually the parameter to the {@code comapreTo} method.
* @return A positive integer if the first's order is greater, a negative integer if the second's is greater, and 0 if they are equal.
*/
public static int compare(ICarvingVariation v1, ICarvingVariation v2) {
return v1.getOrder() - v2.getOrder();
}
}
@@ -0,0 +1,58 @@
package com.cricketcraft.chisel.api.carving;
import java.util.List;
public interface ICarvingGroup {
/**
* The name of this group. Used for internal identification.
*
* @return A string name for the group
*/
String getName();
/**
* Can return null. If null, fallback sound will be used (the standard chisel sound).
*
* @return The string resource path of the sound to use for chiseling items in this group
*/
String getSound();
/**
* Sets the sound of this group
*
* @param sound
* A string resource path for the sound this group makes when chiseled
*/
void setSound(String sound);
/**
* The oredict name to match to this group. All items with this oredict name will be assumed to be part of this group.
*
* @return An ore dictionary name
*/
String getOreName();
/**
* Sets the oredict name for this group.
*
* @param oreName
* The String oredict name to be associated with this group.
*/
void setOreName(String oreName);
/**
* Gets all carving variations associated with this group.
*
* @return A {@link List} of {@link ICarvingVariation}s
*/
List<ICarvingVariation> getVariations();
/**
* Adds a variation to this group.
*
* @param variation
* An {@link ICarvingVariation} to add to this group
*/
void addVariation(ICarvingVariation variation);
}
@@ -0,0 +1,34 @@
package com.cricketcraft.chisel.api.carving;
import net.minecraft.block.Block;
import net.minecraft.item.ItemStack;
/**
* Represents a variation of a chiselable block.
* <p>
* Do note that while this interface extends {@link Comparable}, a default implementation of {@link Comparable#compareTo(Object)} is provided in
* {@link CarvingUtils#compare(ICarvingVariation, ICarvingVariation)}
*/
public interface ICarvingVariation extends Comparable<ICarvingVariation> {
Block getBlock();
int getBlockMeta();
int getItemMeta();
/**
* Gets an {@link ItemStack} representing this variation.
*
* @return An {@link ItemStack} to represent this variation
*/
ItemStack getStack();
/**
* The "order" of this variation. Represents its position in the list of variations.
*
* @return An integer to represent the position of this variation in the list of all variations in the group
*/
int getOrder();
}
@@ -284,7 +284,6 @@ public class CarvableHelper {
if (!forbidChiseling) { if (!forbidChiseling) {
Carving.chisel.addVariation(name + ".top", slab.top, variation.metadata, 0); Carving.chisel.addVariation(name + ".top", slab.top, variation.metadata, 0);
Carving.chisel.setGroupClass(name + ".top", name);
} }
} }
} }
@@ -11,6 +11,7 @@ import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary; import net.minecraftforge.oredict.OreDictionary;
import com.cricketcraft.chisel.Chisel; import com.cricketcraft.chisel.Chisel;
import com.cricketcraft.chisel.api.carving.ICarvingVariation;
public class Carving { public class Carving {
@@ -29,26 +30,12 @@ public class Carving {
return Block.blockRegistry.getNameForObject(block) + "|" + metadata; return Block.blockRegistry.getNameForObject(block) + "|" + metadata;
} }
public boolean isVariationOfSameClass(Block target, int metadata1, Block block2, int metadata2) { public List<ICarvingVariation> getVariations(Block block, int metadata) {
CarvingGroup group1 = carvingGroupsByVariation.get(key(target, metadata1));
if (group1 == null)
return false;
CarvingGroup group2 = carvingGroupsByVariation.get(key(block2, metadata2));
if (group2 == null)
return false;
return group1 == group2 || group1.className.equals(group2.className) && !group1.className.isEmpty();
}
public CarvingVariation[] getVariations(Block block, int metadata) {
CarvingGroup group = getGroup(block, metadata); CarvingGroup group = getGroup(block, metadata);
if (group == null) if (group == null)
return null; return null;
CarvingVariation[] res = group.variations.toArray(new CarvingVariation[group.variations.size()]); return group.getVariations();
return res;
} }
public String getOre(Block block, int metadata) { public String getOre(Block block, int metadata) {
@@ -56,7 +43,7 @@ public class Carving {
if (group == null) if (group == null)
return null; return null;
return group.oreName; return group.getOreName();
} }
public ArrayList<ItemStack> getItems(ItemStack chiseledItem) { public ArrayList<ItemStack> getItems(ItemStack chiseledItem) {
@@ -70,20 +57,23 @@ public class Carving {
HashMap<String, Integer> mapping = new HashMap<String, Integer>(); HashMap<String, Integer> mapping = new HashMap<String, Integer>();
if (group.variations != null) { List<ICarvingVariation> variations = group.getVariations();
for (CarvingVariation v : group.variations) {
String key = Block.getIdFromBlock(v.block) + "|" + v.damage; if (!group.getVariations().isEmpty()) {
for (ICarvingVariation v : variations) {
String key = Block.getIdFromBlock(v.getBlock()) + "|" + v.getItemMeta();
if (mapping.containsKey(key)) if (mapping.containsKey(key))
continue; continue;
mapping.put(key, 1); mapping.put(key, 1);
items.add(new ItemStack(v.block, 1, v.damage)); items.add(new ItemStack(v.getBlock(), 1, v.getItemMeta()));
} }
} }
ArrayList<ItemStack> ores; ArrayList<ItemStack> ores;
if (group.oreName != null && ((ores = OreDictionary.getOres(group.oreName)) != null)) { String oreName = group.getOreName();
if (oreName != null && ((ores = OreDictionary.getOres(oreName)) != null)) {
for (ItemStack stack : ores) { for (ItemStack stack : ores) {
String key = Item.getIdFromItem(stack.getItem()) + "|" + stack.getItemDamage(); String key = Item.getIdFromItem(stack.getItem()) + "|" + stack.getItemDamage();
@@ -124,7 +114,6 @@ public class Carving {
CarvingGroup group = carvingGroupsByName.get(name); CarvingGroup group = carvingGroupsByName.get(name);
if (group == null) { if (group == null) {
group = new CarvingGroup(name); group = new CarvingGroup(name);
group.className = name;
carvingGroupsByName.put(name, group); carvingGroupsByName.put(name, group);
} }
@@ -139,8 +128,7 @@ public class Carving {
return null; return null;
CarvingVariation variation = new CarvingVariation(block, metadata, order); CarvingVariation variation = new CarvingVariation(block, metadata, order);
group.variations.add(variation); group.addVariation(variation);
Collections.sort(group.variations);
carvingGroupsByVariation.put(key(block, metadata), group); carvingGroupsByVariation.put(key(block, metadata), group);
return variation; return variation;
@@ -151,18 +139,12 @@ public class Carving {
carvingGroupsByOre.put(oreName, group); carvingGroupsByOre.put(oreName, group);
group.oreName = oreName; group.setOreName(oreName);
}
public void setGroupClass(String name, String className) {
CarvingGroup group = getGroup(name);
group.className = className;
} }
public void setVariationSound(String name, String sound) { public void setVariationSound(String name, String sound) {
CarvingGroup group = getGroup(name); CarvingGroup group = getGroup(name);
group.sound = sound; group.setSound(sound);
} }
public String getVariationSound(Block block, int metadata) { public String getVariationSound(Block block, int metadata) {
@@ -171,10 +153,10 @@ public class Carving {
public String getVariationSound(Item item, int metadata) { public String getVariationSound(Item item, int metadata) {
CarvingGroup blockGroup = carvingGroupsByVariation.get(key(item, metadata)); CarvingGroup blockGroup = carvingGroupsByVariation.get(key(item, metadata));
if (blockGroup == null || blockGroup.sound == null) if (blockGroup == null || blockGroup.getSound() == null)
return Chisel.MOD_ID + ":chisel.fallback"; return Chisel.MOD_ID + ":chisel.fallback";
return blockGroup.sound; return blockGroup.getSound();
} }
public List<String> getSortedGroupNames() { public List<String> getSortedGroupNames() {
@@ -1,33 +1,57 @@
package com.cricketcraft.chisel.carving; package com.cricketcraft.chisel.carving;
import java.util.ArrayList; import java.util.Collections;
import java.util.List; import java.util.List;
import com.cricketcraft.chisel.api.carving.ICarvingGroup;
import com.cricketcraft.chisel.api.carving.ICarvingVariation;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
public class CarvingGroup { public class CarvingGroup implements ICarvingGroup {
public CarvingGroup(String n) { private String name;
name = n; private String sound;
private String oreName;
private List<ICarvingVariation> variations = Lists.newArrayList();
public CarvingGroup(String name) {
this.name = name;
} }
String name; public List<ICarvingVariation> getVariations() {
String className;
String sound;
String oreName;
List<CarvingVariation> variations = new ArrayList<CarvingVariation>();
public List<CarvingVariation> getVariations() {
return ImmutableList.copyOf(variations); return ImmutableList.copyOf(variations);
} }
@Override
public void addVariation(ICarvingVariation variation) {
variations.add(variation);
Collections.sort(variations);
}
@Override
public String getName() { public String getName() {
return name; return name;
} }
@Override
public String getSound() {
return sound;
}
@Override
public void setSound(String sound) {
this.sound = sound;
}
@Override
public String getOreName() { public String getOreName() {
return oreName; return oreName;
} }
@Override
public void setOreName(String oreName) {
this.oreName = oreName;
}
} }
@@ -3,7 +3,15 @@ package com.cricketcraft.chisel.carving;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
public class CarvingVariation implements Comparable<CarvingVariation> { import com.cricketcraft.chisel.api.carving.CarvingUtils;
import com.cricketcraft.chisel.api.carving.ICarvingVariation;
public class CarvingVariation implements ICarvingVariation {
private int order;
private Block block;
private int meta;
private int damage;
public CarvingVariation(Block block, int metadata, int ord) { public CarvingVariation(Block block, int metadata, int ord) {
this.order = ord; this.order = ord;
@@ -13,17 +21,32 @@ public class CarvingVariation implements Comparable<CarvingVariation> {
} }
@Override @Override
public int compareTo(CarvingVariation a) {
return order - a.order;
}
public int order;
public Block block;
public int meta;
public int damage;
public ItemStack getStack() { public ItemStack getStack() {
return new ItemStack(block, 1, damage); return new ItemStack(block, 1, damage);
} }
@Override
public Block getBlock() {
return block;
}
@Override
public int getBlockMeta() {
return meta;
}
@Override
public int getItemMeta() {
return damage;
}
@Override
public int getOrder() {
return order;
}
@Override
public int compareTo(ICarvingVariation o) {
return CarvingUtils.compare(this, o);
}
} }
@@ -15,6 +15,7 @@ import codechicken.nei.NEIServerUtils;
import codechicken.nei.PositionedStack; import codechicken.nei.PositionedStack;
import codechicken.nei.recipe.TemplateRecipeHandler; import codechicken.nei.recipe.TemplateRecipeHandler;
import com.cricketcraft.chisel.api.carving.ICarvingVariation;
import com.cricketcraft.chisel.carving.Carving; import com.cricketcraft.chisel.carving.Carving;
import com.cricketcraft.chisel.carving.CarvingGroup; import com.cricketcraft.chisel.carving.CarvingGroup;
import com.cricketcraft.chisel.carving.CarvingVariation; import com.cricketcraft.chisel.carving.CarvingVariation;
@@ -164,7 +165,7 @@ public class RecipeHandlerChisel extends TemplateRecipeHandler {
private static List<ItemStack> getVariationStacks(CarvingGroup g) { private static List<ItemStack> getVariationStacks(CarvingGroup g) {
List<ItemStack> stacks = new ArrayList<ItemStack>(); List<ItemStack> stacks = new ArrayList<ItemStack>();
for (CarvingVariation v : g.getVariations()) { for (ICarvingVariation v : g.getVariations()) {
stacks.add(v.getStack()); stacks.add(v.getStack());
} }
String oreName = g.getOreName(); String oreName = g.getOreName();
@@ -49,28 +49,28 @@ public final class ChiselController {
z = event.z; z = event.z;
Block block = event.world.getBlock(x, y, z); Block block = event.world.getBlock(x, y, z);
int metadata = event.world.getBlockMetadata(x, y, z); int metadata = event.world.getBlockMetadata(x, y, z);
CarvingVariation[] variations = Carving.chisel.getVariations(block, metadata); CarvingVariation[] variations = Carving.chisel.getVariations(block, metadata).toArray(new CarvingVariation[] {});
if (variations != null && chisel.canChiselBlock(event.world, x, y, z, block, metadata)) { if (variations != null && chisel.canChiselBlock(event.world, x, y, z, block, metadata)) {
ItemStack target = General.getChiselTarget(held); ItemStack target = General.getChiselTarget(held);
if (target != null) { if (target != null) {
for (CarvingVariation v : variations) { for (CarvingVariation v : variations) {
if (v.block == Block.getBlockFromItem(target.getItem()) && v.meta == target.getItemDamage()) { if (v.getBlock() == Block.getBlockFromItem(target.getItem()) && v.getBlockMeta() == target.getItemDamage()) {
setVariation(event.entityPlayer, event.world, x, y, z, v, target); setVariation(event.entityPlayer, event.world, x, y, z, v, target);
} }
} }
} else { } else {
for (int i = 0; i < variations.length; i++) { for (int i = 0; i < variations.length; i++) {
CarvingVariation v = variations[i]; CarvingVariation v = variations[i];
if (v.block == block && v.meta == metadata) { if (v.getBlock() == block && v.getBlockMeta() == metadata) {
variations = ArrayUtils.remove(variations, i--); variations = ArrayUtils.remove(variations, i--);
} }
} }
int index = event.world.rand.nextInt(variations.length); int index = event.world.rand.nextInt(variations.length);
CarvingVariation newVar = variations[index]; CarvingVariation newVar = variations[index];
setVariation(event.entityPlayer, event.world, x, y, z, newVar, new ItemStack(newVar.block, 1, newVar.damage)); setVariation(event.entityPlayer, event.world, x, y, z, newVar, new ItemStack(newVar.getBlock(), 1, newVar.getItemMeta()));
event.entityPlayer.inventory.currentItem = slot; event.entityPlayer.inventory.currentItem = slot;
} }
} }
@@ -89,8 +89,7 @@ public final class ChiselController {
*/ */
private void setVariation(EntityPlayer player, World world, int x, int y, int z, CarvingVariation v, ItemStack target) { private void setVariation(EntityPlayer player, World world, int x, int y, int z, CarvingVariation v, ItemStack target) {
PacketHandler.INSTANCE.sendTo(new MessageChiselSound(x, y, z, v), (EntityPlayerMP) player); PacketHandler.INSTANCE.sendTo(new MessageChiselSound(x, y, z, v), (EntityPlayerMP) player);
world.setBlock(x, y, z, v.block); world.setBlock(x, y, z, v.getBlock(), v.getBlockMeta(), 3);
world.setBlockMetadataWithNotify(x, y, z, v.meta, 3);
((IChiselItem) player.getCurrentEquippedItem().getItem()).onChisel(world, player.inventory, player.inventory.currentItem, player.getCurrentEquippedItem(), target); ((IChiselItem) player.getCurrentEquippedItem().getItem()).onChisel(world, player.inventory, player.inventory.currentItem, player.getCurrentEquippedItem(), target);
} }
} }
@@ -17,7 +17,6 @@ import com.cricketcraft.chisel.api.ChiselMode;
import com.cricketcraft.chisel.api.IChiselItem; import com.cricketcraft.chisel.api.IChiselItem;
import com.cricketcraft.chisel.carving.Carving; import com.cricketcraft.chisel.carving.Carving;
import com.cricketcraft.chisel.config.Configurations; import com.cricketcraft.chisel.config.Configurations;
import com.cricketcraft.chisel.init.ChiselBlocks;
import com.cricketcraft.chisel.utils.GeneralClient; import com.cricketcraft.chisel.utils.GeneralClient;
public class ItemChisel extends Item implements IChiselItem { public class ItemChisel extends Item implements IChiselItem {
@@ -104,10 +103,8 @@ public class ItemChisel extends Item implements IChiselItem {
String sound = carving.getVariationSound(target.getItem(), target.getItemDamage()); String sound = carving.getVariationSound(target.getItem(), target.getItemDamage());
EntityPlayer player = Chisel.proxy.getClientPlayer(); EntityPlayer player = Chisel.proxy.getClientPlayer();
GeneralClient.playChiselSound(world, MathHelper.floor_double(player.posX), MathHelper.floor_double(player.posY), MathHelper.floor_double(player.posZ), sound); GeneralClient.playChiselSound(world, MathHelper.floor_double(player.posX), MathHelper.floor_double(player.posY), MathHelper.floor_double(player.posZ), sound);
} else { } else if (chisel.getItemDamage() >= chisel.getMaxDamage()) {
if (chisel.getItemDamage() >= chisel.getMaxDamage()) { inv.decrStackSize(slot, 1);
inv.decrStackSize(slot, 1);
}
} }
} }
@@ -23,8 +23,8 @@ public class MessageChiselSound extends MessageCoords {
public MessageChiselSound(int x, int y, int z, CarvingVariation v) { public MessageChiselSound(int x, int y, int z, CarvingVariation v) {
super(x, y, z); super(x, y, z);
this.block = Block.getIdFromBlock(v.block); this.block = Block.getIdFromBlock(v.getBlock());
this.meta = (byte) v.meta; this.meta = (byte) v.getBlockMeta();
} }
@Override @Override