Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 18 additions & 28 deletions src/main/java/com/cleanroommc/groovyscript/GroovyScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
import com.cleanroommc.groovyscript.event.EventHandler;
import com.cleanroommc.groovyscript.helper.JsonHelper;
import com.cleanroommc.groovyscript.helper.StyleConstant;
import com.cleanroommc.groovyscript.keybinds.GroovyScriptKeybinds;
import com.cleanroommc.groovyscript.mapper.AbstractObjectMapper;
import com.cleanroommc.groovyscript.mapper.ObjectMapperManager;
import com.cleanroommc.groovyscript.network.CReload;
import com.cleanroommc.groovyscript.network.NetworkHandler;
import com.cleanroommc.groovyscript.network.NetworkUtils;
import com.cleanroommc.groovyscript.registry.ReloadableRegistryManager;
import com.cleanroommc.groovyscript.sandbox.*;
import com.cleanroommc.groovyscript.sandbox.mapper.GroovyDeobfMapper;
Expand All @@ -30,19 +29,15 @@
import groovy.lang.GroovySystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.command.ICommandSender;
import net.minecraft.item.Item;
import net.minecraft.util.text.Style;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.client.settings.KeyConflictContext;
import net.minecraftforge.client.settings.KeyModifier;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.ModContainer;
Expand All @@ -52,14 +47,14 @@
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.InputEvent;
import net.minecraftforge.fml.relauncher.FMLInjectionData;
import net.minecraftforge.fml.relauncher.FMLLaunchHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.input.Keyboard;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -94,9 +89,6 @@ public class GroovyScript {
private static ModContainer scriptMod;
private static Thread languageServerThread;

private static KeyBinding reloadKey;
private static long timeSinceLastUse;

public static final Random RND = new Random();

@Mod.EventHandler
Expand All @@ -116,21 +108,28 @@ public void onConstruction(FMLConstructionEvent event) {
GroovyScript.sandbox = new GroovyScriptSandbox();
ModSupport.INSTANCE.setup(event.getASMHarvestedData());

if (NetworkUtils.isDedicatedClient()) {
// this resource pack must be added in construction
((DefaultResourcePackAccessor) Minecraft.getMinecraft()).get().add(new GroovyResourcePack());
reloadKey = new KeyBinding("key.groovyscript.reload", KeyConflictContext.IN_GAME, KeyModifier.CONTROL, Keyboard.KEY_R, "key.categories.groovyscript");
ClientRegistry.registerKeyBinding(reloadKey);
}

FluidRegistry.enableUniversalBucket();
getRunConfig().initPackmode();
}

@Mod.EventHandler
@SideOnly(Side.CLIENT)
public void onClientConstruction(FMLConstructionEvent event) {
MinecraftForge.EVENT_BUS.register(GroovyScriptKeybinds.class);
// this resource pack must be added in construction
((DefaultResourcePackAccessor) Minecraft.getMinecraft()).get().add(new GroovyResourcePack());
}

@Mod.EventHandler
public void onInit(FMLInitializationEvent event) {
if (ModSupport.TINKERS_CONSTRUCT.isLoaded()) TinkersConstruct.init();
if (event.getSide().isClient() && Boolean.parseBoolean(System.getProperty("groovyscript.run_ls"))) {
}

@Mod.EventHandler
@SideOnly(Side.CLIENT)
public void onClientInit(FMLInitializationEvent event) {
GroovyScriptKeybinds.initialize();
if (Boolean.parseBoolean(System.getProperty("groovyscript.run_ls"))) {
runLanguageServer();
}
}
Expand Down Expand Up @@ -194,15 +193,6 @@ public void onServerLoad(FMLServerStartingEvent event) {
VanillaModule.INSTANCE.command.onStartServer(event.getServer());
}

@SubscribeEvent
public static void onInput(InputEvent.KeyInputEvent event) {
long time = Minecraft.getSystemTime();
if (Minecraft.getMinecraft().isIntegratedServerRunning() && reloadKey.isPressed() && time - timeSinceLastUse >= 1000 && Minecraft.getMinecraft().player.getPermissionLevel() >= 4) {
NetworkHandler.sendToServer(new CReload());
timeSinceLastUse = time;
}
}

public static @NotNull String getScriptPath() {
return getScriptFile().getPath();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.cleanroommc.groovyscript.api.infocommand;

import com.cleanroommc.groovyscript.event.GsHandEvent;
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.ItemBlock;
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 net.minecraftforge.common.MinecraftForge;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -103,6 +106,10 @@ public InfoParserPackage(

public void setStack(@NotNull ItemStack stack) {
this.stack = stack;
if (!stack.isEmpty() && stack.getItem() instanceof ItemBlock itemBlock) {
setBlock(itemBlock.getBlock());
setBlockState(itemBlock.getBlock().getStateFromMeta(stack.getMetadata()));
}
}

public boolean isPrettyNbt() {
Expand Down Expand Up @@ -165,6 +172,8 @@ public void parse() {
}

public void parse(boolean enabled) {
MinecraftForge.EVENT_BUS.post(new GsHandEvent(this));

InfoParserRegistry.getInfoParsers().forEach(x -> x.parse(this, enabled));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@

import com.cleanroommc.groovyscript.api.infocommand.InfoParserPackage;
import com.cleanroommc.groovyscript.api.infocommand.InfoParserRegistry;
import com.cleanroommc.groovyscript.event.GsHandEvent;
import com.cleanroommc.groovyscript.helper.StyleConstant;
import com.google.common.base.Predicates;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.EntitySelectors;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.common.MinecraftForge;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -28,56 +20,6 @@

public abstract class BaseInfoCommand extends CommandBase {

/**
* gets the block being looked at, stopping on fluid blocks
*/
protected static BlockPos getBlockLookingAt(EntityPlayer player) {
double distance = player.getEntityAttribute(EntityPlayer.REACH_DISTANCE).getAttributeValue();
Vec3d eyes = player.getPositionEyes(0.0F);
Vec3d look = player.getLook(0.0F);
Vec3d end = eyes.add(look.x * distance, look.y * distance, look.z * distance);

RayTraceResult result = player.getEntityWorld().rayTraceBlocks(eyes, end, true);
if (result != null && result.typeOfHit == RayTraceResult.Type.BLOCK) {
return result.getBlockPos();
}
return null;
}

/**
* gets the closest entity being looked at
*/
protected static Entity getEntityLookingAt(EntityPlayer player) {
Entity entity = null;
double d0 = 0.0D;

double distance = player.getEntityAttribute(EntityPlayer.REACH_DISTANCE).getAttributeValue();
Vec3d eyes = player.getPositionEyes(0.0F);
Vec3d look = player.getLook(0.0F);
Vec3d end = eyes.add(look.x * distance, look.y * distance, look.z * distance);

List<Entity> list = player.world.getEntitiesInAABBexcluding(
player,
player.getEntityBoundingBox()
.expand(look.x * distance, look.y * distance, look.z * distance)
.grow(1.0D, 1.0D, 1.0D),
Predicates.and(EntitySelectors.NOT_SPECTATING, e -> e != null && e.canBeCollidedWith()));

for (Entity entity1 : list) {
AxisAlignedBB axisalignedbb = entity1.getEntityBoundingBox().grow(0.3);
RayTraceResult raytraceresult = axisalignedbb.calculateIntercept(eyes, end);

if (raytraceresult != null) {
double d1 = eyes.squareDistanceTo(raytraceresult.hitVec);
if (d1 < d0 || d0 == 0.0D) {
entity = entity1;
d0 = d1;
}
}
}
return entity;
}

@Override
public @NotNull String getUsage(@NotNull ICommandSender sender) {
return String.format(
Expand Down Expand Up @@ -141,9 +83,6 @@ public void execute(@NotNull MinecraftServer server, @NotNull ICommandSender sen
// add different data to the info parser depending on the command being used
gatherInfo(info, player);

GsHandEvent event = new GsHandEvent(info);
MinecraftForge.EVENT_BUS.post(event);

info.parse(enabled);
print(player, messages, argList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.cleanroommc.groovyscript.api.infocommand.InfoParserPackage;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemBlock;
import net.minecraft.util.EnumHand;
import org.jetbrains.annotations.NotNull;

Expand All @@ -22,10 +21,5 @@ protected String targetDescription() {
void gatherInfo(InfoParserPackage info, EntityPlayer player) {
info.setStack(player.getHeldItem(EnumHand.MAIN_HAND));
if (info.getStack().isEmpty()) info.setStack(player.getHeldItem(EnumHand.OFF_HAND));

if (info.getStack().getItem() instanceof ItemBlock itemBlock) {
info.setBlock(itemBlock.getBlock());
info.setBlockState(itemBlock.getBlock().getStateFromMeta(info.getStack().getMetadata()));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.cleanroommc.groovyscript.command;

import com.cleanroommc.groovyscript.api.infocommand.InfoParserPackage;
import com.cleanroommc.groovyscript.helper.RayTracingHelper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemBlock;
import net.minecraft.util.EnumHand;
import org.jetbrains.annotations.NotNull;

Expand All @@ -26,16 +26,13 @@ void gatherInfo(InfoParserPackage info, EntityPlayer player) {
// if there's nothing in the player's hands, get the entity being looked at and then the block position
// because entity should be preferred
if (info.getStack().isEmpty()) {
info.setEntity(getEntityLookingAt(player));
info.setEntity(RayTracingHelper.getEntityLookingAt(player));
if (info.getEntity() == null) {
info.copyFromPos(getBlockLookingAt(player));
info.copyFromPos(RayTracingHelper.getBlockLookingAt(player));
if (info.getPos() == null) {
info.setEntity(player);
}
}
} else if (info.getStack().getItem() instanceof ItemBlock itemBlock) {
info.setBlock(itemBlock.getBlock());
info.setBlockState(itemBlock.getBlock().getStateFromMeta(info.getStack().getMetadata()));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cleanroommc.groovyscript.command;

import com.cleanroommc.groovyscript.api.infocommand.InfoParserPackage;
import com.cleanroommc.groovyscript.helper.RayTracingHelper;
import net.minecraft.entity.player.EntityPlayer;
import org.jetbrains.annotations.NotNull;

Expand All @@ -19,9 +20,9 @@ protected String targetDescription() {
@Override
void gatherInfo(InfoParserPackage info, EntityPlayer player) {
// get the entity being looked at and then the block position because entity should be preferred
info.setEntity(getEntityLookingAt(player));
info.setEntity(RayTracingHelper.getEntityLookingAt(player));
if (info.getEntity() == null) {
info.copyFromPos(getBlockLookingAt(player));
info.copyFromPos(RayTracingHelper.getBlockLookingAt(player));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.cleanroommc.groovyscript.helper;

import com.google.common.base.Predicates;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.EntitySelectors;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;

import java.util.List;

public class RayTracingHelper {

/**
* gets the block being looked at, stopping on fluid blocks
*/
public static BlockPos getBlockLookingAt(EntityPlayer player) {
double distance = player.getEntityAttribute(EntityPlayer.REACH_DISTANCE).getAttributeValue();
Vec3d eyes = player.getPositionEyes(0.0F);
Vec3d look = player.getLook(0.0F);
Vec3d end = eyes.add(look.x * distance, look.y * distance, look.z * distance);

RayTraceResult result = player.getEntityWorld().rayTraceBlocks(eyes, end, true);
if (result != null && result.typeOfHit == RayTraceResult.Type.BLOCK) {
return result.getBlockPos();
}
return null;
}

/**
* gets the closest entity being looked at
*/
public static Entity getEntityLookingAt(EntityPlayer player) {
Entity entity = null;
double d0 = 0.0D;

double distance = player.getEntityAttribute(EntityPlayer.REACH_DISTANCE).getAttributeValue();
Vec3d eyes = player.getPositionEyes(0.0F);
Vec3d look = player.getLook(0.0F);
Vec3d end = eyes.add(look.x * distance, look.y * distance, look.z * distance);

List<Entity> list = player.world.getEntitiesInAABBexcluding(
player,
player.getEntityBoundingBox()
.expand(look.x * distance, look.y * distance, look.z * distance)
.grow(1.0D, 1.0D, 1.0D),
Predicates.and(EntitySelectors.NOT_SPECTATING, e -> e != null && e.canBeCollidedWith()));

for (Entity entity1 : list) {
AxisAlignedBB axisalignedbb = entity1.getEntityBoundingBox().grow(0.3);
RayTraceResult raytraceresult = axisalignedbb.calculateIntercept(eyes, end);

if (raytraceresult != null) {
double d1 = eyes.squareDistanceTo(raytraceresult.hitVec);
if (d1 < d0 || d0 == 0.0D) {
entity = entity1;
d0 = d1;
}
}
}
return entity;
}
}
Loading