mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-16 10:34:49 +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,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.common.command;
|
||||
|
||||
import com.mojang.brigadier.StringReader;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
||||
import net.minecraft.command.arguments.EntitySelector;
|
||||
import net.minecraft.command.arguments.EntitySelectorParser;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* Allows modders to register custom entity selectors by assigning an {@link IEntitySelectorType} to a String token. <br>
|
||||
* The token "test", for example, corresponds to @test[...] in a command.
|
||||
*/
|
||||
public class EntitySelectorManager
|
||||
{
|
||||
private static final HashMap<String, IEntitySelectorType> REGISTRY = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Registers a new {@link IEntitySelectorType} for the given {@code token}.<br>
|
||||
*
|
||||
* @param token Defines the name of the selector
|
||||
*/
|
||||
public static void register(String token, IEntitySelectorType type)
|
||||
{
|
||||
if (token.isEmpty())
|
||||
{
|
||||
throw new IllegalArgumentException("Token must not be empty");
|
||||
}
|
||||
|
||||
if (Arrays.asList("p", "a", "r", "s", "e").contains(token))
|
||||
{
|
||||
throw new IllegalArgumentException("Token clashes with vanilla @" + token);
|
||||
}
|
||||
|
||||
for (char c : token.toCharArray())
|
||||
{
|
||||
if (!StringReader.isAllowedInUnquotedString(c)) {
|
||||
throw new IllegalArgumentException("Token must only contain allowed characters");
|
||||
}
|
||||
}
|
||||
|
||||
REGISTRY.put(token, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called in {@link EntitySelectorParser#parse} <br>
|
||||
*
|
||||
* If the REGISTRY does not contain a custom selector for the command being parsed,
|
||||
* this method returns {@code null} and the vanilla logic in {@link EntitySelectorParser#parseSelector} is used.
|
||||
*/
|
||||
public static EntitySelector parseSelector(EntitySelectorParser parser) throws CommandSyntaxException
|
||||
{
|
||||
if (parser.getReader().canRead())
|
||||
{
|
||||
int i = parser.getReader().getCursor();
|
||||
String token = parser.getReader().readUnquotedString();
|
||||
IEntitySelectorType type = REGISTRY.get(token);
|
||||
if (type != null)
|
||||
{
|
||||
return type.build(parser);
|
||||
}
|
||||
|
||||
parser.getReader().setCursor(i);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called in {@link EntitySelectorParser#fillSelectorSuggestions}
|
||||
*/
|
||||
public static void fillSelectorSuggestions(SuggestionsBuilder suggestionBuilder)
|
||||
{
|
||||
REGISTRY.forEach((token, type) -> suggestionBuilder.suggest("@" + token, type.getSuggestionTooltip()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.common.command;
|
||||
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.command.arguments.EntitySelector;
|
||||
import net.minecraft.command.arguments.EntitySelectorParser;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
|
||||
/**
|
||||
* Implementations of this interface can be registered using {@link EntitySelectorManager#register}
|
||||
*/
|
||||
public interface IEntitySelectorType
|
||||
{
|
||||
/**
|
||||
* Returns an {@link EntitySelector} based on the given {@link EntitySelectorParser}. <br>
|
||||
*
|
||||
* Use {@link EntitySelectorParser#getReader} to read extra arguments and {@link EntitySelectorParser#addFilter} to add the corresponding filters. <br>
|
||||
* If the token being parsed does not match the syntax of this selector, this method should throw an appropriate {@link CommandSyntaxException}.
|
||||
*/
|
||||
EntitySelector build(EntitySelectorParser parser) throws CommandSyntaxException;
|
||||
|
||||
/**
|
||||
* Returns an {@link ITextComponent} containing a short description for this selector type.
|
||||
*/
|
||||
ITextComponent getSuggestionTooltip();
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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.event;
|
||||
|
||||
import com.mojang.brigadier.ParseResults;
|
||||
import net.minecraft.command.CommandSource;
|
||||
import net.minecraft.command.Commands;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.eventbus.api.Cancelable;
|
||||
import net.minecraftforge.eventbus.api.Event;
|
||||
|
||||
/**
|
||||
* CommandEvent is fired after a command is parsed, but before it is executed.
|
||||
* This event is fired during the invocation of {@link Commands#handleCommand(CommandSource, String)}. <br>
|
||||
* <br>
|
||||
* {@link #parse} contains the instance of {@link ParseResults} for the parsed command.<br>
|
||||
* {@link #exception} begins null, but can be populated with an exception to be thrown within the command.<br>
|
||||
* <br>
|
||||
* This event is {@link Cancelable}. <br>
|
||||
* If the event is canceled, the execution of the command does not occur.<br>
|
||||
* <br>
|
||||
* This event does not have a result. {@link HasResult}<br>
|
||||
* <br>
|
||||
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.<br>
|
||||
**/
|
||||
@Cancelable
|
||||
public class CommandEvent extends Event
|
||||
{
|
||||
private ParseResults<CommandSource> parse;
|
||||
private Throwable exception;
|
||||
|
||||
public CommandEvent(ParseResults<CommandSource> parse)
|
||||
{
|
||||
this.parse = parse;
|
||||
}
|
||||
|
||||
public ParseResults<CommandSource> getParseResults() { return parse; }
|
||||
public void setParseResults(ParseResults<CommandSource> parse) { this.parse = parse; }
|
||||
public Throwable getException() { return exception; }
|
||||
public void setException(Throwable exception) { this.exception = exception; }
|
||||
}
|
||||
Reference in new issue
Block a user