Skip to content

Commit

Permalink
Improve hand command functionality (#186)
Browse files Browse the repository at this point in the history
* add dimension object mapper

* add VillagerCareer object mapper

* move GrS code creation methods to GroovyScriptCodeConverter

* add asGroovyCode for some modded objects

* add asGroovyCode for AE2 Tunnels and AS Constellations

* add multiple info commands and info parser registry

* add vanilla info parsers

* add modded info parsers

* add the ability to block specific info parsers

* fix mekanism asGroovyCode infusionType

* simplify formatForgeRegistryImpl

* fire GsHandEvent

* use comma integer for itemstacks

* make header white

* make nbt be logged instead of only tile entity nbt

* special case EntityPlayer nbt

* handle itemstack with no metadata correctly
  • Loading branch information
WaitingIdly authored Jul 23, 2024
1 parent fc8ae84 commit abe8bc6
Show file tree
Hide file tree
Showing 60 changed files with 2,497 additions and 314 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/cleanroommc/groovyscript/GroovyScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.cleanroommc.groovyscript.compat.mods.ModSupport;
import com.cleanroommc.groovyscript.compat.mods.tinkersconstruct.TinkersConstruct;
import com.cleanroommc.groovyscript.compat.vanilla.VanillaModule;
import com.cleanroommc.groovyscript.compat.vanilla.command.infoparser.StandardInfoParserRegistry;
import com.cleanroommc.groovyscript.core.mixin.DefaultResourcePackAccessor;
import com.cleanroommc.groovyscript.documentation.Documentation;
import com.cleanroommc.groovyscript.documentation.linkgenerator.LinkGeneratorHooks;
Expand Down Expand Up @@ -163,6 +164,7 @@ public static void initializeRunConfig(File minecraftHome) {
public static void initializeGroovyPreInit() {
// called via mixin in between construction and fml pre init
ObjectMapperManager.init();
StandardInfoParserRegistry.init();
VanillaModule.initializeBinding();
ModSupport.init();
for (ObjectMapper<?> goh : ObjectMapperManager.getObjectMappers()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.cleanroommc.groovyscript.api.infocommand;

import net.minecraft.util.text.Style;
import net.minecraft.util.text.TextFormatting;

public interface InfoParser {

/**
* The style for any parser header - bold and light purple.
*/
Style headerStyle = new Style().setColor(TextFormatting.WHITE).setBold(true);

/**
* Priority of the parser for determining the order they are logged in chat,
* with lowest being first and highest being last.
* The is 100, and {@link com.cleanroommc.groovyscript.compat.vanilla.command.infoparser.InfoParserItem#priority()} is set to 1.
*
* @return the priority of the Parser
*/
int priority();

/**
* The id used to determine what modes are active when running the command.
* Displayed in-game as part of the allowed argument list for the command.
*
* @return id of the parser
*/
String id();

/**
* Checks if the parser is enabled and parses it if it is.
* Regardless of if {@code enabled} is {@code true}, the parser should first check
* to see if {@link #id()} is disabled via a {@code -} prefix and should run if it was disabled.
* Then, it should check if {@link #id()} was enabled, and run if it was or if the method is enabled by default.
* Typically, {@code enabled} will be {@code true} if there are no arguments, the arguments included "{@code all}",
* or all arguments started with {@code -} to negate a specific parser being enabled.
*
* @param info the info package, containing all the information of the command
* @param enabled if this should always be parsed
* @see InfoParserPackage
*/
void parse(InfoParserPackage info, boolean enabled);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package com.cleanroommc.groovyscript.api.infocommand;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
* Created when /gs info is executed
*/
public class InfoParserPackage {

/**
* Server where the command is executed
*/
@NotNull
private final MinecraftServer server;
/**
* Player who executes the command
*/
@NotNull
private final EntityPlayer player;
/**
* Arguments of the command
*/
@NotNull
private final List<String> args;
/**
* A list of messages that will be sent to the player after this event.
* Add or remove your messages here.
*/
@NotNull
private final List<ITextComponent> messages;
/**
* If pretty nbt is enabled
*/
private final boolean prettyNbt;
/**
* The held item or the item form of the block being looked at.
*/
@NotNull
private ItemStack stack;
/**
* The entity the player is looking at
*/
@Nullable
private Entity entity;
/**
* The block position the player is looking at
*/
@Nullable
private BlockPos pos;
/**
* The block state of the held item or the block state the player is looking at
*/
@Nullable
private IBlockState blockState;
/**
* The block of the held item or the block the player is looking at
*/
@Nullable
private Block block;
/**
* The tile entity the player is looking at
*/
@Nullable
private TileEntity tileEntity;

public InfoParserPackage(
@NotNull MinecraftServer server,
@NotNull EntityPlayer player,
@NotNull List<String> args,
@NotNull List<ITextComponent> messages,
boolean prettyNbt
) {
this.server = server;
this.player = player;
this.args = args;
this.messages = messages;
this.stack = ItemStack.EMPTY;
this.prettyNbt = prettyNbt;
}

public @NotNull MinecraftServer getServer() {
return server;
}

public @NotNull EntityPlayer getPlayer() {
return player;
}

public @NotNull List<String> getArgs() {
return args;
}

public @NotNull List<ITextComponent> getMessages() {
return messages;
}

public @NotNull ItemStack getStack() {
return stack;
}

public void setStack(@NotNull ItemStack stack) {
this.stack = stack;
}

public boolean isPrettyNbt() {
return prettyNbt;
}

public @Nullable Entity getEntity() {
return entity;
}

public void setEntity(@Nullable Entity entity) {
this.entity = entity;
}

public @Nullable BlockPos getPos() {
return pos;
}

public void setPos(@Nullable BlockPos pos) {
this.pos = pos;
}

public @Nullable IBlockState getBlockState() {
return blockState;
}

public void setBlockState(@Nullable IBlockState blockState) {
this.blockState = blockState;
}

public @Nullable Block getBlock() {
return block;
}

public void setBlock(@Nullable Block block) {
this.block = block;
}

public @Nullable TileEntity getTileEntity() {
return tileEntity;
}

public void setTileEntity(@Nullable TileEntity tileEntity) {
this.tileEntity = tileEntity;
}

public void copyFromPos(BlockPos pos) {
if (pos == null) return;
this.pos = pos;
this.blockState = player.world.getBlockState(pos);
this.block = blockState.getBlock();
this.tileEntity = player.world.getTileEntity(pos);

this.stack = block.getPickBlock(blockState, Minecraft.getMinecraft().objectMouseOver, player.world, pos, player);
if (this.stack.isEmpty()) this.stack = new ItemStack(block, 1, block.getMetaFromState(blockState));
}

public void parse() {
parse(false);
}

public void parse(boolean enabled) {
InfoParserRegistry.getInfoParsers().forEach(x -> x.parse(this, enabled));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.cleanroommc.groovyscript.api.infocommand;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class InfoParserRegistry {

private static final List<InfoParser> INFO_PARSERS = new ArrayList<>();

public static void addInfoParser(InfoParser command) {
INFO_PARSERS.add(command);
}

public static List<InfoParser> getInfoParsers() {
return INFO_PARSERS.stream().sorted(Comparator.comparing(InfoParser::priority)).collect(Collectors.toList());
}

public static List<String> getIds() {
return INFO_PARSERS.stream().sorted(Comparator.comparing(InfoParser::priority)).map(InfoParser::id).collect(Collectors.toList());
}

}
Loading

0 comments on commit abe8bc6

Please sign in to comment.