Skip to content

Commit

Permalink
Added IPartConverter2, a version of IPartConverter with a "simulated"…
Browse files Browse the repository at this point in the history
… flag for conversion. Fixes #6
  • Loading branch information
amadornes committed Jan 10, 2016
1 parent c7a8d93 commit b411c6c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
22 changes: 21 additions & 1 deletion src/main/java/mcmultipart/multipart/IPartConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,40 @@
import net.minecraft.world.IBlockAccess;

/**
* Implement this interface to allow conversion of normal blocks into multiparts.
* DO NOT IMPLEMENT THIS. USE {@link IPartConverter2}!
*/
public interface IPartConverter {

/**
* Gets the blocks that can be converted by this class.
*/
@Deprecated
public Collection<Block> getConvertableBlocks();

/**
* Converts a block into a collection of multiparts.
*/
@Deprecated
public Collection<? extends IMultipart> convertBlock(IBlockAccess world, BlockPos pos);

/**
* Implement this interface to allow conversion of normal blocks into multiparts.
*/
public static interface IPartConverter2 {

/**
* Gets the blocks that can be converted by this class.
*/
public Collection<Block> getConvertableBlocks();

/**
* Converts a block into a collection of multiparts. Simulated determines whether we're getting the parts to perform checks or to
* actually convert the block.
*/
public Collection<? extends IMultipart> convertBlock(IBlockAccess world, BlockPos pos, boolean simulated);

}

/**
* Implement this interface to allow reverse conversion of multiparts into blocks.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mcmultipart/multipart/MultipartHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static boolean canAddPart(World world, BlockPos pos, IMultipart part) {
for (AxisAlignedBB bb : list)
if (!world.checkNoEntityCollision(bb.offset(pos.getX(), pos.getY(), pos.getZ()))) return false;

Collection<? extends IMultipart> parts = MultipartRegistry.convert(world, pos);
Collection<? extends IMultipart> parts = MultipartRegistry.convert(world, pos, true);
if (parts != null && !parts.isEmpty()) {
TileMultipart tmp = new TileMultipart();
for (IMultipart p : parts)
Expand Down Expand Up @@ -103,7 +103,7 @@ public static IMultipartContainer getOrConvertPartContainer(World world, BlockPo
IMultipartContainer container = getPartContainer(world, pos);
if (container != null) return container;

Collection<? extends IMultipart> parts = MultipartRegistry.convert(world, pos);
Collection<? extends IMultipart> parts = MultipartRegistry.convert(world, pos, false);
if (parts == null || parts.isEmpty()) return null;

if (doConvert) {
Expand Down
44 changes: 39 additions & 5 deletions src/main/java/mcmultipart/multipart/MultipartRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Map;
import java.util.Set;

import mcmultipart.multipart.IPartConverter.IPartConverter2;
import mcmultipart.multipart.IPartConverter.IReversePartConverter;
import mcmultipart.multipart.IPartFactory.IAdvancedPartFactory;
import net.minecraft.block.Block;
Expand All @@ -28,7 +29,7 @@ public class MultipartRegistry {
private static Map<String, IAdvancedPartFactory> partProviders = new HashMap<String, IAdvancedPartFactory>();
private static BiMap<String, Class<? extends IMultipart>> partClasses = HashBiMap.create();

private static Map<Block, IPartConverter> converters = new HashMap<Block, IPartConverter>();
private static Map<Block, IPartConverter2> converters = new HashMap<Block, IPartConverter2>();
private static List<IReversePartConverter> reverseConverters = new ArrayList<IReversePartConverter>();

@Deprecated
Expand Down Expand Up @@ -89,10 +90,19 @@ public static void registerPart(Class<? extends IMultipart> clazz, String identi
}

/**
* Registers an {@link IPartConverter}.
* Registers an {@link IPartConverter}. USE {@link IPartConverter2}!
*/
@Deprecated
public static void registerPartConverter(IPartConverter converter) {

registerPartConverter(new WrappedPartConverter(converter));
}

/**
* Registers an {@link IPartConverter2}.
*/
public static void registerPartConverter(IPartConverter2 converter) {

for (Block block : converter.getConvertableBlocks())
converters.put(block, converter);
}
Expand Down Expand Up @@ -175,10 +185,10 @@ public static boolean hasRegisteredParts() {
/**
* Converts the block at the specified location into a collection of multiparts. Doesn't actually replace the block.
*/
public static Collection<? extends IMultipart> convert(IBlockAccess world, BlockPos pos) {
public static Collection<? extends IMultipart> convert(IBlockAccess world, BlockPos pos, boolean simulated) {

IPartConverter converter = converters.get(world.getBlockState(pos).getBlock());
if (converter != null) return converter.convertBlock(world, pos);
IPartConverter2 converter = converters.get(world.getBlockState(pos).getBlock());
if (converter != null) return converter.convertBlock(world, pos, simulated);
return null;
}

Expand Down Expand Up @@ -247,4 +257,28 @@ public IMultipart createPart(String type, NBTTagCompound tag) {
}
}

@SuppressWarnings("deprecation")
private static class WrappedPartConverter implements IPartConverter2 {

private final IPartConverter converter;

public WrappedPartConverter(IPartConverter converter) {

this.converter = converter;
}

@Override
public Collection<Block> getConvertableBlocks() {

return converter.getConvertableBlocks();
}

@Override
public Collection<? extends IMultipart> convertBlock(IBlockAccess world, BlockPos pos, boolean simulated) {

return converter.convertBlock(world, pos);
}

}

}

0 comments on commit b411c6c

Please sign in to comment.