generated from CleanroomMC/TemplateDevEnv
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* reloadable commands & helper * rename class * fix commands not being removed * fix OreDictIngredient mutating ores list * prevent overwriting existing commands
- Loading branch information
Showing
10 changed files
with
169 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/main/java/com/cleanroommc/groovyscript/compat/vanilla/Command.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package com.cleanroommc.groovyscript.compat.vanilla; | ||
|
||
import com.cleanroommc.groovyscript.GroovyScript; | ||
import com.cleanroommc.groovyscript.api.GroovyBlacklist; | ||
import com.cleanroommc.groovyscript.api.GroovyLog; | ||
import com.cleanroommc.groovyscript.api.IScriptReloadable; | ||
import com.cleanroommc.groovyscript.command.SimpleCommand; | ||
import com.cleanroommc.groovyscript.core.mixin.CommandHandlerAccessor; | ||
import com.cleanroommc.groovyscript.registry.AbstractReloadableStorage; | ||
import com.cleanroommc.groovyscript.registry.NamedRegistry; | ||
import net.minecraft.command.CommandHandler; | ||
import net.minecraft.command.ICommand; | ||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraftforge.client.ClientCommandHandler; | ||
import net.minecraftforge.fml.common.FMLCommonHandler; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
|
||
public class Command extends NamedRegistry implements IScriptReloadable { | ||
|
||
private final List<ICommand> serverCommands = new ArrayList<>(); | ||
private final AbstractReloadableStorage<ICommand> serverReloadableCommands = new AbstractReloadableStorage<>(); | ||
private final AbstractReloadableStorage<ICommand> clientReloadableCommands = new AbstractReloadableStorage<>(); | ||
private boolean serverStarted = false; | ||
|
||
public void registerCommand(ICommand command) { | ||
if (GroovyScript.getSandbox().isRunning() && GroovyScript.getSandbox().getCurrentLoader().isReloadable()) { | ||
this.serverReloadableCommands.addScripted(command); | ||
} else { | ||
this.serverCommands.add(command); | ||
} | ||
if (this.serverStarted) { | ||
forServer(commandHandler -> registerCommand(commandHandler, command)); | ||
} | ||
} | ||
|
||
public void registerClientCommand(ICommand command) { | ||
if (FMLCommonHandler.instance().getSide().isServer()) return; | ||
|
||
if (registerCommand(ClientCommandHandler.instance, command) && GroovyScript.getSandbox().isRunning() && GroovyScript.getSandbox().getCurrentLoader().isReloadable()) { | ||
this.clientReloadableCommands.addScripted(command); | ||
} | ||
} | ||
|
||
public void registerCommand(String name, String usage, SimpleCommand.ICommand command) { | ||
registerCommand(new SimpleCommand(name, usage, command)); | ||
} | ||
|
||
public void registerCommand(String name, SimpleCommand.ICommand command) { | ||
registerCommand(new SimpleCommand(name, "/" + name, command)); | ||
} | ||
|
||
public void registerClientCommand(String name, String usage, SimpleCommand.ICommand command) { | ||
registerClientCommand(new SimpleCommand(name, usage, command)); | ||
} | ||
|
||
public void registerClientCommand(String name, SimpleCommand.ICommand command) { | ||
registerClientCommand(new SimpleCommand(name, "/" + name, command)); | ||
} | ||
|
||
public boolean registerCommand(CommandHandler handler, ICommand command) { | ||
if (handler.getCommands().containsKey(command.getName())) { | ||
GroovyLog.get().error("Error registering command '/{}', because a command with that name already exists", command.getName()); | ||
return false; | ||
} | ||
for (String alias : command.getAliases()) { | ||
if (handler.getCommands().containsKey(alias)) { | ||
GroovyLog.get().error("Error registering command '/{}', because a command for the alias '/{}' already exists", command.getName(), alias); | ||
return false; | ||
} | ||
} | ||
handler.registerCommand(command); | ||
return true; | ||
} | ||
|
||
@GroovyBlacklist | ||
public void removeCommand(CommandHandler commandHandler, ICommand command) { | ||
Set<ICommand> commands = ((CommandHandlerAccessor) commandHandler).getCommandSet(); | ||
if (commands.remove(command)) { | ||
commandHandler.getCommands().entrySet().removeIf(entry -> Objects.equals(command, entry.getValue())); | ||
} | ||
} | ||
|
||
@GroovyBlacklist | ||
public void onStartServer(MinecraftServer server) { | ||
this.serverStarted = true; | ||
CommandHandler commandHandler = (CommandHandler) server.getCommandManager(); | ||
for (ICommand command : this.serverCommands) { | ||
registerCommand(commandHandler, command); | ||
} | ||
for (ICommand command : this.serverReloadableCommands.getScriptedRecipes()) { | ||
registerCommand(commandHandler, command); | ||
} | ||
} | ||
|
||
@GroovyBlacklist | ||
public void onReload() { | ||
this.clientReloadableCommands.removeScripted().forEach(c -> removeCommand(ClientCommandHandler.instance, c)); | ||
forServer(commandHandler -> this.serverReloadableCommands.removeScripted().forEach(c -> removeCommand(commandHandler, c))); | ||
} | ||
|
||
@Override | ||
public void afterScriptLoad() {} | ||
|
||
private void forServer(Consumer<CommandHandler> consumer) { | ||
MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance(); | ||
if (server != null) { | ||
consumer.accept((CommandHandler) server.getCommandManager()); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/cleanroommc/groovyscript/compat/vanilla/CommandSenderExpansion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.cleanroommc.groovyscript.compat.vanilla; | ||
|
||
import net.minecraft.command.ICommandSender; | ||
import net.minecraft.entity.player.EntityPlayer; | ||
import net.minecraft.util.text.TextComponentString; | ||
|
||
public class CommandSenderExpansion { | ||
|
||
public static boolean isPlayer(ICommandSender commandSender) { | ||
return commandSender instanceof EntityPlayer; | ||
} | ||
|
||
public static void sendMessage(ICommandSender commandSender, String msg) { | ||
commandSender.sendMessage(new TextComponentString(msg)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/com/cleanroommc/groovyscript/core/mixin/CommandHandlerAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.cleanroommc.groovyscript.core.mixin; | ||
|
||
import net.minecraft.command.CommandHandler; | ||
import net.minecraft.command.ICommand; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.gen.Accessor; | ||
|
||
import java.util.Set; | ||
|
||
@Mixin(CommandHandler.class) | ||
public interface CommandHandlerAccessor { | ||
|
||
@Accessor | ||
Set<ICommand> getCommandSet(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters