mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-12 08:17:35 +00:00
Add support for custom entity selectors in commands (#3356)
This commit is contained in:
1 parent
27ebd0e9b9
commit
42e0fbbb02
3 files changed
+141
-2
No files matched your search
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Lists;
|
||||
import net.minecraft.command.EntitySelector;
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.fml.common.eventhandler.Event;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* EntitySelectorEvent is fired whenever Minecraft collects entity selectors.
|
||||
* This happens (one or multiple times) when you use something like @a[gamemode=1] in a command.<br>
|
||||
* This event is fired via {@link ForgeEventFactory#gatherEntitySelectors(Map, String, ICommandSender, Vec3d)},
|
||||
* which is executed in {@link net.minecraft.command.EntitySelector#matchEntities(ICommandSender, String, Class)}<br>
|
||||
* <br>
|
||||
* This event is not cancelable and does not have a result.<br>
|
||||
* <br>
|
||||
* This event is fired on the {@link MinecraftForge#EVENT_BUS}
|
||||
*/
|
||||
public class EntitySelectorEvent extends Event
|
||||
{
|
||||
|
||||
private final Map<String, String> map;
|
||||
private final String mainSelector;
|
||||
private final ICommandSender sender;
|
||||
private final Vec3d position;
|
||||
private final List<Predicate<Entity>> selectors;
|
||||
|
||||
public EntitySelectorEvent(Map<String, String> map, String mainSelector, ICommandSender sender, Vec3d position)
|
||||
{
|
||||
this.map = map;
|
||||
this.mainSelector = mainSelector;
|
||||
this.sender = sender;
|
||||
this.position = position;
|
||||
selectors = Lists.newArrayList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add your custom selector.
|
||||
* It is recommend to use "modid:name", if checking for own arguments, to avoid inter-mod interference (e.g. "@a[Forge:timeplayed=100]").
|
||||
*
|
||||
* @param selector Your custom predicate
|
||||
*/
|
||||
public void addPredicate(Predicate<Entity> selector)
|
||||
{
|
||||
if (selector == null)
|
||||
{
|
||||
throw new NullPointerException("Attempted to add null predicate as entity selector");
|
||||
}
|
||||
selectors.add(selector);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The main selector used (e.g. 'a' for all players or 'e' for all entities)
|
||||
*/
|
||||
public String getMainSelector()
|
||||
{
|
||||
return mainSelector;
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: "@a[test=true]" would result in a map with "test"=>"true"
|
||||
*
|
||||
* @return The argument map. Maps all given argument names with its value.
|
||||
*/
|
||||
public Map<String, String> getArgumentMap()
|
||||
{
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* See {@link EntitySelector#getPosFromArguments(Map, Vec3d)}
|
||||
*
|
||||
* @return A position either specified in the selector arguments or by the players position.
|
||||
*/
|
||||
public Vec3d getPosition()
|
||||
{
|
||||
return position;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The sender of the command.
|
||||
*/
|
||||
public ICommandSender getSender()
|
||||
{
|
||||
return sender;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The list of added custom selectors
|
||||
*/
|
||||
List<Predicate<Entity>> getSelectors()
|
||||
{
|
||||
return selectors;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,9 +22,12 @@ package net.minecraftforge.event;
|
||||
import java.io.File;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.EntityLiving;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
@@ -242,6 +245,13 @@ public class ForgeEventFactory
|
||||
return event.getDisplayname();
|
||||
}
|
||||
|
||||
public static List<Predicate<Entity>> gatherEntitySelectors(Map<String, String> map, String mainSelector, ICommandSender sender, Vec3d position)
|
||||
{
|
||||
EntitySelectorEvent event=new EntitySelectorEvent(map, mainSelector, sender, position);
|
||||
MinecraftForge.EVENT_BUS.post(event);
|
||||
return event.getSelectors();
|
||||
}
|
||||
|
||||
public static float fireBlockHarvesting(List<ItemStack> drops, World world, BlockPos pos, IBlockState state, int fortune, float dropChance, boolean silkTouch, EntityPlayer player)
|
||||
{
|
||||
BlockEvent.HarvestDropsEvent event = new BlockEvent.HarvestDropsEvent(world, pos, state, fortune, dropChance, drops, player, silkTouch);
|
||||
|
||||
Reference in new issue
Block a user