mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-17 02:55:17 +00:00
Port CommandEvent and custom entity selectors to 1.13 (#5411)
This commit is contained in:
1 parent
2e5f25d8ae
commit
8084ca43b9
12 files changed
+429
-167
No files matched your search
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2018.
|
||||
*
|
||||
* 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.debug.chat;
|
||||
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.tree.CommandNode;
|
||||
import net.minecraft.command.CommandException;
|
||||
import net.minecraft.command.CommandSource;
|
||||
import net.minecraft.util.text.TextComponentString;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.CommandEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Mod("command_event_test")
|
||||
public class CommandEventTest
|
||||
{
|
||||
public final Logger LOGGER = LogManager.getLogger();
|
||||
|
||||
public CommandEventTest()
|
||||
{
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onCommand(CommandEvent event)
|
||||
{
|
||||
CommandDispatcher<CommandSource> dispatcher = event.getParseResults().getContext().getDispatcher();
|
||||
List<CommandNode<CommandSource>> nodes = new ArrayList<>(event.getParseResults().getContext().getNodes().keySet());
|
||||
CommandSource source = event.getParseResults().getContext().getSource();
|
||||
|
||||
// test: when the /time command is used with no arguments, automatically add default arguments (/time set day)
|
||||
if (nodes.size() == 1 && nodes.get(0) == dispatcher.getRoot().getChild("time"))
|
||||
{
|
||||
event.setParseResults(dispatcher.parse("time set day", source));
|
||||
return;
|
||||
}
|
||||
|
||||
// test: whenever a player uses the /give command, let everyone on the server know
|
||||
if (nodes.size() > 0 && nodes.get(0) == dispatcher.getRoot().getChild("give"))
|
||||
{
|
||||
String msg = source.getName() + " used the give command: " + event.getParseResults().getReader().getString();
|
||||
source.getServer().getPlayerList().getPlayers().forEach(entityPlayerMP -> entityPlayerMP.sendMessage(new TextComponentString(msg)));
|
||||
return;
|
||||
}
|
||||
|
||||
// test: when the /kill command is used with no arguments, throw a custom exception
|
||||
if (nodes.size() == 1 && nodes.get(0) == dispatcher.getRoot().getChild("kill"))
|
||||
{
|
||||
event.setException(new CommandException(new TextComponentString("You tried to use the /kill command with no arguments")));
|
||||
event.setCanceled(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2018.
|
||||
*
|
||||
* 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.debug.chat;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
import net.minecraft.util.text.TextComponentString;
|
||||
import net.minecraftforge.fml.common.IEntitySelectorFactory;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.event.FMLInitializationEvent;
|
||||
import net.minecraftforge.fml.common.registry.GameRegistry;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* You can verify that this test is working fine by following these steps.
|
||||
* Spawn a few cows and a few sheep and then use "/kill @e[forge:min_health=!10]"
|
||||
* All entities with less than 10 health points (current, not max) should die.
|
||||
* This should include the sheep, but not the (healthy) cows.
|
||||
*/
|
||||
@Mod(modid = "customentityselectortest", name = "Custom Entity Selector Test", version = "1.0", acceptableRemoteVersions = "*")
|
||||
public class EntitySelectorFactoryTest
|
||||
{
|
||||
|
||||
@Mod.EventHandler
|
||||
public void init(FMLInitializationEvent event)
|
||||
{
|
||||
GameRegistry.registerEntitySelector(new EntitySelectorFactory(), "forge:min_health");
|
||||
}
|
||||
|
||||
private class EntitySelectorFactory implements IEntitySelectorFactory
|
||||
{
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<Predicate<Entity>> createPredicates(Map<String, String> arguments, String mainSelector, ICommandSender sender, Vec3d position)
|
||||
{
|
||||
String health = arguments.get("forge:min_health");
|
||||
|
||||
//If our selector is used in this command create a Predicate otherwise return an empty list.
|
||||
if (health != null)
|
||||
{
|
||||
final boolean invert = health.startsWith("!");
|
||||
|
||||
if (invert)
|
||||
{
|
||||
health = health.substring(1);
|
||||
}
|
||||
try
|
||||
{
|
||||
final int value = Integer.parseInt(health);
|
||||
|
||||
//Return a list of predicates. All these predicates have to apply for any entity to be selected.
|
||||
return Collections.<Predicate<Entity>>singletonList(new Predicate<Entity>()
|
||||
{
|
||||
@Override
|
||||
public boolean apply(@Nullable Entity input)
|
||||
{
|
||||
if (!(input instanceof EntityLivingBase))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return (((EntityLivingBase) input).getHealth() >= value) != invert;
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (NumberFormatException e)
|
||||
{
|
||||
sender.sendMessage(new TextComponentString("Entity selector 'forge:min_health' has to be an integer"));
|
||||
}
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2018.
|
||||
*
|
||||
* 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.debug.chat;
|
||||
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.advancements.criterion.MinMaxBounds;
|
||||
import net.minecraft.command.arguments.EntityOptions;
|
||||
import net.minecraft.command.arguments.EntitySelector;
|
||||
import net.minecraft.command.arguments.EntitySelectorParser;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.TextComponentString;
|
||||
import net.minecraftforge.common.command.EntitySelectorManager;
|
||||
import net.minecraftforge.common.command.IEntitySelectorType;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
|
||||
import net.minecraftforge.fml.javafmlmod.FMLModLoadingContext;
|
||||
|
||||
@Mod("entity_selector_test")
|
||||
public class EntitySelectorTest
|
||||
{
|
||||
public EntitySelectorTest()
|
||||
{
|
||||
FMLModLoadingContext.get().getModEventBus().addListener(this::setup);
|
||||
}
|
||||
|
||||
public void setup(FMLCommonSetupEvent event)
|
||||
{
|
||||
EntityOptions.register("health", this::healthArgument, parser -> true,
|
||||
new TextComponentString("Selects entities based on their current health."));
|
||||
EntitySelectorManager.register("er", new ExampleCustomSelector());
|
||||
}
|
||||
|
||||
/**
|
||||
* Example for a custom selector argument, checks for the health of the entity
|
||||
*/
|
||||
private void healthArgument(EntitySelectorParser parser) throws CommandSyntaxException
|
||||
{
|
||||
MinMaxBounds.FloatBound bound = MinMaxBounds.FloatBound.fromReader(parser.getReader());
|
||||
parser.addFilter(entity -> entity instanceof EntityLivingBase && bound.test(((EntityLivingBase) entity).getHealth()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Example for a custom selector type, works like @r but for entities.
|
||||
* Basically does exactly what @e[sorter=random, limit=1, ...] does.
|
||||
*/
|
||||
private class ExampleCustomSelector implements IEntitySelectorType
|
||||
{
|
||||
@Override
|
||||
public EntitySelector build(EntitySelectorParser parser) throws CommandSyntaxException
|
||||
{
|
||||
parser.setSorter(EntitySelectorParser.RANDOM);
|
||||
parser.setLimit(1);
|
||||
parser.setIncludeNonPlayers(true);
|
||||
parser.addFilter(Entity::isAlive);
|
||||
parser.setSuggestionHandler((builder, consumer) -> builder.suggest(String.valueOf('[')).buildFuture());
|
||||
if (parser.getReader().canRead() && parser.getReader().peek() == '[')
|
||||
{
|
||||
parser.getReader().skip();
|
||||
parser.setSuggestionHandler((builder, consumer) -> {
|
||||
builder.suggest(String.valueOf(']'));
|
||||
EntityOptions.suggestOptions(parser, builder);
|
||||
return builder.buildFuture();
|
||||
});
|
||||
|
||||
parser.parseArguments();
|
||||
}
|
||||
|
||||
parser.updateFilter();
|
||||
return parser.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITextComponent getSuggestionTooltip()
|
||||
{
|
||||
return new TextComponentString("Example: Selects a random entity");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user