Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prototype configuration tool #1663

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions src/main/java/gregtech/api/capability/ConfigurationContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package gregtech.api.capability;

import net.minecraft.util.EnumFacing;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;

/**
* Context used during configuration events
*/
public interface ConfigurationContext extends ICapabilityProvider {

default boolean isAdvanced() {
return false;
}

// For feedback
void sendMessage(ITextComponent component);

@Override
default boolean hasCapability(final Capability<?> capability, final EnumFacing facing) {
return getCapability(capability, facing) != null;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gregtech.api.capability;

import gregtech.api.capability.tool.IConfiguratorItem;
import gregtech.api.capability.tool.IScrewdriverItem;
import gregtech.api.capability.tool.ISoftHammerItem;
import gregtech.api.capability.tool.IWrenchItem;
Expand All @@ -23,6 +24,9 @@ public class GregtechCapabilities {
@CapabilityInject(ISoftHammerItem.class)
public static Capability<ISoftHammerItem> CAPABILITY_MALLET = null;

@CapabilityInject(IConfiguratorItem.class)
public static Capability<IConfiguratorItem> CAPABILITY_CONFIGURATOR = null;

@CapabilityInject(IFuelable.class)
public static Capability<IFuelable> CAPABILITY_FUELABLE = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public class GregtechTileCapabilities {
@CapabilityInject(IActiveOutputSide.class)
public static Capability<IActiveOutputSide> CAPABILITY_ACTIVE_OUTPUT_SIDE = null;

@CapabilityInject(IConfigurable.class)
public static Capability<IConfigurable> CAPABILITY_CONFIGURABLE = null;
}
18 changes: 18 additions & 0 deletions src/main/java/gregtech/api/capability/IConfigurable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package gregtech.api.capability;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;

/**
* For configurable things
*/
public interface IConfigurable {

ResourceLocation getConfigurationID();

String getConfigurationName();

NBTTagCompound copyConfiguration(ConfigurationContext context);

void pasteConfiguration(ConfigurationContext context, NBTTagCompound configuration);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gregtech.api.capability;

import gregtech.api.capability.tool.IConfiguratorItem;
import gregtech.api.capability.tool.IScrewdriverItem;
import gregtech.api.capability.tool.ISoftHammerItem;
import gregtech.api.capability.tool.IWrenchItem;
Expand Down Expand Up @@ -40,10 +41,12 @@ public static void init() {
registerCapabilityWithNoDefault(IControllable.class);
registerCapabilityWithNoDefault(IActiveOutputSide.class);
registerCapabilityWithNoDefault(IFuelable.class);
registerCapabilityWithNoDefault(IConfigurable.class);

registerCapabilityWithNoDefault(IWrenchItem.class);
registerCapabilityWithNoDefault(IScrewdriverItem.class);
registerCapabilityWithNoDefault(ISoftHammerItem.class);
registerCapabilityWithNoDefault(IConfiguratorItem.class);

//internal capabilities
CapabilityManager.INSTANCE.register(GTWorldGenCapability.class, GTWorldGenCapability.STORAGE, GTWorldGenCapability.FACTORY);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gregtech.api.capability.impl;

import gregtech.api.GTValues;
import gregtech.api.capability.ConfigurationContext;
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.capability.IMultipleTankHandler;
import gregtech.api.capability.IWorkable;
Expand Down Expand Up @@ -504,4 +505,30 @@ public void deserializeNBT(NBTTagCompound compound) {
}
}

@Override
protected boolean isConfigurable() {
return true;
}

@Override
public NBTTagCompound copyConfiguration(final ConfigurationContext context) {
final NBTTagCompound compound = super.copyConfiguration(context);
compound.setBoolean(ALLOW_OVERCLOCKING, this.allowOverclocking);
compound.setLong(OVERCLOCK_VOLTAGE, this.overclockVoltage);
return compound;
}

@Override
public void pasteConfiguration(final ConfigurationContext context, final NBTTagCompound compound) {
super.pasteConfiguration(context, compound);
if (compound.hasKey(ALLOW_OVERCLOCKING)) {
setAllowOverclocking(compound.getBoolean(ALLOW_OVERCLOCKING));
}
if (compound.hasKey(OVERCLOCK_VOLTAGE)) {
this.overclockVoltage = compound.getLong(OVERCLOCK_VOLTAGE);
} else {
// Calculate overclock voltage based on old allow flag
this.overclockVoltage = this.allowOverclocking ? getMaxVoltage() : 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package gregtech.api.capability.impl;

import gregtech.api.capability.ConfigurationContext;
import gregtech.api.capability.GregtechCapabilities;
import gregtech.api.capability.tool.IConfiguratorItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.common.capabilities.Capability;

public class PlayerConfigurationContext implements ConfigurationContext {

private final EntityPlayer player;
private final ItemStack configurator;
private final boolean advanced;

public PlayerConfigurationContext(final EntityPlayer player, final ItemStack configurator) {
this.player = player;
this.configurator = configurator;
boolean isAdvanced = false;
// Review: Should this be an error (the passed item is not a configurator)?
if (configurator != null) {
final IConfiguratorItem item = configurator.getCapability(GregtechCapabilities.CAPABILITY_CONFIGURATOR, null);
if (item != null) {
isAdvanced = item.isAdvanced();
}
}
this.advanced = isAdvanced;
}

@Override
public boolean isAdvanced() {
return this.advanced;
}

// Review: available via casting - not part of the generic api
public ItemStack getConfigurator() {
return this.configurator;
}

@Override
public void sendMessage(final ITextComponent component) {
this.player.sendMessage(component);
}

@Override
public <T> T getCapability(final Capability<T> capability, final EnumFacing facing) {
return isAdvanced() ? this.player.getCapability(capability, facing) : null;
}
}
11 changes: 11 additions & 0 deletions src/main/java/gregtech/api/capability/tool/IConfiguratorItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gregtech.api.capability.tool;

public interface IConfiguratorItem {

default boolean isAdvanced() {
return false;
}

boolean damageItem(int damage, boolean simulate);

}
37 changes: 35 additions & 2 deletions src/main/java/gregtech/api/cover/CoverBehavior.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@
import codechicken.lib.vec.Matrix4;
import com.google.common.collect.Lists;
import gregtech.api.GTValues;
import gregtech.api.capability.ConfigurationContext;
import gregtech.api.capability.IConfigurable;
import gregtech.api.gui.IUIHolder;
import gregtech.api.items.metaitem.MetaItem;
import gregtech.api.items.metaitem.MetaItem.MetaValueItem;
import gregtech.api.render.SimpleSidedCubeRenderer.RenderSide;
import gregtech.api.render.Textures;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
Expand All @@ -33,8 +39,7 @@
* <p>
* Can implement {@link net.minecraft.util.ITickable} to listen to meta tile entity updates
*/
@SuppressWarnings("unused")
public abstract class CoverBehavior implements IUIHolder {
public abstract class CoverBehavior implements IUIHolder, IConfigurable {

private CoverDefinition coverDefinition;
public final ICoverable coverHolder;
Expand Down Expand Up @@ -85,6 +90,34 @@ public void readFromNBT(NBTTagCompound tagCompound) {
this.redstoneSignalOutput = tagCompound.getInteger("RedstoneSignal");
}

@Override
public ResourceLocation getConfigurationID() {
return getCoverDefinition().getCoverId();
}

@Override
public String getConfigurationName() {
// FIXME: how to do this properly?
final ItemStack coverStack = getCoverDefinition().getDropItemStack();
final Item item = coverStack.getItem();
if (item instanceof MetaItem) {
MetaItem<?> metaItem = (MetaItem<?>) item;
MetaItem<?>.MetaValueItem metaValueItem = metaItem.getItem(coverStack);
return String.format("metaitem.%s.name", metaValueItem.unlocalizedName);
}
return String.format("%s.name", coverStack.getTranslationKey());
}

@Override
public NBTTagCompound copyConfiguration(final ConfigurationContext context) {
return new NBTTagCompound();
}

@Override
public void pasteConfiguration(final ConfigurationContext context, final NBTTagCompound configuration) {
// nothing by default
}

public void writeInitialSyncData(PacketBuffer packetBuffer) {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package gregtech.api.items.toolitem;

import gregtech.api.capability.GregtechCapabilities;
import gregtech.api.capability.tool.IConfiguratorItem;
import gregtech.api.items.metaitem.stats.IItemCapabilityProvider;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;

public class ConfiguratorItemStat implements IItemCapabilityProvider {

private boolean advanced = false;

@Override
public ICapabilityProvider createProvider(final ItemStack itemStack) {
return new CapabilityProvider(itemStack);
}

public ConfiguratorItemStat advanced() {
this.advanced = true;
return this;
}

private class CapabilityProvider extends AbstractToolItemCapabilityProvider<IConfiguratorItem> implements IConfiguratorItem {

public CapabilityProvider(final ItemStack itemStack) {
super(itemStack);
}

@Override
public boolean isAdvanced() {
return ConfiguratorItemStat.this.advanced;
}

@Override
protected Capability<IConfiguratorItem> getCapability() {
return GregtechCapabilities.CAPABILITY_CONFIGURATOR;
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/gregtech/api/metatileentity/MTETrait.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import java.util.function.Consumer;

import gregtech.api.capability.ConfigurationContext;

public abstract class MTETrait {

protected MetaTileEntity metaTileEntity;
Expand Down Expand Up @@ -39,6 +41,20 @@ public NBTTagCompound serializeNBT() {
public void deserializeNBT(NBTTagCompound compound) {
}

@SuppressWarnings("static-method")
protected boolean isConfigurable() {
return false;
}

@SuppressWarnings("static-method")
public NBTTagCompound copyConfiguration(final ConfigurationContext context) {
return new NBTTagCompound();
}

public void pasteConfiguration(final ConfigurationContext context, final NBTTagCompound compound) {
// nothing by default
}

public void writeInitialData(PacketBuffer buffer) {
}

Expand Down
Loading