Skip to content

Commit

Permalink
Implemented disguise state system, to make disguise appearance more c…
Browse files Browse the repository at this point in the history
…ustomizable
  • Loading branch information
fuj1n committed Apr 29, 2020
1 parent 0c5a512 commit ece32d9
Show file tree
Hide file tree
Showing 16 changed files with 438 additions and 12 deletions.
@@ -0,0 +1,36 @@
package slimeknights.tmechworks.api.disguisestate;

import net.minecraft.block.BlockState;
import net.minecraft.state.IProperty;

import java.util.Collection;

public abstract class BasicDisguiseState<T extends Comparable<T>> extends DisguiseState<T> {
private final IProperty<T> property;
private final T defaultValue;

public BasicDisguiseState(IProperty<T> property, T defaultValue) {
this.property = property;
this.defaultValue = defaultValue;
}

@Override
public boolean canApplyTo(BlockState state) {
return state.has(property);
}

@Override
public BlockState apply(BlockState state, String value) {
return state.with(property, getValueFrom(value));
}

@Override
public Collection<T> getAllowedValues() {
return property.getAllowedValues();
}

@Override
public T getValueFrom(String value) {
return property.parseValue(value).orElse(defaultValue);
}
}
@@ -0,0 +1,31 @@
package slimeknights.tmechworks.api.disguisestate;

import net.minecraft.block.BlockState;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import slimeknights.tmechworks.TMechworks;

import java.util.Collection;

public abstract class DisguiseState<T extends Comparable<T>> {
public abstract boolean canApplyTo(BlockState state);

public abstract BlockState apply(BlockState state, String value);

public abstract Collection<T> getAllowedValues();
public abstract T getValueFrom(String state);

@OnlyIn(Dist.CLIENT)
public ResourceLocation getIconSheet() {
return new ResourceLocation(TMechworks.modId, "textures/gui/disguise_states.png");
}

@OnlyIn(Dist.CLIENT)
public final int unsafeGetIconFor(Object value) {
return getIconFor((T) value);
}

@OnlyIn(Dist.CLIENT)
public abstract int getIconFor(T value);
}
@@ -0,0 +1,37 @@
package slimeknights.tmechworks.api.disguisestate;

import net.minecraft.block.BlockState;
import net.minecraft.state.properties.BlockStateProperties;

import java.util.ArrayList;

public final class DisguiseStates {
public static ArrayList<DisguiseState<?>> disguiseStates = new ArrayList<>();

public static final SlabTypeDisguiseState SLAB_TYPE = new SlabTypeDisguiseState();

public static DisguiseState<?> getForState(BlockState state) {
for(DisguiseState<?> ds : DisguiseStates.disguiseStates) {
if(ds.canApplyTo(state))
return ds;
}

return null;
}

public static BlockState processDisguiseStates(BlockState state, String disguiseState) {
if (state.has(BlockStateProperties.FACING))
state = state.with(BlockStateProperties.FACING, state.get(BlockStateProperties.FACING));

DisguiseState<?> ds = getForState(state);
if(ds != null) {
return ds.apply(state, disguiseState);
}

return state;
}

static {
disguiseStates.add(SLAB_TYPE);
}
}
@@ -0,0 +1,33 @@
package slimeknights.tmechworks.api.disguisestate;

import com.google.common.collect.ImmutableSet;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.state.properties.SlabType;

import java.util.Collection;

public class SlabTypeDisguiseState extends BasicDisguiseState<SlabType> {
public SlabTypeDisguiseState() {
super(BlockStateProperties.SLAB_TYPE, SlabType.BOTTOM);
}

@Override
public int getIconFor(SlabType value) {
switch(value) {
case BOTTOM:
return 1;
case TOP:
return 2;
case DOUBLE:
return 3;
}

return 0;
}

@Override
public Collection<SlabType> getAllowedValues() {
// Overridden for specific order
return ImmutableSet.of(SlabType.TOP, SlabType.DOUBLE, SlabType.BOTTOM);
}
}
@@ -1,14 +1,23 @@
package slimeknights.tmechworks.client.gui;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.block.BlockState;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import slimeknights.tmechworks.TMechworks;
import slimeknights.tmechworks.api.disguisestate.DisguiseStates;
import slimeknights.tmechworks.client.gui.components.DisguiseStateWidget;
import slimeknights.tmechworks.common.blocks.tileentity.RedstoneMachineTileEntity;
import slimeknights.tmechworks.common.inventory.DisguiseContainer;

