Skip to content

Commit

Permalink
Add some capability logic for later
Browse files Browse the repository at this point in the history
Has a few sideeffects I was innitially unaware of, so I want to consider further how I should handle it.
  • Loading branch information
KnightMiner committed Sep 16, 2020
1 parent 7c2ed64 commit 6a64dfe
Show file tree
Hide file tree
Showing 6 changed files with 435 additions and 177 deletions.
Expand Up @@ -22,6 +22,7 @@ public class TileCauldronInventory extends CauldronItemInventory {
@Nullable
private ICauldronContents newContents = null;
private int newLevel = -1;
private boolean silent = false;
public TileCauldronInventory(CauldronTileEntity tile) {
this.tile = tile;
}
Expand All @@ -33,14 +34,23 @@ public boolean isSimple() {

@Override
public void playSound(SoundEvent sound) {
World world = tile.getWorld();
if (world != null) {
world.playSound(null, tile.getPos(), sound, SoundCategory.BLOCKS, 1.0f, 1.0f);
if (!silent) {
World world = tile.getWorld();
if (world != null) {
world.playSound(null, tile.getPos(), sound, SoundCategory.BLOCKS, 1.0f, 1.0f);
}
}
}

/* Item handling */

/** Clears any cached data. Basically common logic between set context and clear context */
private void clearCache() {
this.newLevel = -1;
this.newContents = null;
this.silent = false;
}

/**
* Sets the context for item stack handling
* @param stack Stack to match for recipes
Expand All @@ -51,8 +61,17 @@ public void setItemContext(ItemStack stack, @Nullable Consumer<ItemStack> itemSe
this.stack = stack;
this.itemSetter = itemSetter == null ? EMPTY_CONSUMER : itemSetter;
this.itemAdder = itemAdder;
this.newLevel = -1;
this.newContents = null;
this.clearCache();
}

/**
* Sets the context for {@link knightminer.inspirations.recipes.tileentity.capability.CauldronItemHandler}. Separate since most uses don't need silent
* @param itemAdder Item adder logic
* @param silent If true, no sounds will be playedd
*/
public void setItemHandlerContext(Consumer<ItemStack> itemAdder, boolean silent) {
setItemContext(ItemStack.EMPTY, null, itemAdder);
this.silent = silent;
}

/**
Expand All @@ -62,8 +81,7 @@ public void clearContext() {
this.stack = ItemStack.EMPTY;
this.itemSetter = EMPTY_CONSUMER;
this.itemAdder = EMPTY_CONSUMER;
this.newLevel = -1;
this.newContents = null;
this.clearCache();
}


Expand Down
156 changes: 0 additions & 156 deletions src/main/java/knightminer/inspirations/recipes/tank/CauldronTank.java

This file was deleted.

Expand Up @@ -70,6 +70,14 @@ public class CauldronTileEntity extends TileEntity implements ITickableTileEntit
.withInitial(OFFSET, 0).build();
private final TileCauldronInventory craftingInventory = new TileCauldronInventory(this);

// capabilities
/* TODO: have to determine what sort of automation I want, waiting on dispenser stuff
private final CauldronItemHandler itemHandler = new CauldronItemHandler(this, craftingInventory);
private final LazyOptional<IItemHandler> itemHandlerCap = LazyOptional.of(() -> itemHandler);
private final CauldronFluidHandler fluidHandler = new CauldronFluidHandler(this);
private final LazyOptional<IFluidHandler> fluidHandlerCap = LazyOptional.of(() -> fluidHandler);
*/

// cauldron properties
/** Current cauldron contents */
private ICauldronContents contents;
Expand Down Expand Up @@ -163,6 +171,15 @@ public boolean canMimicVanilla() {
return levelOffset >= 0 && contents.isSimple();
}

/**
* Called when the state, contents, level offset, or temperature changes to handle recipe updates
*/
private void contentsChanged() {
this.updateTransform = true;
//this.itemHandler.clearCache();
//this.tank.clearCache();
}

/**
* Updates the cauldron state, including the block state level
* @param contents New contents, null for no change
Expand Down Expand Up @@ -226,7 +243,7 @@ protected void updateState(@Nullable ICauldronContents contents, int levelOffset
if (levelOffset != this.levelOffset || contents != null) {
this.levelOffset = levelOffset;
InspirationsNetwork.sendToClients(world, pos, new CauldronStateUpdatePacket(pos, contents, levelOffset));
this.updateTransform = true;
this.contentsChanged();
}
}

Expand Down Expand Up @@ -373,11 +390,11 @@ public void neighborChanged(BlockPos neighbor) {
if (direction == Direction.DOWN) {
isBoiling = null;
temperature = null;
updateTransform = true;
this.contentsChanged();
} else if (direction.getAxis() != Axis.Y) {
isFreezing = null;
temperature = null;
updateTransform = true;
this.contentsChanged();
}
// on the client, immediately update temperature
if (world != null && world.isRemote) {
Expand All @@ -391,6 +408,25 @@ public void neighborChanged(BlockPos neighbor) {

/* behavior */

@Nullable
public ICauldronRecipe findRecipe() {
if (world == null) {
return null;
}
// try last recipe first
if (lastRecipe != null && lastRecipe.matches(craftingInventory, world)) {
return lastRecipe;
}
// fall back to finding a new recipe
ICauldronRecipe recipe = world.getRecipeManager().getRecipe(RecipeTypes.CAULDRON, craftingInventory, world).orElse(null);
if (recipe != null) {
lastRecipe = recipe;
return recipe;
}
// no recipe found
return null;
}

/**
* Handles a cauldron recipe. Will do everything except update the cauldron level and clear the context.
* The caller is responsible for handling those (as each caller has different needs)
Expand All @@ -408,17 +444,9 @@ private boolean handleRecipe(ItemStack stack, @Nullable Consumer<ItemStack> item
craftingInventory.setItemContext(stack, itemSetter, itemAdder);

// grab recipe
ICauldronRecipe recipe;
if (lastRecipe != null && lastRecipe.matches(craftingInventory, world)) {
recipe = lastRecipe;
} else {
recipe = world.getRecipeManager().getRecipe(RecipeTypes.CAULDRON, craftingInventory, world).orElse(null);
}

// if we found a match
ICauldronRecipe recipe = findRecipe();
boolean success = false;
if (recipe != null) {
lastRecipe = recipe;
success = true;
if (!world.isRemote) {
recipe.handleRecipe(craftingInventory);
Expand Down Expand Up @@ -592,7 +620,7 @@ else if (fluid.getAttributes().getTemperature() > 450 && !entity.isImmuneToFire(
@Override
public void updateContainingBlockInfo() {
super.updateContainingBlockInfo();
this.updateTransform = true;
this.contentsChanged();
}

/**
Expand Down Expand Up @@ -726,6 +754,28 @@ public void onBreak(BlockPos pos, int level) {
}
*/

/* Automation */

/* TODO: see above
@Override
public <T> LazyOptional<T> getCapability(Capability<T> cap, @Nullable Direction side) {
if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
return itemHandlerCap.cast();
}
if (cap == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) {
return fluidHandlerCap.cast();
}
return super.getCapability(cap, side);
}
@Override
protected void invalidateCaps() {
super.invalidateCaps();
itemHandlerCap.invalidate();
fluidHandlerCap.invalidate();
}
*/

/* NBT */
private static final String TAG_CONTENTS = "contents";
private static final String TAG_LEVEL_OFFSET = "level_offset";
Expand Down

0 comments on commit 6a64dfe

Please sign in to comment.