Skip to content

Commit

Permalink
Add foundation for binding script variables
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Dec 11, 2023
1 parent 0d96ff2 commit 3370922
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@
import net.minecraftforge.registries.NewRegistryEvent;
import org.apache.logging.log4j.Level;
import org.cyclops.cyclopscore.config.ConfigHandler;
import org.cyclops.cyclopscore.helper.MinecraftHelpers;
import org.cyclops.cyclopscore.infobook.IInfoBookRegistry;
import org.cyclops.cyclopscore.init.ItemGroupMod;
import org.cyclops.cyclopscore.init.ModBaseVersionable;
import org.cyclops.cyclopscore.proxy.IClientProxy;
import org.cyclops.cyclopscore.proxy.ICommonProxy;
import org.cyclops.integrateddynamics.IntegratedDynamics;
import org.cyclops.integrateddynamics.api.item.IVariableFacadeHandlerRegistry;
import org.cyclops.integrateddynamics.infobook.OnTheDynamicsOfIntegrationBook;
import org.cyclops.integratedscripting.api.language.ILanguageHandlerRegistry;
import org.cyclops.integratedscripting.block.BlockScriptingDriveConfig;
import org.cyclops.integratedscripting.blockentity.BlockEntityScriptingDriveConfig;
import org.cyclops.integratedscripting.capability.ScriptingNetworkCapabilityConstructors;
import org.cyclops.integratedscripting.capability.network.ScriptingNetworkConfig;
import org.cyclops.integratedscripting.command.CommandTestScript;
import org.cyclops.integratedscripting.core.client.model.ScriptingVariableModelProviders;
import org.cyclops.integratedscripting.core.evaluate.ScriptVariableFacadeHandler;
import org.cyclops.integratedscripting.core.language.LanguageHandlerRegistry;
import org.cyclops.integratedscripting.core.language.LanguageHandlers;
import org.cyclops.integratedscripting.core.network.ScriptingData;
Expand Down Expand Up @@ -68,6 +72,16 @@ public IntegratedScripting() {
}

