/
InspirationsTweaks.java
233 lines (201 loc) · 9.96 KB
/
InspirationsTweaks.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package knightminer.inspirations.tweaks;
import com.google.common.collect.ImmutableSet;
import knightminer.inspirations.common.Config;
import knightminer.inspirations.common.ModuleBase;
import knightminer.inspirations.common.item.HidableItem;
import knightminer.inspirations.shared.InspirationsShared;
import knightminer.inspirations.tweaks.block.BlockCropBlock;
import knightminer.inspirations.tweaks.block.CactusCropBlock;
import knightminer.inspirations.tweaks.block.DryHopperBlock;
import knightminer.inspirations.tweaks.block.FittedCarpetBlock;
import knightminer.inspirations.tweaks.block.FlatCarpetBlock;
import knightminer.inspirations.tweaks.block.SugarCaneCropBlock;
import knightminer.inspirations.tweaks.block.WetHopperBlock;
import knightminer.inspirations.tweaks.datagen.TweaksRecipeProvider;
import knightminer.inspirations.tweaks.item.SeedItem;
import knightminer.inspirations.tweaks.recipe.NormalBrewingRecipe;
import knightminer.inspirations.tweaks.util.SmoothGrowthListener;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.block.ComposterBlock;
import net.minecraft.block.DispenserBlock;
import net.minecraft.block.HopperBlock;
import net.minecraft.data.DataGenerator;
import net.minecraft.dispenser.DefaultDispenseItemBehavior;
import net.minecraft.dispenser.IDispenseItemBehavior;
import net.minecraft.item.BlockItem;
import net.minecraft.item.DirectionalPlaceContext;
import net.minecraft.item.DyeColor;
import net.minecraft.item.Food;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.Items;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.potion.Potions;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.PlantType;
import net.minecraftforge.common.brewing.BrewingRecipeRegistry;
import net.minecraftforge.event.LootTableLoadEvent;
import net.minecraftforge.event.RegistryEvent.Register;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.GatherDataEvent;
import net.minecraftforge.registries.IForgeRegistry;
import slimeknights.mantle.registration.adapter.BlockRegistryAdapter;
import slimeknights.mantle.registration.adapter.ItemRegistryAdapter;
import slimeknights.mantle.registration.object.EnumObject;
import java.util.Objects;
@SuppressWarnings({"WeakerAccess", "unused"})
public class InspirationsTweaks extends ModuleBase {
public static final String pulseID = "InspirationsTweaks";
// blocks
public static BlockCropBlock cactus;
public static BlockCropBlock sugarCane;
public static HopperBlock wetHopper;
public static HopperBlock dryHopper;
// enum
public static EnumObject<DyeColor,FittedCarpetBlock> fitCarpets = EnumObject.empty();
public static EnumObject<DyeColor,FlatCarpetBlock> flatCarpets = EnumObject.empty();
// items
//public static Item potatoSeeds;
//public static Item carrotSeeds;
public static Item sugarCaneSeeds;
public static Item cactusSeeds;
//public static Item silverfishPowder;
public static Item heartbeet;
@SubscribeEvent
void registerBlocks(Register<Block> event) {
BlockRegistryAdapter registry = new BlockRegistryAdapter(event.getRegistry());
IForgeRegistry<Block> r = event.getRegistry();
if (Config.enableFittedCarpets.get()) {
EnumObject.Builder<DyeColor,FlatCarpetBlock> flatBuilder = new EnumObject.Builder<>(DyeColor.class);
EnumObject.Builder<DyeColor,FittedCarpetBlock> fittedBuilder = new EnumObject.Builder<>(DyeColor.class);
for (DyeColor color : DyeColor.values()) {
Block original = InspirationsShared.VANILLA_CARPETS.get(color);
Block.Properties props = Block.Properties.from(original);
flatBuilder.putDelegate(color, registry.register(new FlatCarpetBlock(color, props), original).delegate);
fittedBuilder.putDelegate(color, registry.register(new FittedCarpetBlock(color, props), color.getString() + "_fitted_carpet").delegate);
}
flatCarpets = flatBuilder.build();
fitCarpets = fittedBuilder.build();
}
if (Config.waterlogHopper.get()) {
dryHopper = registry.registerOverride(DryHopperBlock::new, Blocks.HOPPER);
wetHopper = registry.register(new WetHopperBlock(Block.Properties.from(Blocks.HOPPER)), "wet_hopper");
}
cactus = registry.register(new CactusCropBlock(Blocks.CACTUS, PlantType.DESERT), "cactus");
sugarCane = registry.register(new SugarCaneCropBlock(Blocks.SUGAR_CANE, PlantType.BEACH), "sugar_cane");
}
@SubscribeEvent
void registerItem(Register<Item> event) {
ItemRegistryAdapter registry = new ItemRegistryAdapter(event.getRegistry());
Item.Properties decorationProps = new Item.Properties().group(ItemGroup.DECORATIONS);
IForgeRegistry<Item> r = event.getRegistry();
if (Config.enableFittedCarpets.get()) {
for (DyeColor color : DyeColor.values()) {
Block carpet = InspirationsShared.VANILLA_CARPETS.get(color);
BlockItem item = registry.registerBlockItem(carpet, decorationProps);
Item.BLOCK_TO_ITEM.put(carpet, item);
Item.BLOCK_TO_ITEM.put(Objects.requireNonNull(flatCarpets.get(color)), item);
}
}
if (Config.waterlogHopper.get()) {
registry.register(new BlockItem(dryHopper, new Item.Properties().group(ItemGroup.REDSTONE)), Items.HOPPER);
}
Item.Properties props = new Item.Properties().group(ItemGroup.FOOD);
cactusSeeds = registry.register(new SeedItem(cactus, props), "cactus_seeds");
sugarCaneSeeds = registry.register(new SeedItem(sugarCane, props), "sugar_cane_seeds");
/*
carrotSeeds = registerItem(r, new SeedItem((CropsBlock) Blocks.CARROTS, PlantType.Crop), "carrot_seeds");
potatoSeeds = registerItem(r, new SeedItem((CropsBlock) Blocks.POTATOES, PlantType.Crop), "potato_seeds");
*/
heartbeet = registry.register(new HidableItem(new Item.Properties().group(ItemGroup.FOOD)
.food(new Food.Builder().hunger(2).saturation(2.4f).effect(() -> new EffectInstance(Effects.REGENERATION, 100), 1).build()
), Config.enableHeartbeet), "heartbeet");
// silverfishPowder = registerItem(r, new HidableItem(
// new Item.Properties().group(ItemGroup.BREWING),
// () -> false // TODO: Make this have a purpose...
// ), "silverfish_powder");
}
@SubscribeEvent
public void registerTileEntities(Register<TileEntityType<?>> event) {
if (Config.waterlogHopper.get()) {
// We need to inject our replacement hopper blocks into the valid ones for the TE type.
// It's an immutable set, so we need to replace it entirely.
synchronized (TileEntityType.HOPPER) {
TileEntityType.HOPPER.validBlocks = new ImmutableSet.Builder<Block>()
.addAll(TileEntityType.HOPPER.validBlocks)
.add(dryHopper)
.add(wetHopper)
.build();
}
}
}
@SubscribeEvent
public void setup(FMLCommonSetupEvent event) {
// TODO: Forge needs to implement the ability to inject these into the loot tables.
// See MinecraftForge#
//if(Config.addGrassDrops()) {
// MinecraftForge.addGrassSeed(new ItemStack(InspirationsTweaks.carrotSeeds), 4);
// MinecraftForge.addGrassSeed(new ItemStack(InspirationsTweaks.potatoSeeds), 3);
//}
// brew heartbeets into regen potions
Ingredient heartbeet = Ingredient.fromItems(InspirationsTweaks.heartbeet);
BrewingRecipeRegistry.addRecipe(
new NormalBrewingRecipe(Potions.WATER, heartbeet, Potions.MUNDANE, Config.brewHeartbeet)
);
BrewingRecipeRegistry.addRecipe(
new NormalBrewingRecipe(Potions.AWKWARD, heartbeet, Potions.REGENERATION, Config.brewHeartbeet
));
registerCompostables();
registerDispenserBehavior();
MinecraftForge.EVENT_BUS.addListener(new SmoothGrowthListener(Blocks.CACTUS, cactus, false));
MinecraftForge.EVENT_BUS.addListener(new SmoothGrowthListener(Blocks.SUGAR_CANE, sugarCane, true));
}
@SubscribeEvent
public void gatherData(GatherDataEvent event) {
DataGenerator gen = event.getGenerator();
if (event.includeServer()) {
gen.addProvider(new TweaksRecipeProvider(gen));
}
}
@SubscribeEvent
public static void loadLoot(LootTableLoadEvent event) {
addToVanillaLoot(event, "entities/cave_spider");
addToVanillaLoot(event, "entities/skeleton");
}
private static final IDispenseItemBehavior DEFAULT = new DefaultDispenseItemBehavior();
private void registerCompostables() {
ComposterBlock.registerCompostable(0.3F, cactusSeeds);
ComposterBlock.registerCompostable(0.3F, sugarCaneSeeds);
ComposterBlock.registerCompostable(0.8F, heartbeet);
}
private void registerDispenserBehavior() {
IDispenseItemBehavior behavior = (source, stack) -> {
if (!Config.dispensersPlaceAnvils.get()) {
DEFAULT.dispense(source, stack);
}
// get basic data
Direction facing = source.getBlockState().get(DispenserBlock.FACING);
World world = source.getWorld();
BlockPos pos = source.getBlockPos().offset(facing);
DirectionalPlaceContext context = new DirectionalPlaceContext(world, pos, facing, stack, facing.getOpposite());
if (((BlockItem)stack.getItem()).tryPlace(context) == ActionResultType.SUCCESS) {
return stack;
} else {
// if we cannot place it, toss the item
return DEFAULT.dispense(source, stack);
}
};
DispenserBlock.registerDispenseBehavior(Blocks.ANVIL, behavior);
DispenserBlock.registerDispenseBehavior(Blocks.CHIPPED_ANVIL, behavior);
DispenserBlock.registerDispenseBehavior(Blocks.DAMAGED_ANVIL, behavior);
}
}