-
Notifications
You must be signed in to change notification settings - Fork 18
/
TweaksEvents.java
353 lines (316 loc) · 10.7 KB
/
TweaksEvents.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package knightminer.inspirations.tweaks;
import knightminer.inspirations.common.Config;
import knightminer.inspirations.common.network.InspirationsNetwork;
import knightminer.inspirations.common.network.MilkablePacket;
import knightminer.inspirations.library.InspirationsRegistry;
import knightminer.inspirations.shared.SharedEvents;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.BushBlock;
import net.minecraft.block.CropsBlock;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.passive.CowEntity;
import net.minecraft.entity.passive.PigEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tags.BlockTags;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.living.LivingFallEvent;
import net.minecraftforge.event.entity.player.BonemealEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.EntityInteract;
import net.minecraftforge.event.world.BlockEvent.HarvestDropsEvent;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.items.ItemHandlerHelper;
import java.util.List;
public class TweaksEvents {
@SubscribeEvent
public static void unsaddlePig(EntityInteract event) {
if(!Config.enablePigDesaddle.get()) {
return;
}
PlayerEntity player = event.getPlayer();
ItemStack stack = player.getHeldItem(event.getHand());
// must be sneaking and holding nothing
if(player.isSneaking() && stack.isEmpty()) {
Entity target = event.getTarget();
if(target instanceof PigEntity) {
PigEntity pig = (PigEntity) target;
if(pig.getSaddled()) {
pig.setSaddled(false);
pig.world.playSound(player, pig.posX, pig.posY, pig.posZ, SoundEvents.ENTITY_PIG_SADDLE, SoundCategory.NEUTRAL, 0.5F, 1.0F);
ItemHandlerHelper.giveItemToPlayer(player, new ItemStack(Items.SADDLE), player.inventory.currentItem);
event.setCanceled(true);
}
}
}
}
@SubscribeEvent
public static void extraBonemeal(BonemealEvent event) {
if(!Config.bonemealMushrooms.get() && !Config.bonemealDeadBush.get() && !Config.bonemealGrassSpread.get() && !Config.bonemealMyceliumSpread.get()) {
return;
}
// running client side acts weird
World world = event.getWorld();
if(world.isRemote) {
return;
}
BlockPos pos = event.getPos();
BlockState state = world.getBlockState(pos);
Block block = state.getBlock();
// block must be mycelium for mushrooms or sand for dead bushes
if((Config.bonemealMushrooms.get() && block == Blocks.MYCELIUM) || (Config.bonemealDeadBush.get() && block.isIn(BlockTags.SAND))) {
bonemealPlants(block, world, pos);
event.setResult(Event.Result.ALLOW);
}
// block must be dirt for grass/mycelium spread
else if((Config.bonemealGrassSpread.get() || Config.bonemealMyceliumSpread.get()) && block == Blocks.DIRT) {
if (bonemealDirt(world, pos)) {
event.setResult(Event.Result.ALLOW);
}
}
}
/** Called when using bonemeal on mycelium or sand to produce a plant */
private static void bonemealPlants(Block base, World world, BlockPos pos) {
// this is mostly copied from grass block code, so its a bit weird
BlockPos up = pos.up();
BushBlock bush = (BushBlock) Blocks.DEAD_BUSH;
BlockState state = bush.getDefaultState();
// 128 chances, this affects how far blocks are spread
boolean isMycelium = base == Blocks.MYCELIUM;
for (int i = 0; i < 128; ++i) {
BlockPos next = up;
int j = 0;
while (true) {
// the longer we go, the closer to old blocks we place the block
if (j >= i / 16) {
if (world.isAirBlock(next)) {
if (world.rand.nextInt(128) == 0) {
// mycelium randomly picks between red and brown
if(isMycelium) {
bush = (BushBlock) (world.rand.nextInt(2) == 0 ? Blocks.RED_MUSHROOM : Blocks.BROWN_MUSHROOM);
state = bush.getDefaultState();
}
// if it can be planted here, plant it
if(bush.isValidPosition(state, world, next)) {
world.setBlockState(next, state);
}
}
}
break;
}
// randomly offset the position
next = next.add(world.rand.nextInt(3) - 1, (world.rand.nextInt(3) - 1) * world.rand.nextInt(3) / 2, world.rand.nextInt(3) - 1);
// if the new position is invalid, this cycle is done
if (world.getBlockState(next.down()).getBlock() != base || world.getBlockState(next).isNormalCube(world, next)) {
break;
}
++j;
}
}
}
/** Called when using bonemeal on a dirt block to spread grass */
private static boolean bonemealDirt(World world, BlockPos pos) {
if(world.getLight(pos.up()) < 9) {
return false;
}
// first, get a count of grass and mycelium on all sides
int grass = 0;
int mycelium = 0;
for (Direction side : Direction.Plane.HORIZONTAL) {
BlockPos offset = pos.offset(side);
BlockState state = world.getBlockState(offset);
Block block = state.getBlock();
// hill logic: go up for dirt, down for air
if (block.isAir(state, world, pos)) {
state = world.getBlockState(offset.down());
block = state.getBlock();
}
else if (block != Blocks.GRASS_BLOCK && block != Blocks.MYCELIUM) {
state = world.getBlockState(offset.up());
block = state.getBlock();
}
// increment if the state is grass/mycelium
if (Config.bonemealGrassSpread.get() && block == Blocks.GRASS_BLOCK) {
grass++;
}
else if (Config.bonemealMyceliumSpread.get() && block == Blocks.MYCELIUM) {
mycelium++;
}
}
// no results? exit
if (grass == 0 && mycelium == 0) {
return false;
}
// chance gets higher the more blocks of the type surround
if (world.rand.nextInt(5) > (Math.max(grass, mycelium) - 1)) {
return true;
}
// place block based on which has more
// if there is a tie, randomly choose
if (grass == mycelium) {
if(world.rand.nextBoolean()) {
mycelium++;
}
}
world.setBlockState(pos, grass >= mycelium ? Blocks.GRASS_BLOCK.getDefaultState() : Blocks.MYCELIUM.getDefaultState());
return true;
}
@SubscribeEvent
public static void dropHeartbeet(HarvestDropsEvent event) {
if(!Config.enableHeartbeet.get()) {
return;
}
// insure its fully grown beetroots
BlockState state = event.getState();
Block block = state.getBlock();
if(block != Blocks.BEETROOTS || !(block instanceof CropsBlock) || !((CropsBlock)block).isMaxAge(state)) {
return;
}
// we get a base of two chances, and each fortune level adds one more
int rolls = event.getFortuneLevel() + 2;
// up to fortune 4 we will keep, any higher just ignore
if(rolls > 6) {
rolls = 6;
}
List<ItemStack> drops = event.getDrops();
// find the first beetroot from the drops
iterator:
for(ItemStack stack : drops) {
// as soon as we find one, chance to replace it
if(stack.getItem() == Items.BEETROOT) {
// for each roll, try to get the drop once
for(int i = 0; i < rolls; i++) {
if(event.getWorld().getRandom().nextInt(Config.heartbeetChance.get()) == 0) {
stack.shrink(1);
if(stack.isEmpty()) {
drops.remove(stack);
}
drops.add(new ItemStack(InspirationsTweaks.heartbeet));
// cap at one heartroot in case we get extras, plus prevents concurrent modification
break iterator;
}
}
}
}
}
/* TODO: reconsider feature
@SubscribeEvent
public static void dropCarrotsPotatos(HarvestDropsEvent event) {
if(!Config.nerfCarrotPotatoDrops()) {
return;
}
// validate block and ensure its not max age
BlockState state = event.getState();
Block block = state.getBlock();
if((block != Blocks.CARROTS && block != Blocks.POTATOES) || !(block instanceof CropsBlock) || ((CropsBlock)block).isMaxAge(state)) {
return;
}
// replace the seed with our seed
event.getDrops().clear();
event.getDrops().add(new ItemStack(block == Blocks.CARROTS ? InspirationsTweaks.carrotSeeds : InspirationsTweaks.potatoSeeds));
}
*/
@SubscribeEvent(priority = EventPriority.LOW)
public static void onFall(LivingFallEvent event) {
if(!Config.lilypadBreakFall.get()) {
return;
}
// no fall damage
if(event.getDistance() < 4) {
return;
}
// ensure client world
LivingEntity entity = event.getEntityLiving();
World world = entity.getEntityWorld();
if(world.isRemote) {
return;
}
// actually hit the lily pad
Vec3d vec = entity.getPositionVector();
if(vec.y % 1 > 0.09375) {
return;
}
// build a list of lily pads we hit
BlockPos blockPos = entity.getPosition();
BlockPos[] posList = new BlockPos[4];
int i = 0;
posList[i++] = blockPos;
double x = vec.x % 1;
if(x < 0) {
x += 1;
}
double z = vec.z % 1;
if(z < 0) {
z += 1;
}
// about 0.3 out of the block is into another block
if(x > 0.7) {
posList[i++] = blockPos.east();
} else if(x < 0.3) {
posList[i++] = blockPos.west();
}
if(z > 0.7) {
posList[i++] = blockPos.south();
// make sure to get the corners
if(i == 3) {
posList[i++] = posList[1].south();
}
} else if(z < 0.3) {
posList[i++] = blockPos.north();
if(i == 3) {
posList[i++] = posList[1].north();
}
}
// loop through the position list and find any lily pads
boolean safe = false;
for(BlockPos pos : posList) {
if(pos != null && world.getBlockState(pos).getBlock() == Blocks.LILY_PAD) {
world.destroyBlock(pos, true);
safe = true;
}
}
// if we got one, this fall is safe
if(safe) {
event.setDistance(0);
}
}
@SubscribeEvent
public static void milkCow(EntityInteract event) {
if(!Config.milkCooldown.get()) {
return;
}
// only care about cows
Entity target = event.getTarget();
if(!(target instanceof CowEntity) || ((CowEntity)target).isChild()) {
return;
}
// must be holding a milk container
ItemStack stack = event.getPlayer().getHeldItem(event.getHand());
if(stack.getItem().isIn(InspirationsRegistry.TAG_MILK_CONTAINERS)) {
// if has tag, cannot be milked
CompoundNBT tags = target.getPersistentData();
if (tags.getShort(SharedEvents.TAG_MILKCOOLDOWN) > 0) {
event.setCancellationResult(ActionResultType.PASS);
event.setCanceled(true);
} else {
// no tag means we add it as part of milking
tags.putShort(SharedEvents.TAG_MILKCOOLDOWN, Config.milkCooldownTime.get().shortValue());
if (!event.getWorld().isRemote) {
InspirationsNetwork.sendToClients(event.getWorld(), target.getPosition(), new MilkablePacket(target, false));
}
}
}
}
}