public void onRegistriesCreate(NewRegistryEvent event) {
// Register handlers
IntegratedDynamics._instance.getRegistryManager().getRegistry(IVariableFacadeHandlerRegistry.class)
.registerHandler(ScriptVariableFacadeHandler.getInstance());

// Load client models
if (MinecraftHelpers.isClientSide()) {
ScriptingVariableModelProviders.load();
}

// Load parts
PartTypes.load();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.cyclops.integratedscripting.api.item;

import org.cyclops.integrateddynamics.api.item.IVariableFacade;

import java.nio.file.Path;

/**
* Variable facade for variables that refer to a script.
* @author rubensworks
*/
public interface IScriptVariableFacade extends IVariableFacade {

/**
* @return The script disk id.
*/
public int getDisk();

/**
* @return The path to the script inside the disk.
*/
public Path getPath();

/**
* @return The referenced member element inside the file referred to by the path inside the disk.
*/
public String getMember();

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.cyclops.cyclopscore.helper.Helpers;
import org.cyclops.cyclopscore.helper.L10NHelpers;
import org.cyclops.cyclopscore.helper.RenderHelpers;
import org.cyclops.integrateddynamics.core.client.gui.container.DisplayErrorsComponent;
import org.cyclops.integratedscripting.GeneralConfig;
import org.cyclops.integratedscripting.IntegratedScripting;
import org.cyclops.integratedscripting.Reference;
Expand Down Expand Up @@ -65,6 +66,7 @@ public class ContainerScreenTerminalScripting extends ContainerScreenExtended<Co
private ButtonText buttonCreateFile;
private WidgetDialog pendingScriptRemovalDialog;
private int lastClientSyncTick;
private final DisplayErrorsComponent displayErrors = new DisplayErrorsComponent();

public ContainerScreenTerminalScripting(ContainerTerminalScripting container, Inventory inventory, Component title) {
super(container, inventory, title);
Expand Down Expand Up @@ -169,6 +171,14 @@ public int getBaseYSize() {
return 240;
}

protected int getErrorX() {
return 212;
}

protected int getErrorY() {
return 139;
}

@Override
protected void renderBg(PoseStack matrixStack, float partialTicks, int mouseX, int mouseY) {
super.renderBg(matrixStack, partialTicks, mouseX, mouseY);
Expand All @@ -191,6 +201,9 @@ protected void renderBg(PoseStack matrixStack, float partialTicks, int mouseX, i
fill(matrixStack, leftPos + SCRIPT_X, topPos + SCRIPT_Y, leftPos + SCRIPT_X + SCRIPT_WIDTH, topPos + SCRIPT_Y + SCRIPT_HEIGHT, Helpers.RGBAToInt(50, 50, 50, 100));
RenderSystem.setShaderColor(1, 1, 1, 1);
}

displayErrors.drawBackground(matrixStack, getMenu().getReadErrors(), getErrorX(), getErrorY(), getErrorX(), getErrorY(), this,
this.leftPos, this.topPos, getMenu().canWriteScriptToVariable());
}

@Nullable
Expand Down Expand Up @@ -272,6 +285,8 @@ protected void renderLabels(PoseStack poseStack, int mouseX, int mouseY) {

// Draw disk label
drawString(poseStack, font, L10NHelpers.localize("gui.integratedscripting.disk") + ":", 8, 6, 16777215);

displayErrors.drawForeground(poseStack, getMenu().getReadErrors(), getErrorX(), getErrorY(), mouseX, mouseY, this, this.leftPos, this.topPos);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.cyclops.integratedscripting.core.client.model;

import net.minecraft.resources.ResourceLocation;
import org.cyclops.integrateddynamics.IntegratedDynamics;
import org.cyclops.integrateddynamics.api.client.model.IVariableModelProviderRegistry;
import org.cyclops.integrateddynamics.core.client.model.SingleVariableModelProvider;
import org.cyclops.integratedscripting.Reference;

/**
* Collection of variable model providers.
* @author rubensworks
*/
public class ScriptingVariableModelProviders {

public static final IVariableModelProviderRegistry REGISTRY = IntegratedDynamics._instance.getRegistryManager().getRegistry(IVariableModelProviderRegistry.class);

public static final SingleVariableModelProvider SCRIPT = REGISTRY.addProvider(new SingleVariableModelProvider(new ResourceLocation(Reference.MOD_ID, "customoverlay/script")));

public static void load() {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.cyclops.integratedscripting.core.evaluate;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import org.cyclops.integrateddynamics.api.item.IVariableFacadeHandler;
import org.cyclops.integratedscripting.Reference;
import org.cyclops.integratedscripting.api.item.IScriptVariableFacade;
import org.cyclops.integratedscripting.core.item.ScriptVariableFacade;

import java.nio.file.Path;

/**
* Handler for script variable facades.
* @author rubensworks
*/
public class ScriptVariableFacadeHandler implements IVariableFacadeHandler<IScriptVariableFacade> {

private static final IScriptVariableFacade INVALID_FACADE = new ScriptVariableFacade(false, -1, Path.of(""), "");
private static ScriptVariableFacadeHandler _instance;

private ScriptVariableFacadeHandler() {

}

public static ScriptVariableFacadeHandler getInstance() {
if(_instance == null) _instance = new ScriptVariableFacadeHandler();
return _instance;
}

@Override
public ResourceLocation getUniqueName() {
return new ResourceLocation(Reference.MOD_ID, "script");
}

@Override
public IScriptVariableFacade getVariableFacade(int id, CompoundTag tag) {
if(!tag.contains("disk", Tag.TAG_INT)) {
return INVALID_FACADE;
}
if(!tag.contains("path", Tag.TAG_STRING)) {
return INVALID_FACADE;
}
if(!tag.contains("member", Tag.TAG_STRING)) {
return INVALID_FACADE;
}
return new ScriptVariableFacade(id, tag.getInt("disk"), Path.of(tag.getString("path")), tag.getString("member"));
}

@Override
public void setVariableFacade(CompoundTag tag, IScriptVariableFacade variableFacade) {
tag.putInt("disk", variableFacade.getDisk());
tag.putString("path", variableFacade.getPath().toString());
tag.putString("member", variableFacade.getMember());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package org.cyclops.integratedscripting.core.item;

import lombok.Data;
import lombok.EqualsAndHashCode;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.network.chat.Component;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.model.data.ModelData;
import org.cyclops.integrateddynamics.api.client.model.IVariableModelBaked;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValueType;
import org.cyclops.integrateddynamics.api.evaluate.variable.IVariable;
import org.cyclops.integrateddynamics.api.network.IPartNetwork;
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypes;
import org.cyclops.integrateddynamics.core.item.VariableFacadeBase;
import org.cyclops.integratedscripting.api.item.IScriptVariableFacade;
import org.cyclops.integratedscripting.core.client.model.ScriptingVariableModelProviders;

import java.nio.file.Path;
import java.util.List;

/**
* Variable facade for variables determined by proxies.
* @author rubensworks
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class ScriptVariableFacade extends VariableFacadeBase implements IScriptVariableFacade {

private final int disk;
private final Path path;
private final String member;

public ScriptVariableFacade(boolean generateId, int disk, Path path, String member) {
super(generateId);
this.disk = disk;
this.path = path;
this.member = member;
}

public ScriptVariableFacade(int id, int disk, Path path, String member) {
super(id);
this.disk = disk;
this.path = path;
this.member = member;
}

@Override
public <V extends IValue> IVariable<V> getVariable(IPartNetwork network) {
if(isValid()) {
// TODO: get variable of script from scripting network
// IVariable<V> variable = getTargetVariable(network).orElse(null);
// return variable;
}
return null;
}

@Override
public boolean isValid() {
return disk >= 0;
}

@Override
public void validate(IPartNetwork network, IValidator validator, IValueType containingValueType) {
// TODO: validation
// Optional<IVariable> targetVariable = getTargetVariable(network);
// if (!isValid()) {
// validator.addError(Component.translatable(L10NValues.VARIABLE_ERROR_INVALIDITEM));
// } else if (network.getScript(proxyId) == null) {
// validator.addError(getScriptNotInNetworkError());
// } else if (!targetVariable.isPresent()) {
// validator.addError(getScriptInvalidError());
// } else if (!ValueHelpers.correspondsTo(containingValueType, targetVariable.get().getType())) {
// validator.addError(getScriptInvalidTypeError(network, containingValueType,
// targetVariable.get().getType()));
// }

getVariable(network);
}

@Override
public IValueType getOutputType() {
return ValueTypes.CATEGORY_ANY;
}

protected List<Component> getScriptTooltip() {
return List.of(
Component.translatable("script.integratedscripting.tooltip.disk", getDisk()),
Component.translatable("script.integratedscripting.tooltip.path", getPath()),
Component.translatable("script.integratedscripting.tooltip.member", getMember())
);
}

@OnlyIn(Dist.CLIENT)
@Override
public void appendHoverText(List<Component> list, Level world) {
if(isValid()) {
list.addAll(getScriptTooltip());
}
super.appendHoverText(list, world);
}

@OnlyIn(Dist.CLIENT)
@Override
public void addModelOverlay(IVariableModelBaked variableModelBaked, List<BakedQuad> quads, RandomSource random, ModelData modelData) {
if(isValid()) {
quads.addAll(variableModelBaked.getSubModels(ScriptingVariableModelProviders.SCRIPT).getBakedModel().getQuads(null, null, random, modelData, null));
}
}
}
Loading

0 comments on commit 3370922

Please sign in to comment.