public class DisguiseScreen extends ContainerScreen<DisguiseContainer> {
public static final ResourceLocation SCREEN_LOCATION = new ResourceLocation("tmechworks", "textures/gui/generic_1.png");
public static final ResourceLocation SCREEN_LOCATION = new ResourceLocation(TMechworks.modId, "textures/gui/generic_1.png");

private DisguiseStateWidget disguiseWidget;

public DisguiseScreen(DisguiseContainer container, PlayerInventory inventory, ITextComponent name) {
super(container, inventory, name);
Expand All @@ -18,6 +27,29 @@ public static DisguiseScreen create(DisguiseContainer container, PlayerInventory
return new DisguiseScreen(container, player, title);
}

@Override
protected void init() {
super.init();

disguiseWidget = new DisguiseStateWidget(guiLeft + 99, guiTop + 30, container.getTileEntity());
addButton(disguiseWidget);
}

@Override
public void tick() {
super.tick();

RedstoneMachineTileEntity te = container.getTileEntity();
ItemStack disguise = te.getDisguiseBlock();

if (disguise.getItem() instanceof BlockItem) {
BlockState disguiseState = ((BlockItem) disguise.getItem()).getBlock().getDefaultState();
disguiseWidget.setState(DisguiseStates.getForState(disguiseState), te.getDisguiseState());
} else {
disguiseWidget.setState(null, null);
}
}

@Override
public void render(int mouseX, int mouseY, float partialTicks) {
this.renderBackground();
Expand Down
Expand Up @@ -2,17 +2,22 @@

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Direction;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.fml.network.PacketDistributor;
import slimeknights.tmechworks.TMechworks;
import slimeknights.tmechworks.api.disguisestate.DisguiseStates;
import slimeknights.tmechworks.client.gui.components.ArrowWidget;
import slimeknights.tmechworks.client.gui.components.DisguiseStateWidget;
import slimeknights.tmechworks.common.blocks.DrawbridgeBlock;
import slimeknights.tmechworks.common.blocks.tileentity.DrawbridgeTileEntity;
import slimeknights.tmechworks.common.inventory.DrawbridgeContainer;
Expand All @@ -24,8 +29,10 @@
import java.util.List;

public class DrawbridgeScreen extends ContainerScreen<DrawbridgeContainer> {
public static final ResourceLocation SCREEN_LOCATION = new ResourceLocation("tmechworks", "textures/gui/drawbridge.png");
public static final ResourceLocation ADVANCED_LOCATION = new ResourceLocation("tmechworks", "textures/gui/drawbridge_advanced.png");
public static final ResourceLocation SCREEN_LOCATION = new ResourceLocation(TMechworks.modId, "textures/gui/drawbridge.png");
public static final ResourceLocation ADVANCED_LOCATION = new ResourceLocation(TMechworks.modId, "textures/gui/drawbridge_advanced.png");

public DisguiseStateWidget disguiseWidget;

public final boolean isAdvanced;
private final int slotCount;
Expand Down Expand Up @@ -58,6 +65,9 @@ protected void init() {
ArrowWidget arrow = new ArrowWidget(aX, aY, width, height, true, this::arrowClicked);
updateSelection(arrow);
addButton(arrow);

disguiseWidget = new DisguiseStateWidget(guiLeft + 198, guiTop + 133, container.getTileEntity());
addButton(disguiseWidget);
}

@Override
Expand All @@ -70,6 +80,16 @@ public void tick() {
if(isAdvanced != te.stats.isAdvanced || slotCount != te.slots.getSizeInventory()) {
PacketHandler.send(PacketDistributor.SERVER.noArg(), new ServerReopenUiPacket(container.getTileEntity().getPos()));
}

// Update disguise state controls
ItemStack disguise = te.getDisguiseBlock();

if (disguise.getItem() instanceof BlockItem) {
BlockState disguiseState = ((BlockItem) disguise.getItem()).getBlock().getDefaultState();
disguiseWidget.setState(DisguiseStates.getForState(disguiseState), te.getDisguiseState());
} else {
disguiseWidget.setState(null, null);
}
}

@Override
Expand All @@ -86,7 +106,13 @@ protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, i

blit(guiLeft, guiTop, 0, 0, xSize, ySize); // Background
blit(guiLeft - 44, guiTop + ySize - 65, 0, 182, 47, 60); // Upgrades cutout

blit(guiLeft + xSize - 3, guiTop + ySize - 37, 52, 182, 29, 32); // Disguise cutout
// 75 182
if(disguiseWidget.getColumnCount() > 0) {
blit(guiLeft + xSize + 22, guiTop + ySize - 37, disguiseWidget.getColumnCount() * 8 + 2, 32, 75, 76, 182, 214);
blit(guiLeft + xSize + 22 + disguiseWidget.getColumnCount() * 8 + 2, guiTop + ySize - 37, 3, 32, 78, 81, 183, 214);
}

if(!isAdvanced) {
// drawSlicedBox(guiLeft + 34, guiTop + 35, 18, 18, 17, 166); // Disguise slot
Expand Down
Expand Up @@ -7,11 +7,12 @@
import net.minecraft.client.resources.I18n;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.client.gui.GuiUtils;
import slimeknights.tmechworks.TMechworks;

import java.util.Arrays;

public class ArrowWidget extends Widget {
public static final ResourceLocation ARROW_WIDGET = new ResourceLocation("tmechworks", "textures/gui/arrows.png");
public static final ResourceLocation ARROW_WIDGET = new ResourceLocation(TMechworks.modId, "textures/gui/arrows.png");

public static final String[] LABELS_DEFAULT = {
"tmechworks:widget.arrow.down",
Expand Down Expand Up @@ -130,7 +131,6 @@ public void renderButton(int mouseX, int mouseY, float partialTicks) {
blit(arrow.x, arrow.y, indexX, indexY, arrow.subW, arrow.subH);
}

RenderSystem.color4f(1F, 1F, 1F, 1F);
RenderSystem.popMatrix();

if (hoveredArrow == null)
Expand All @@ -141,6 +141,7 @@ public void renderButton(int mouseX, int mouseY, float partialTicks) {
}
}

@Override
public boolean mouseClicked(double p_mouseClicked_1_, double p_mouseClicked_3_, int p_mouseClicked_5_) {
if (hoveredArrow != null) {
this.playDownSound(Minecraft.getInstance().getSoundHandler());
Expand Down

0 comments on commit ece32d9

Please sign in to comment.