Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* If a method utilizes this {@link net.minecraftforge.eventbus.api.Event} as
* its parameter, the method will receive every child event of this class.<br>
* <br>
* {@link #Chunk} contains the Chunk this event is affecting.<br>
* {@link #chunk} contains the Chunk this event is affecting.<br>
* <br>
* All children of this event are fired on the
* {@link MinecraftForge#EVENT_BUS}.<br>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Minecraft Forge, Patchwork Project
* Copyright (c) 2016-2020, 2019-2020
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

package net.minecraftforge.event.world;

import java.util.Random;

import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;

/**
* SaplingGrowTreeEvent is fired when a sapling grows into a tree.
*
* <p>This event is fired during sapling growth in
* {@link net.minecraft.block.SaplingBlock#generate(IWorld, BlockPos, BlockState, Random)}.
*
* <p>{@link #pos} contains the coordinates of the growing sapling.
* {@link #rand} contains an instance of Random for use.
*
* <p>This event is not cancellable.
*
* <p>This event has a result.
* This result determines if the sapling is allowed to grow.
*/

public class SaplingGrowTreeEvent extends WorldEvent {
private final BlockPos pos;
private final Random rand;

public SaplingGrowTreeEvent(IWorld world, Random rand, BlockPos pos) {
super(world);
this.rand = rand;
this.pos = pos;
}

public BlockPos getPos() {
return pos;
}

public Random getRand() {
return rand;
}

@Override
public boolean hasResult() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@

import java.util.Collections;
import java.util.List;
import java.util.Random;

import javax.annotation.Nullable;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.ChunkEvent;
import net.minecraftforge.event.world.ChunkWatchEvent;
import net.minecraftforge.event.world.SaplingGrowTreeEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.eventbus.api.Event;

import net.minecraft.entity.EntityCategory;
import net.minecraft.server.network.ServerPlayerEntity;
Expand Down Expand Up @@ -105,4 +108,10 @@ public void onInitialize() {
// Fire ChunkEvent.Unload on server side
ServerChunkEvents.CHUNK_UNLOAD.register((server, chunk) -> MinecraftForge.EVENT_BUS.post(new ChunkEvent.Unload(chunk)));
}

public static boolean onSaplingGrowTree(IWorld world, Random rand, BlockPos pos) {
SaplingGrowTreeEvent event = new SaplingGrowTreeEvent(world, rand, pos);
MinecraftForge.EVENT_BUS.post(event);
return event.getResult() != Event.Result.DENY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Minecraft Forge, Patchwork Project
* Copyright (c) 2016-2020, 2019-2020
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

package net.patchworkmc.mixin.event.world;

import java.util.Random;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.block.BlockState;
import net.minecraft.block.SaplingBlock;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;

import net.patchworkmc.impl.event.world.WorldEvents;

@Mixin(SaplingBlock.class)
public class MixinSaplingBlock {
@Inject(method = "generate", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/sapling/SaplingGenerator;generate(Lnet/minecraft/world/IWorld;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;Ljava/util/Random;)Z"), cancellable = true)
private void onGenerateTree(IWorld world, BlockPos pos, BlockState state, Random random, CallbackInfo ci) {
if (!WorldEvents.onSaplingGrowTree(world, random, pos)) {
ci.cancel();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"mixins": [
"MixinChunkGenerator",
"MixinMinecraftServer",
"MixinSaplingBlock",
"MixinServerWorld",
"MixinSpawnHelper",
"MixinThreadedAnvilChunkStorage"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.minecraftforge.common.extensions.IForgeBlockState;
import net.minecraftforge.eventbus.api.Event;

import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DefaultedList;
import net.minecraft.block.BlockState;
Expand Down Expand Up @@ -166,4 +167,32 @@ public PlayerEntity getHarvester() {
return harvester;
}
}

/**
* Fired when when farmland gets trampled
* This event is cancellable.
*/
public static class FarmlandTrampleEvent extends BlockEvent {
private final Entity entity;
private final float fallDistance;

public FarmlandTrampleEvent(World world, BlockPos pos, BlockState state, float fallDistance, Entity entity) {
super(world, pos, state);
this.entity = entity;
this.fallDistance = fallDistance;
}

public Entity getEntity() {
return entity;
}

public float getFallDistance() {
return fallDistance;
}

@Override
public boolean isCancelable() {
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.minecraft.block.RedstoneOreBlock;
import net.minecraft.block.SpawnerBlock;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
Expand Down Expand Up @@ -170,4 +171,13 @@ public static float fireBlockHarvesting(DefaultedList<ItemStack> drops, World wo
MinecraftForge.EVENT_BUS.post(event);
return event.getDropChance();
}

public static boolean onFarmlandTrample(World world, BlockPos pos, BlockState state, float fallDistance, Entity entity) {
// TODO: In forge, the possibility of trampling is handled by IForgeEntity.canTrample
// Maybe there's a good way to reconcile that to not break any Fabric mods trying to
// manipulate crop trampling, but for now I just let the vanilla check do it's thing.
BlockEvent.FarmlandTrampleEvent event = new BlockEvent.FarmlandTrampleEvent(world, pos, state, fallDistance, entity);
MinecraftForge.EVENT_BUS.post(event);
return !event.isCanceled();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,26 @@
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.FarmlandBlock;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;

import net.patchworkmc.impl.extensions.block.BlockHarvestManager;

@Mixin(FarmlandBlock.class)
public class MixinFarmlandBlock {
public class MixinFarmlandBlock extends Block {
public MixinFarmlandBlock() {
super(null);
}

@Inject(method = "hasCrop", cancellable = true, at = @At("HEAD"))
private static void onHasCrop(BlockView world, BlockPos pos, CallbackInfoReturnable<Boolean> cir) {
final BlockState ourState = world.getBlockState(pos);
Expand All @@ -44,4 +54,12 @@ private static void onHasCrop(BlockView world, BlockPos pos, CallbackInfoReturna
cir.cancel();
}
}

@Inject(method = "onLandedUpon", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/FarmlandBlock;setToDirt(Lnet/minecraft/block/BlockState;Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;)V"), cancellable = true)
private void onSetToDirt(World world, BlockPos pos, Entity entity, float distance, CallbackInfo ci) {
if (!BlockHarvestManager.onFarmlandTrample(world, pos, world.getBlockState(pos), distance, entity)) {
super.onLandedUpon(world, pos, entity, distance);
ci.cancel();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.eventbus.api.Event;

import net.minecraft.block.BlockState;
import net.minecraft.world.dimension.DimensionType;
import net.minecraft.entity.Entity;
import net.minecraft.entity.ItemEntity;
Expand Down Expand Up @@ -137,4 +138,8 @@ public static LootTable loadLootTable(Gson gson, Identifier name, JsonObject dat
public static String readPoolName(JsonObject json) {
return LootHooks.readPoolName(json);
}

public static boolean onFarmlandTrample(World world, BlockPos pos, BlockState state, float fallDistance, Entity entity) {
return BlockHarvestManager.onFarmlandTrample(world, pos, state, fallDistance, entity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package net.minecraftforge.event;

import java.util.Random;

import javax.annotation.Nullable;

import net.minecraftforge.common.capabilities.CapabilityDispatcher;
Expand All @@ -38,18 +40,19 @@
import net.minecraft.loot.LootTable;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.MobSpawnerLogic;
import net.minecraft.world.World;
import net.minecraft.block.BlockState;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DefaultedList;
import net.minecraft.util.math.BlockPos;

import net.patchworkmc.impl.capability.CapabilityEvents;
import net.patchworkmc.impl.event.entity.EntityEvents;
import net.patchworkmc.impl.event.entity.PlayerEvents;
import net.patchworkmc.impl.event.loot.LootEvents;
import net.patchworkmc.impl.event.world.WorldEvents;
import net.patchworkmc.impl.extensions.block.BlockHarvestManager;

/*
Expand Down Expand Up @@ -87,6 +90,10 @@ public static LootTable loadLootTable(Identifier name, LootTable table, LootMana
return LootEvents.loadLootTable(name, table, lootTableManager);
}

public static boolean saplingGrowTree(IWorld world, Random rand, BlockPos pos) {
return WorldEvents.onSaplingGrowTree(world, rand, pos);
}

// Forge might remove BlockEvent.HarvestDropsEvent, which is replaced by the new loot modifier.
@Deprecated
public static float fireBlockHarvesting(DefaultedList<ItemStack> drops, World world, BlockPos pos, BlockState state, int fortune, float dropChance, boolean silkTouch, PlayerEntity player) {
Expand Down