Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.
Closed
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
1 change: 1 addition & 0 deletions patchwork-events-entity/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ version = getSubprojectVersion(project, "0.4.0")

dependencies {
compile project(path: ':patchwork-fml', configuration: 'dev')
compile project(path: ':patchwork-extensions-entity', configuration: 'dev')
compile project(path: ':patchwork-extensions-item', configuration: 'dev')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.item;

import net.minecraftforge.event.entity.EntityEvent;

import net.minecraft.entity.ItemEntity;

/**
* Base class for all ItemEntity events. Contains a reference to the
* ItemEntity of interest. For most ItemEntity events, there's little to no
* additional useful data from the firing method that isn't already contained
* within the ItemEntity instance.
*/
public class ItemEvent extends EntityEvent {
private final ItemEntity entityItem;

/**
* Creates a new event for an EntityItem.
*
* @param itemEntity The EntityItem for this event
*/
public ItemEvent(ItemEntity itemEntity) {
super(itemEntity);
this.entityItem = itemEntity;
}

/**
* The relevant EntityItem for this event, already cast for you.
*/
public ItemEntity getEntityItem() {
return entityItem;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.item;

import net.minecraft.entity.ItemEntity;

/**
* Event that is fired when an ItemEntity's age has reached its maximum
* lifespan. Canceling this event will prevent the ItemEntity from being
* flagged as dead, thus staying it's removal from the world. If canceled
* it will add more time to the entities life equal to extraLife.
*/

public class ItemExpireEvent extends ItemEvent {
private int extraLife;

/**
* Creates a new event for an expiring EntityItem.
*
* @param entityItem The ItemEntity being deleted.
* @param extraLife The amount of time to be added to this entities lifespan if the event is canceled.
*/
public ItemExpireEvent(ItemEntity entityItem, int extraLife) {
super(entityItem);
this.setExtraLife(extraLife);
}

public int getExtraLife() {
return extraLife;
}

public void setExtraLife(int extraLife) {
this.extraLife = extraLife;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.item;

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

/**
* Event that is fired whenever a player tosses (Q) an item or drag-n-drops a
* stack of items outside the inventory GUI screens. Canceling the event will
* stop the items from entering the world, but will not prevent them being
* removed from the inventory - and thus removed from the system.
*/
// TODO: Call Location - ForgeHook#onPlayerTossEvent -> PlayerEntity#dropSelectedItem + PlayerEntity#dropItem
public class ItemTossEvent extends ItemEvent {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May as well leave this out if you're not going to implement the hooks for it

Copy link
Contributor Author

@Rongmario Rongmario Jul 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do it in the same PR if its cool :^)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go ahead

private final PlayerEntity player;

/**
* Creates a new event for ItemEntities tossed by a player.
*
* @param entityItem The ItemEntity being tossed.
* @param player The player tossing the item.
*/
public ItemTossEvent(ItemEntity entityItem, PlayerEntity player) {
super(entityItem);
this.player = player;
}

/**
* The player tossing the item.
*/
public PlayerEntity getPlayer() {
return player;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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;

/**
* 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>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>setResult(ALLOW) is the same as the old setHandled()
*/
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import net.minecraftforge.event.entity.living.LivingEvent;

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

Expand Down Expand Up @@ -131,14 +133,38 @@ public boolean isWasDeath() {
}
}

public static class ItemPickupEvent extends PlayerEvent {
/**
* Original EntityItem with current remaining stack size.
*/
private final ItemEntity originalEntity;
/**
* Clone item stack, containing the item and amount picked up.
*/
private final ItemStack stack;

public ItemPickupEvent(PlayerEntity player, ItemEntity entPickedUp, ItemStack stack) {
super(player);
this.originalEntity = entPickedUp;
this.stack = stack;
}

public ItemStack getStack() {
return stack;
}

public ItemEntity getOriginalEntity() {
return originalEntity;
}
}

/*TODO Events:
HarvestCheck
BreakSpeed
NameFormat
LoadFromFile
SaveToFile
Visibility
ItemPickupEvent
ItemCraftedEvent
ItemSmeltedEvent
PlayerLoggedOutEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@

package net.patchworkmc.impl.event.entity;

import com.google.common.collect.Lists;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.extensions.IForgeEntity;
import net.minecraftforge.common.extensions.IForgeItem;
import net.minecraftforge.event.entity.EntityEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.item.ItemExpireEvent;
import net.minecraftforge.event.entity.item.ItemTossEvent;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.event.entity.living.LivingDamageEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
Expand All @@ -32,6 +36,7 @@
import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent;
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
import net.minecraftforge.event.entity.player.AttackEntityEvent;
import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.entity.player.PlayerFlyableFallEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
Expand All @@ -43,6 +48,7 @@
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityDimensions;
import net.minecraft.entity.EntityPose;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.SpawnType;
import net.minecraft.entity.damage.DamageSource;
Expand Down Expand Up @@ -181,6 +187,51 @@ public static boolean attackEntity(PlayerEntity player, Entity target) {
return !item.onLeftClickEntity(stack, player, target);
}

public static void firePlayerItemPickupEvent(PlayerEntity player, ItemEntity item, ItemStack clone) {
MinecraftForge.EVENT_BUS.post(new PlayerEvent.ItemPickupEvent(player, item, clone));
}

public static boolean onItemPickup(ItemEntity entityItem, PlayerEntity player) {
return MinecraftForge.EVENT_BUS.post(new EntityItemPickupEvent(player, entityItem));
}

public static int onItemExpire(ItemEntity entity, ItemStack item) {
if (item.isEmpty()) {
return -1;
}

ItemExpireEvent event = new ItemExpireEvent(entity, 6000);
// TODO: ItemExpireEvent event = new ItemExpireEvent(entity, (item.isEmpty() ? 6000 : item.getItem().getEntityLifespan(item, entity.world)));

if (!MinecraftForge.EVENT_BUS.post(event)) {
return -1;
}

return event.getExtraLife();
}

public static ItemEntity onPlayerTossEvent(PlayerEntity player, ItemStack item, boolean includeName) {
((IForgeEntity) player).captureDrops(Lists.newArrayList());
ItemEntity ret = player.dropItem(item, false, includeName);
((IForgeEntity) player).captureDrops(null);

if (ret == null) {
return null;
}

ItemTossEvent event = new ItemTossEvent(ret, player);

if (MinecraftForge.EVENT_BUS.post(event)) {
return null;
}

if (!player.world.isClient) {
player.getEntityWorld().spawnEntity(event.getEntityItem());
}

return event.getEntityItem();
}

@Override
public void onInitialize() {
UseItemCallback.EVENT.register((player, world, hand) -> {
Expand Down
Loading