/
DispenseCauldronRecipe.java
147 lines (128 loc) · 5.49 KB
/
DispenseCauldronRecipe.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package knightminer.inspirations.recipes.dispenser;
import knightminer.inspirations.common.Config;
import knightminer.inspirations.library.InspirationsTags;
import knightminer.inspirations.library.recipe.cauldron.legacy.ICauldronRecipe;
import knightminer.inspirations.library.recipe.cauldron.legacy.ICauldronRecipe.CauldronState;
import knightminer.inspirations.recipes.block.EnhancedCauldronBlock;
import knightminer.inspirations.recipes.tileentity.CauldronTileEntity;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.CauldronBlock;
import net.minecraft.block.DispenserBlock;
import net.minecraft.dispenser.DefaultDispenseItemBehavior;
import net.minecraft.dispenser.IBlockSource;
import net.minecraft.dispenser.IDispenseItemBehavior;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.DispenserTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
public class DispenseCauldronRecipe extends DefaultDispenseItemBehavior {
private static final DefaultDispenseItemBehavior DEFAULT = new DefaultDispenseItemBehavior();
private IDispenseItemBehavior fallback;
public DispenseCauldronRecipe(IDispenseItemBehavior fallback) {
this.fallback = fallback;
}
@Override
protected ItemStack dispenseStack(IBlockSource source, ItemStack stack) {
if (!stack.getItem().isIn(InspirationsTags.Items.DISP_CAULDRON_RECIPES)) {
return fallback.dispense(source, stack);
}
Direction side = source.getBlockState().get(DispenserBlock.FACING);
BlockPos pos = source.getBlockPos().offset(side);
World world = source.getWorld();
BlockState state = world.getBlockState(pos);
if (state.getBlock() != Blocks.CAULDRON) {
return fallback.dispense(source, stack);
}
// grab the TE if extended
CauldronTileEntity cauldron = null;
CauldronState cauldronState = CauldronState.WATER;
boolean boiling = false;
Block block = state.getBlock();
if (Config.enableExtendedCauldron() && block instanceof EnhancedCauldronBlock) {
TileEntity te = world.getTileEntity(pos);
if (te instanceof CauldronTileEntity) {
cauldron = (CauldronTileEntity)te;
cauldronState = cauldron.getState();
boiling = state.get(EnhancedCauldronBlock.BOILING);
}
} else {
boiling = CauldronTileEntity.isCauldronFire(world.getBlockState(pos.down()));
}
// other properties
int level = EnhancedCauldronBlock.getCauldronLevel(state);
// grab recipe
ICauldronRecipe recipe = null;//InspirationsRegistry.getCauldronResult(stack, boiling, level, cauldronState);
if (recipe == null) {
return DEFAULT.dispense(source, stack);
}
// grab state first since we may need to back out
CauldronState newState = recipe.getState(stack, boiling, level, cauldronState);
// if its not extended, stop right here and disallow any recipes which do not return water
if (!Config.enableExtendedCauldron() && !CauldronState.WATER.matches(newState)) {
return DEFAULT.dispense(source, stack);
}
// play sound
SoundEvent sound = recipe.getSound(stack, boiling, level, cauldronState);
if (sound != null) {
world.playSound(null, pos, sound, SoundCategory.BLOCKS, recipe.getVolume(sound), 1.0F);
}
// update level
int newLevel = recipe.getLevel(level);
if (newLevel != level || !cauldronState.matches(newState)) {
// overrides for full cauldrons, assuming we started with a "valid cauldron", in this context an iron one
if (!(block instanceof CauldronBlock)) {
((CauldronBlock)Blocks.CAULDRON).setWaterLevel(world, pos, Blocks.CAULDRON.getDefaultState(), newLevel);
// missing the tile entity
if (Config.enableExtendedCauldron()) {
TileEntity te = world.getTileEntity(pos);
if (te instanceof CauldronTileEntity) {
cauldron = (CauldronTileEntity)te;
}
}
} else {
((CauldronBlock)block).setWaterLevel(world, pos, state, newLevel);
}
if (newLevel == 0) {
newState = CauldronState.WATER;
}
}
// update the state
if (cauldron != null) {
cauldron.setState(newState, true);
}
// result
ItemStack result = recipe.getResult(stack, boiling, level, cauldronState);
ItemStack container = recipe.getContainer(stack);
int oldSize = stack.getCount();
ItemStack remainder = recipe.transformInput(stack.copy(), boiling, level, cauldronState);
// if there is no remainder, return will be different
if (remainder.isEmpty()) {
// no container means return is result
if (container.isEmpty()) {
return result;
}
// otherwise update the container and its our return
container.setCount(container.getCount() * oldSize);
dispenseItem(source, result);
return container;
}
// we at least have a remainder, so dispense the item and container
dispenseItem(source, result);
if (!container.isEmpty()) {
container.setCount(container.getCount() * (oldSize - remainder.getCount()));
dispenseItem(source, container);
}
return remainder;
}
private static void dispenseItem(IBlockSource source, ItemStack stack) {
if (((DispenserTileEntity)source.getBlockTileEntity()).addItemStack(stack) < 0) {
DEFAULT.dispense(source, stack);
}
}
}