Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Add EntityItemPickupEvent and EntityTravelToDimensionEvent #179

Merged
merged 4 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
@@ -0,0 +1,56 @@
/*
* 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.entity;

import net.minecraftforge.common.MinecraftForge;

import net.minecraft.entity.Entity;
import net.minecraft.world.dimension.DimensionType;

/**
* <p>EntityTravelToDimensionEvent is fired before an Entity travels to a dimension.</p>
*
* <p>{@link #dimension} contains the id of the dimension the entity is traveling to.</p>
*
* <p>This event is cancelable.
* If this event is canceled, the Entity does not travel to the dimension.</p>
*
* <p>This event does not have a result.</p>
*
* <p>This event is fired on the {@link MinecraftForge#EVENT_BUS}.</p>
*/
public class EntityTravelToDimensionEvent extends EntityEvent {
private final DimensionType dimension;

public EntityTravelToDimensionEvent(Entity entity, DimensionType dimension) {
super(entity);
this.dimension = dimension;
}

public DimensionType getDimension() {
return dimension;
}

@Override
public boolean isCancelable() {
return true;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.entity.player;

import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;

/**
* <p>This event is called when a player collides with a EntityItem on the ground.
* The event can be canceled, and no further processing will be done.</p>
*
* <p>You can set the result of this event to ALLOW which will trigger the
* processing of achievements, FML's event, play the sound, and kill the
* entity if all the items are picked up.</p>
*
* <p>setResult(ALLOW) is the same as the old setHandled()</p>
*/
public class EntityItemPickupEvent extends PlayerEvent {
private final ItemEntity item;

public EntityItemPickupEvent(PlayerEntity player, ItemEntity item) {
super(player);
this.item = item;
}

public ItemEntity getItem() {
return item;
}

@Override
public boolean isCancelable() {
return true;
}

@Override
public boolean hasResult() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.minecraftforge.common.extensions.IForgeItem;
import net.minecraftforge.event.entity.EntityEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.EntityTravelToDimensionEvent;
import net.minecraftforge.event.entity.ProjectileImpactEvent;
import net.minecraftforge.event.entity.living.AnimalTameEvent;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
Expand Down Expand Up @@ -68,6 +69,7 @@
import net.minecraft.world.IWorld;
import net.minecraft.world.MobSpawnerLogic;
import net.minecraft.world.World;
import net.minecraft.world.dimension.DimensionType;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
Expand Down Expand Up @@ -217,6 +219,19 @@ public static boolean onProjectileImpact(ThrownEntity throwable, HitResult ray)
return MinecraftForge.EVENT_BUS.post(new ProjectileImpactEvent.Throwable(throwable, ray));
}

public static boolean onTravelToDimension(Entity entity, DimensionType dimensionType) {
EntityTravelToDimensionEvent event = new EntityTravelToDimensionEvent(entity, dimensionType);
return !MinecraftForge.EVENT_BUS.post(event);
/* Forge has this thing:
williambl marked this conversation as resolved.
Show resolved Hide resolved

if (event.isCanceled()) {
// Revert variable back to true as it would have been set to false
if (entity instanceof StorageMinecartEntity) {
((StorageMinecartEntity) entity).dropContentsWhenDead(true);
}
} */
}

@Override
public void onInitialize() {
UseItemCallback.EVENT.register((player, world, hand) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
package net.patchworkmc.impl.event.entity;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.Event;

import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
Expand Down Expand Up @@ -56,4 +58,14 @@ public static void firePlayerCraftingEvent(PlayerEntity player, ItemStack crafte
public static void firePlayerSmeltedEvent(PlayerEntity player, ItemStack smelted) {
MinecraftForge.EVENT_BUS.post(new PlayerEvent.ItemSmeltedEvent(player, smelted));
}

/**
*
* @return -1 if the event was cancelled, 0 if the event was denied, 1 if the event was accepted
*/
public static int onItemPickup(PlayerEntity player, ItemEntity entityItem) {
Event event = new EntityItemPickupEvent(player, entityItem);
if (MinecraftForge.EVENT_BUS.post(event)) return -1;
return event.getResult() == Event.Result.ALLOW ? 1 : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
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.entity.Entity;
import net.minecraft.entity.EntityDimensions;
import net.minecraft.entity.EntityPose;
import net.minecraft.entity.EntityType;
import net.minecraft.world.World;
import net.minecraft.world.dimension.DimensionType;

import net.patchworkmc.impl.event.entity.EntityEvents;

Expand All @@ -52,4 +54,14 @@ public void hookConstructor(EntityType<?> type, World world, CallbackInfo ci) {

EntityEvents.onEntityConstruct(entity);
}

@Inject(method = "changeDimension",
at = @At("HEAD"),
cancellable = true
)
void patchwork_fireTravelToDimensionEventChangeDimension(DimensionType newDimension, CallbackInfoReturnable<Entity> cir) {
if (!EntityEvents.onTravelToDimension((Entity) (Object) this, newDimension)) {
cir.setReturnValue(null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@

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

import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;

import net.patchworkmc.impl.event.entity.PlayerEvents;

Expand All @@ -44,4 +47,22 @@ private void onPlayerPickUpItemEntity(PlayerEntity player, CallbackInfo ci) {
ItemEntity me = (ItemEntity) (Object) this;
PlayerEvents.firePlayerItemPickupEvent(player, me, me.getStack().copy());
}

int eventResult;

@Inject(method = "onPlayerCollision",
at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getCount()I"),
cancellable = true
)
void patchwork_fireItemPickupEvent(PlayerEntity player, CallbackInfo ci) {
eventResult = PlayerEvents.onItemPickup(player, (ItemEntity) (Object) this);
if (eventResult != 1) ci.cancel();
}

@Redirect(method = "onPlayerCollision",
at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerInventory;insertStack(Lnet/minecraft/item/ItemStack;)Z")
)
boolean patchwork_skipIfEventNotAllowed(PlayerInventory playerInventory, ItemStack stack) {
return eventResult == 1 || playerInventory.insertStack(stack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package net.patchworkmc.mixin.event.entity;

import com.mojang.authlib.GameProfile;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerEvent;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -33,16 +34,22 @@
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.World;
import net.minecraft.world.dimension.DimensionType;
import net.minecraft.world.level.LevelProperties;

import net.patchworkmc.impl.event.entity.EntityEvents;
import net.patchworkmc.impl.event.entity.PlayerEvents;

@Mixin(ServerPlayerEntity.class)
public class MixinServerPlayerEntity {
public abstract class MixinServerPlayerEntity extends PlayerEntity {
public MixinServerPlayerEntity(World world, GameProfile profile) {
super(world, profile);
}

@Inject(method = "onDeath", at = @At("HEAD"), cancellable = true)
private void hookDeath(DamageSource source, CallbackInfo callback) {
LivingEntity entity = (LivingEntity) (Object) this;
Expand All @@ -59,6 +66,19 @@ private void hookCopyFromForCloneEvent(ServerPlayerEntity oldPlayer, boolean ali
MinecraftForge.EVENT_BUS.post(new PlayerEvent.Clone(speThis, oldPlayer, !alive));
}

@Inject(method = "teleport",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/network/ServerPlayerEntity;getServerWorld()Lnet/minecraft/server/world/ServerWorld;"
),
cancellable = true
)
void patchwork_fireTravelToDimensionEventTeleport(ServerWorld targetWorld, double x, double y, double z, float yaw, float pitch, CallbackInfo ci) {
if (!EntityEvents.onTravelToDimension(this, targetWorld.dimension.getType())) {
ci.cancel();
}
}

////////////////////////////////////
// PlayerChangedDimensionEvent
////////////////////////////////////
Expand All @@ -79,6 +99,16 @@ private void teleport_sendInventory(ServerWorld targetWorld, double x, double y,
@Unique
private static final ThreadLocal<DimensionType> changeDimension_from = new ThreadLocal<>();

@Inject(method = "changeDimension",
at = @At("HEAD"),
cancellable = true
)
void patchwork_fireTravelToDimensionEventChangeDimensionPlayer(DimensionType newDimension, CallbackInfoReturnable<Entity> cir) {
if (!EntityEvents.onTravelToDimension(this, newDimension)) {
cir.setReturnValue(null);
}
}

@Inject(method = "changeDimension",
at = @At(
value = "INVOKE",
Expand Down
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.world.dimension.DimensionType;
import net.minecraft.entity.Entity;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.LivingEntity;
Expand Down Expand Up @@ -110,6 +111,10 @@ public static boolean onPlayerAttackTarget(PlayerEntity player, Entity target) {
return EntityEvents.attackEntity(player, target);
}

public static boolean onTravelToDimension(Entity entity, DimensionType dimensionType) {
return EntityEvents.onTravelToDimension(entity, dimensionType);
}

public static int onBlockBreakEvent(World world, GameMode gameType, ServerPlayerEntity entityPlayer, BlockPos pos) {
return BlockHarvestManager.onBlockBreakEvent(world, gameType, entityPlayer, pos);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.minecraft.entity.passive.AnimalEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.projectile.ExplosiveProjectileEntity;
import net.minecraft.entity.projectile.ProjectileEntity;
import net.minecraft.entity.thrown.ThrownEntity;
Expand All @@ -47,6 +48,7 @@

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.extensions.block.BlockHarvestManager;

Expand Down Expand Up @@ -110,4 +112,8 @@ public static boolean onProjectileImpact(ExplosiveProjectileEntity fireball, Hit
public static boolean onProjectileImpact(ThrownEntity throwable, HitResult ray) {
return EntityEvents.onProjectileImpact(throwable, ray);
}

public static int onItemPickup(ItemEntity item, PlayerEntity player) {
return PlayerEvents.onItemPickup(player, item);
}
}