use of net.minecraftforge.fluids.capability.IFluidHandler in project MinecraftForge by MinecraftForge.
the class ModelLoader method loadItemModels.
@Override
protected void loadItemModels() {
// register model for the universal bucket, if it exists
if (FluidRegistry.isUniversalBucketEnabled()) {
setBucketModelDefinition(ForgeModContainer.getInstance().universalBucket);
}
registerVariantNames();
List<Item> items = Lists.newArrayList(Iterables.filter(Item.REGISTRY, new Predicate<Item>() {
public boolean apply(Item item) {
return item.getRegistryName() != null;
}
}));
Collections.sort(items, new Comparator<Item>() {
public int compare(Item i1, Item i2) {
return i1.getRegistryName().toString().compareTo(i2.getRegistryName().toString());
}
});
ProgressBar itemBar = ProgressManager.push("ModelLoader: items", items.size());
for (Item item : items) {
itemBar.step(item.getRegistryName().toString());
for (String s : getVariantNames(item)) {
ResourceLocation file = getItemLocation(s);
ModelResourceLocation memory = getInventoryVariant(s);
IModel model = ModelLoaderRegistry.getMissingModel();
Exception exception = null;
try {
model = ModelLoaderRegistry.getModel(file);
} catch (Exception normalException) {
// try blockstate json if the item model is missing
FMLLog.fine("Item json isn't found for '" + memory + "', trying to load the variant from the blockstate json");
try {
model = ModelLoaderRegistry.getModel(memory);
} catch (Exception blockstateException) {
exception = new ItemLoadingException("Could not load item model either from the normal location " + file + " or from the blockstate", normalException, blockstateException);
}
}
if (exception != null) {
storeException(memory, exception);
model = ModelLoaderRegistry.getMissingModel(memory, exception);
}
stateModels.put(memory, model);
}
}
ProgressManager.pop(itemBar);
// replace vanilla bucket models if desired. done afterwards for performance reasons
if (ForgeModContainer.replaceVanillaBucketModel) {
// ensure the bucket model is loaded
if (!stateModels.containsKey(ModelDynBucket.LOCATION)) {
// load forges blockstate json for it
try {
registerVariant(getModelBlockDefinition(ModelDynBucket.LOCATION), ModelDynBucket.LOCATION);
} catch (Exception exception) {
FMLLog.getLogger().error("Could not load the forge bucket model from the blockstate", exception);
return;
}
}
// empty bucket
for (String s : getVariantNames(Items.BUCKET)) {
ModelResourceLocation memory = getInventoryVariant(s);
IModel model = ModelLoaderRegistry.getModelOrMissing(new ResourceLocation(ForgeVersion.MOD_ID, "item/bucket"));
// only on successful load, otherwise continue using the old model
if (model != getMissingModel()) {
stateModels.put(memory, model);
}
}
setBucketModel(Items.WATER_BUCKET);
setBucketModel(Items.LAVA_BUCKET);
// milk bucket only replaced if some mod adds milk
if (FluidRegistry.isFluidRegistered("milk")) {
// can the milk be put into a bucket?
Fluid milk = FluidRegistry.getFluid("milk");
FluidStack milkStack = new FluidStack(milk, Fluid.BUCKET_VOLUME);
IFluidHandler bucketHandler = FluidUtil.getFluidHandler(new ItemStack(Items.BUCKET));
if (bucketHandler != null && bucketHandler.fill(milkStack, false) == Fluid.BUCKET_VOLUME) {
setBucketModel(Items.MILK_BUCKET);
}
} else {
// milk bucket if no milk fluid is present
for (String s : getVariantNames(Items.MILK_BUCKET)) {
ModelResourceLocation memory = getInventoryVariant(s);
IModel model = ModelLoaderRegistry.getModelOrMissing(new ResourceLocation(ForgeVersion.MOD_ID, "item/bucket_milk"));
// only on successful load, otherwise continue using the old model
if (model != getMissingModel()) {
stateModels.put(memory, model);
}
}
}
}
}
use of net.minecraftforge.fluids.capability.IFluidHandler in project MinecraftForge by MinecraftForge.
the class FluidUtil method tryPlaceFluid.
/**
* Tries to place a fluid in the world in block form and drains the container.
* Makes a fluid emptying sound when successful.
* Honors the amount of fluid contained by the used container.
* Checks if water-like fluids should vaporize like in the nether.
*
* Modeled after {@link net.minecraft.item.ItemBucket#tryPlaceContainedLiquid(EntityPlayer, World, BlockPos)}
*
* @param player Player who places the fluid. May be null for blocks like dispensers.
* @param world World to place the fluid in
* @param pos The position in the world to place the fluid block
* @param container The fluid container holding the fluidStack to place
* @param resource The fluidStack to place
* @return the container's ItemStack with the remaining amount of fluid if the placement was successful, null otherwise
*/
@Nonnull
public static FluidActionResult tryPlaceFluid(@Nullable EntityPlayer player, World world, BlockPos pos, @Nonnull ItemStack container, FluidStack resource) {
if (world == null || resource == null || pos == null) {
return FluidActionResult.FAILURE;
}
Fluid fluid = resource.getFluid();
if (fluid == null || !fluid.canBePlacedInWorld()) {
return FluidActionResult.FAILURE;
}
// check that we can place the fluid at the destination
IBlockState destBlockState = world.getBlockState(pos);
Material destMaterial = destBlockState.getMaterial();
boolean isDestNonSolid = !destMaterial.isSolid();
boolean isDestReplaceable = destBlockState.getBlock().isReplaceable(world, pos);
if (!world.isAirBlock(pos) && !isDestNonSolid && !isDestReplaceable) {
// Non-air, solid, unreplacable block. We can't put fluid here.
return FluidActionResult.FAILURE;
}
if (world.provider.doesWaterVaporize() && fluid.doesVaporize(resource)) {
fluid.vaporize(player, world, pos, resource);
return tryEmptyContainer(container, new VoidFluidHandler(), Integer.MAX_VALUE, player, true);
} else {
if (!world.isRemote && (isDestNonSolid || isDestReplaceable) && !destMaterial.isLiquid()) {
world.destroyBlock(pos, true);
}
// Defer the placement to the fluid block
// Instead of actually "filling", the fluid handler method replaces the block
Block block = fluid.getBlock();
IFluidHandler handler;
if (block instanceof IFluidBlock) {
handler = new FluidBlockWrapper((IFluidBlock) block, world, pos);
} else if (block instanceof BlockLiquid) {
handler = new BlockLiquidWrapper((BlockLiquid) block, world, pos);
} else {
handler = new BlockWrapper(block, world, pos);
}
FluidActionResult result = tryEmptyContainer(container, handler, Integer.MAX_VALUE, player, true);
if (result.isSuccess()) {
SoundEvent soundevent = fluid.getEmptySound(resource);
world.playSound(player, pos, soundevent, SoundCategory.BLOCKS, 1.0F, 1.0F);
}
return result;
}
}
use of net.minecraftforge.fluids.capability.IFluidHandler in project MinecraftForge by MinecraftForge.
the class FluidHandlerConcatenate method drain.
@Override
public FluidStack drain(int maxDrain, boolean doDrain) {
if (maxDrain == 0)
return null;
FluidStack totalDrained = null;
for (IFluidHandler handler : subHandlers) {
if (totalDrained == null) {
totalDrained = handler.drain(maxDrain, doDrain);
if (totalDrained != null) {
maxDrain -= totalDrained.amount;
}
} else {
FluidStack copy = totalDrained.copy();
copy.amount = maxDrain;
FluidStack drain = handler.drain(copy, doDrain);
if (drain != null) {
totalDrained.amount += drain.amount;
maxDrain -= drain.amount;
}
}
if (maxDrain <= 0)
break;
}
return totalDrained;
}
use of net.minecraftforge.fluids.capability.IFluidHandler in project ImmersiveEngineering by BluSunrize.
the class TileEntitySheetmetalTank method update.
@Override
public void update() {
if (pos == 4 && !worldObj.isRemote && worldObj.isBlockIndirectlyGettingPowered(getPos()) > 0)
for (int i = 0; i < 6; i++) if (i != 1 && tank.getFluidAmount() > 0) {
EnumFacing f = EnumFacing.getFront(i);
int outSize = Math.min(144, tank.getFluidAmount());
FluidStack out = new FluidStack(tank.getFluid().getFluid(), outSize);
BlockPos outputPos = getPos().offset(f);
IFluidHandler output = FluidUtil.getFluidHandler(worldObj, outputPos, f.getOpposite());
if (output != null) {
int accepted = output.fill(out, false);
if (accepted > 0) {
int drained = output.fill(Utils.copyFluidStackWithAmount(out, Math.min(out.amount, accepted), false), true);
this.tank.drain(drained, true);
this.markContainingBlockForUpdate(null);
updateComparatorValuesPart2();
}
}
}
}
use of net.minecraftforge.fluids.capability.IFluidHandler in project ImmersiveEngineering by BluSunrize.
the class ItemJerrycan method getContainerItem.
@Override
public ItemStack getContainerItem(ItemStack stack) {
if (ItemNBTHelper.hasKey(stack, "jerrycanDrain")) {
ItemStack ret = stack.copy();
IFluidHandler handler = FluidUtil.getFluidHandler(ret);
handler.drain(ItemNBTHelper.getInt(ret, "jerrycanDrain"), true);
ItemNBTHelper.remove(ret, "jerrycanDrain");
return ret;
} else if (FluidUtil.getFluidContained(stack) != null) {
ItemStack ret = stack.copy();
IFluidHandler handler = FluidUtil.getFluidHandler(ret);
handler.drain(1000, true);
return ret;
}
return stack;
}
Aggregations