refactor library API to be more flexible

This commit is contained in:
Adrian 2015-06-04 12:06:23 +02:00
parent a93b5990f9
commit 2b906fb802
11 changed files with 124 additions and 65 deletions

View file

@ -1,14 +0,0 @@
package buildcraft.api.library;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
public interface ILibraryTypeHandler {
boolean isHandler(ItemStack stack, boolean store);
String getFileExtension();
int getTextColor();
String getName(ItemStack stack);
ItemStack load(ItemStack stack, NBTTagCompound compound);
boolean store(ItemStack stack, NBTTagCompound compound);
}

View file

@ -1,28 +1,29 @@
package buildcraft.api.library;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public final class LibraryAPI {
private static final Set<ILibraryTypeHandler> handlers = new HashSet<ILibraryTypeHandler>();
private static final Map<String, ILibraryTypeHandler> handlersByExt = new HashMap<String, ILibraryTypeHandler>();
private static final Set<LibraryTypeHandler> handlers = new HashSet<LibraryTypeHandler>();
private LibraryAPI() {
}
public static Set<ILibraryTypeHandler> getHandlerSet() {
public static Set<LibraryTypeHandler> getHandlerSet() {
return handlers;
}
public static void registerHandler(ILibraryTypeHandler handler) {
public static void registerHandler(LibraryTypeHandler handler) {
handlers.add(handler);
handlersByExt.put(handler.getFileExtension(), handler);
}
public static ILibraryTypeHandler getHandler(String ext) {
return handlersByExt.get(ext);
public static LibraryTypeHandler getHandlerFor(String extension) {
for(LibraryTypeHandler h : handlers) {
if (h.isInputExtension(extension)) {
return h;
}
}
return null;
}
}

View file

@ -0,0 +1,28 @@
package buildcraft.api.library;
import net.minecraft.item.ItemStack;
public abstract class LibraryTypeHandler {
public enum HandlerType {
LOAD, STORE
}
private final String extension;
public LibraryTypeHandler(String extension) {
this.extension = extension;
}
public abstract boolean isHandler(ItemStack stack, HandlerType type);
public boolean isInputExtension(String ext) {
return extension.equals(ext);
}
public String getOutputExtension() {
return extension;
}
public abstract int getTextColor();
public abstract String getName(ItemStack stack);
}

View file

@ -0,0 +1,12 @@
package buildcraft.api.library;
import net.minecraft.item.ItemStack;
public abstract class LibraryTypeHandlerByteArray extends LibraryTypeHandler {
public LibraryTypeHandlerByteArray(String extension) {
super(extension);
}
public abstract ItemStack load(ItemStack stack, byte[] data);
public abstract byte[] store(ItemStack stack);
}

View file

@ -0,0 +1,13 @@
package buildcraft.api.library;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
public abstract class LibraryTypeHandlerNBT extends LibraryTypeHandler {
public LibraryTypeHandlerNBT(String extension) {
super(extension);
}
public abstract ItemStack load(ItemStack stack, NBTTagCompound nbt);
public abstract boolean store(ItemStack stack, NBTTagCompound nbt);
}

View file

@ -6,7 +6,7 @@
* Please check the contents of the license, which should be located
* as "LICENSE.API" in the BuildCraft source code distribution.
*/
@API(apiVersion = "1.0", owner = "BuildCraftAPI|core", provides = "BuildCraftAPI|library")
@API(apiVersion = "2.0", owner = "BuildCraftAPI|core", provides = "BuildCraftAPI|library")
package buildcraft.api.library;
import cpw.mods.fml.common.API;

View file

@ -4,19 +4,20 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import buildcraft.BuildCraftBuilders;
import buildcraft.api.library.ILibraryTypeHandler;
import buildcraft.api.library.LibraryTypeHandlerNBT;
import buildcraft.core.blueprints.BlueprintBase;
import buildcraft.core.blueprints.LibraryId;
public class LibraryBlueprintTypeHandler implements ILibraryTypeHandler {
public class LibraryBlueprintTypeHandler extends LibraryTypeHandlerNBT {
private final boolean isBlueprint;
public LibraryBlueprintTypeHandler(boolean isBlueprint) {
super(isBlueprint ? "bpt" : "tpl");
this.isBlueprint = isBlueprint;
}
@Override
public boolean isHandler(ItemStack stack, boolean store) {
public boolean isHandler(ItemStack stack, HandlerType type) {
if (isBlueprint) {
return stack.getItem() instanceof ItemBlueprintStandard;
} else {
@ -24,11 +25,6 @@ public class LibraryBlueprintTypeHandler implements ILibraryTypeHandler {
}
}
@Override
public String getFileExtension() {
return isBlueprint ? "bpt" : "tpl";
}
@Override
public int getTextColor() {
return isBlueprint ? 0x305080 : 0;
@ -43,7 +39,7 @@ public class LibraryBlueprintTypeHandler implements ILibraryTypeHandler {
public ItemStack load(ItemStack stack, NBTTagCompound compound) {
BlueprintBase blueprint = BlueprintBase.loadBluePrint((NBTTagCompound) compound.copy());
blueprint.id.name = compound.getString("__filename");
blueprint.id.extension = getFileExtension();
blueprint.id.extension = getOutputExtension();
BuildCraftBuilders.serverDB.add(blueprint.id, compound);
return blueprint.getStack();
}

View file

@ -4,24 +4,23 @@ import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import buildcraft.api.library.ILibraryTypeHandler;
import buildcraft.api.library.LibraryTypeHandlerNBT;
import buildcraft.core.lib.utils.NBTUtils;
public class LibraryBookTypeHandler implements ILibraryTypeHandler {
public class LibraryBookTypeHandler extends LibraryTypeHandlerNBT {
public LibraryBookTypeHandler() {
super("book");
}
@Override
public boolean isHandler(ItemStack stack, boolean store) {
if (store) {
public boolean isHandler(ItemStack stack, HandlerType type) {
if (type == HandlerType.STORE) {
return stack.getItem() == Items.written_book;
} else {
return stack.getItem() == Items.writable_book || stack.getItem() == Items.written_book;
}
}
@Override
public String getFileExtension() {
return "book";
}
@Override
public int getTextColor() {
return 0x684804;

View file

@ -132,7 +132,7 @@ public class LibraryDatabase {
public boolean accept(File dir, String name) {
int dotIndex = name.lastIndexOf('.') + 1;
String extension = name.substring(dotIndex);
return LibraryAPI.getHandler(extension) != null;
return LibraryAPI.getHandlerFor(extension) != null;
}
};

View file

@ -23,8 +23,10 @@ import cpw.mods.fml.relauncher.Side;
import buildcraft.BuildCraftBuilders;
import buildcraft.BuildCraftCore;
import buildcraft.api.library.ILibraryTypeHandler;
import buildcraft.api.library.LibraryAPI;
import buildcraft.api.library.LibraryTypeHandler;
import buildcraft.api.library.LibraryTypeHandlerByteArray;
import buildcraft.api.library.LibraryTypeHandlerNBT;
import buildcraft.core.blueprints.LibraryId;
import buildcraft.core.lib.block.TileBuildCraft;
import buildcraft.core.lib.inventory.SimpleInventory;
@ -88,7 +90,7 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory,
pageId++;
}
setCurrentPage(BuildCraftBuilders.clientDB.getPage (pageId));
setCurrentPage(BuildCraftBuilders.clientDB.getPage(pageId));
}
public void pagePrev () {
@ -161,7 +163,7 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory,
inv.setInventorySlotContents(i, itemstack);
if (i == 0) {
if (getStackInSlot(0) != null && findHandler(0, true) != null) {
if (getStackInSlot(0) != null && findHandler(0, LibraryTypeHandler.HandlerType.STORE) != null) {
progressIn = 1;
} else {
progressIn = 0;
@ -169,7 +171,7 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory,
}
if (i == 2) {
if (getStackInSlot(2) != null && findHandler(2, false) != null) {
if (getStackInSlot(2) != null && findHandler(2, LibraryTypeHandler.HandlerType.LOAD) != null) {
progressOut = 1;
} else {
progressOut = 0;
@ -210,11 +212,11 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory,
public void closeInventory() {
}
private ILibraryTypeHandler findHandler(int slot, boolean store) {
private LibraryTypeHandler findHandler(int slot, LibraryTypeHandler.HandlerType type) {
ItemStack stack = getStackInSlot(slot);
for (ILibraryTypeHandler h : LibraryAPI.getHandlerSet()) {
if (h.isHandler(stack, store)) {
for (LibraryTypeHandler h : LibraryAPI.getHandlerSet()) {
if (h.isHandler(stack, type)) {
return h;
}
}
@ -245,15 +247,25 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory,
// On progress IN, we'll download the blueprint from the server to the
// client, and then store it to the client.
if (progressIn == 100 && getStackInSlot(1) == null) {
final NBTTagCompound nbt = new NBTTagCompound();
ILibraryTypeHandler handler = findHandler(0, true);
LibraryTypeHandler handler = findHandler(0, LibraryTypeHandler.HandlerType.STORE);
if (handler == null) {
uploadingPlayer = null;
return;
}
if (!handler.store(getStackInSlot(0), nbt)) {
byte[] data = null;
if (handler instanceof LibraryTypeHandlerNBT) {
final NBTTagCompound nbt = new NBTTagCompound();
if (((LibraryTypeHandlerNBT) handler).store(getStackInSlot(0), nbt)) {
data = NBTUtils.save(nbt);
}
} else if (handler instanceof LibraryTypeHandlerByteArray) {
data = ((LibraryTypeHandlerByteArray) handler).store(getStackInSlot(0));
}
if (data == null) {
uploadingPlayer = null;
return;
}
@ -261,18 +273,18 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory,
setInventorySlotContents(1, getStackInSlot(0));
setInventorySlotContents(0, null);
final byte[] dataOut = data;
final LibraryId id = new LibraryId();
id.name = handler.getName(getStackInSlot(1));
id.extension = handler.getFileExtension();
id.extension = handler.getOutputExtension();
if (uploadingPlayer != null) {
BuildCraftCore.instance.sendToPlayer(uploadingPlayer, new PacketCommand(this, "downloadBlueprintToClient",
new CommandWriter() {
public void write(ByteBuf data) {
byte[] bytes = NBTUtils.save(nbt);
id.generateUniqueId(bytes);
id.generateUniqueId(dataOut);
id.writeData(data);
NetworkUtils.writeByteArray(data, bytes);
NetworkUtils.writeByteArray(data, dataOut);
}
}));
uploadingPlayer = null;
@ -336,7 +348,7 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory,
byte[] data = NetworkUtils.readByteArray(stream);
try {
ILibraryTypeHandler handler = LibraryAPI.getHandler(id.extension);
LibraryTypeHandler handler = LibraryAPI.getHandlerFor(id.extension);
if (handler == null) {
return;
}
@ -368,11 +380,23 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory,
}
} else if ("uploadServerEnd".equals(command)) {
try {
NBTTagCompound nbt = CompressedStreamTools.func_152457_a(blueprintDownload, NBTSizeTracker.field_152451_a);
ItemStack output = LibraryAPI.getHandler(blueprintDownloadId.extension).load(getStackInSlot(2), nbt);
LibraryTypeHandler handler = LibraryAPI.getHandlerFor(blueprintDownloadId.extension);
setInventorySlotContents(3, output);
setInventorySlotContents(2, null);
if (handler != null) {
ItemStack output = null;
if (handler instanceof LibraryTypeHandlerNBT) {
NBTTagCompound nbt = CompressedStreamTools.func_152457_a(blueprintDownload, NBTSizeTracker.field_152451_a);
output = ((LibraryTypeHandlerNBT) handler).load(getStackInSlot(2), nbt);
} else if (handler instanceof LibraryTypeHandlerByteArray) {
output = ((LibraryTypeHandlerByteArray) handler).load(getStackInSlot(2), blueprintDownload);
}
if (output != null) {
setInventorySlotContents(3, output);
setInventorySlotContents(2, null);
}
}
} catch (IOException e) {
e.printStackTrace();
}
@ -396,11 +420,11 @@ public class TileBlueprintLibrary extends TileBuildCraft implements IInventory,
}));
}
private boolean isOutputConsistent () {
private boolean isOutputConsistent() {
if (selected == -1 || getStackInSlot(2) == null) {
return false;
}
return LibraryAPI.getHandler(currentPage.get(selected).extension).isHandler(getStackInSlot(2), false);
return LibraryAPI.getHandlerFor(currentPage.get(selected).extension).isHandler(getStackInSlot(2), LibraryTypeHandler.HandlerType.LOAD);
}
}

View file

@ -83,7 +83,7 @@ public class GuiBlueprintLibrary extends GuiBuildCraft {
//}
}
fontRendererObj.drawString(name, 9, 25 + 9 * c, LibraryAPI.getHandler(bpt.extension).getTextColor());
fontRendererObj.drawString(name, 9, 25 + 9 * c, LibraryAPI.getHandlerFor(bpt.extension).getTextColor());
c++;
}