mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-22 13:09:59 +00:00
Create ItemFishedEvent, the event is cancelable, but items are not mutable. Use LootTables to control that. (#3705)
This commit is contained in:
1 parent
0b41a0a822
commit
6ffd94a08e
3 files changed
+173
No files matched your search
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016.
|
||||
*
|
||||
* 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 com.google.common.base.Preconditions;
|
||||
import net.minecraft.entity.projectile.EntityFishHook;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.NonNullList;
|
||||
import net.minecraftforge.fml.common.eventhandler.Cancelable;
|
||||
|
||||
import javax.annotation.Nonnegative;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This event is called when a player fishes an item.
|
||||
*
|
||||
* This event is {@link net.minecraftforge.fml.common.eventhandler.Cancelable}
|
||||
* Canceling the event will cause the player to receive no items at all.
|
||||
* The hook will still take the damage specified
|
||||
*/
|
||||
@Cancelable
|
||||
public class ItemFishedEvent extends PlayerEvent
|
||||
{
|
||||
private final NonNullList<ItemStack> stacks = NonNullList.create();
|
||||
private final EntityFishHook hook;
|
||||
private int rodDamage;
|
||||
|
||||
public ItemFishedEvent(List<ItemStack> stacks, int rodDamage, EntityFishHook hook)
|
||||
{
|
||||
super(hook.getAngler());
|
||||
this.stacks.addAll(stacks);
|
||||
this.rodDamage = rodDamage;
|
||||
this.hook = hook;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the damage the rod will take.
|
||||
* @return The damage the rod will take
|
||||
*/
|
||||
public int getRodDamage()
|
||||
{
|
||||
return rodDamage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies the amount of damage that the fishing rod should take.
|
||||
* This is not added to the pre-existing damage to be taken.
|
||||
* @param rodDamage The damage the rod will take. Must be nonnegative
|
||||
*/
|
||||
public void damageRodBy(@Nonnegative int rodDamage)
|
||||
{
|
||||
Preconditions.checkArgument(rodDamage >= 0);
|
||||
this.rodDamage = rodDamage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this to get the items the player will receive.
|
||||
* You cannot use this to modify the drops the player will get.
|
||||
* If you want to affect the loot, you should use LootTables.
|
||||
*/
|
||||
public NonNullList<ItemStack> getDrops()
|
||||
{
|
||||
return stacks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this to stuff related to the hook itself, like the position of the bobber.
|
||||
*/
|
||||
public EntityFishHook getHookEntity()
|
||||
{
|
||||
return hook;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package net.minecraftforge.test;
|
||||
|
||||
import net.minecraft.entity.projectile.EntityFishHook;
|
||||
import net.minecraft.init.Biomes;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.biome.Biome;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.entity.player.ItemFishedEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@Mod(modid = "itemfishtest", name = "ItemFishTest", version = "1.0.0")
|
||||
public class ItemFishedTest
|
||||
{
|
||||
|
||||
private static final boolean ENABLE = false;
|
||||
private static Logger logger;
|
||||
|
||||
@Mod.EventHandler
|
||||
public void onInit(FMLPreInitializationEvent event)
|
||||
{
|
||||
if (ENABLE)
|
||||
{
|
||||
logger = event.getModLog();
|
||||
logger.info("Enabling Fishing Test mod");
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onItemFished(ItemFishedEvent event)
|
||||
{
|
||||
EntityFishHook hook = event.getHookEntity();
|
||||
BlockPos bobberPos = hook.getPosition();
|
||||
Biome biomeFishedAt = hook.getEntityWorld().getBiome(bobberPos);
|
||||
logger.info("Item fished in Biome " + biomeFishedAt.getBiomeName());
|
||||
if (biomeFishedAt.equals(Biomes.OCEAN))
|
||||
{
|
||||
logger.info("Canceling the event because biome is ocean");
|
||||
event.setCanceled(true);
|
||||
}
|
||||
event.damageRodBy(50);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user