Skip to content

Commit

Permalink
finish up jade plugin
Browse files Browse the repository at this point in the history
Co-authored-by: TheMCCrazyMan <themccrazyman@mail.ch>
  • Loading branch information
TheMCLoveMan and TheMCCrazyMan committed Feb 26, 2024
1 parent 7592682 commit cf36092
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 40 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ minecraft_version_range=[1.20.4,1.21)

# NeoForge
neo_version=20.4.167
neo_version_range=[20.4,)
neo_version_range=[20.4.167,)
loader_version_range=[2,)

# Mappings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.apache.commons.lang3.ArrayUtils;

import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.function.Function;

public abstract class AbstractMachineBlockEntity extends BlockEntity implements WorldlyContainer, MenuProvider {
Expand Down Expand Up @@ -158,9 +157,9 @@ public void clearContent() {
this.stacks.clear();
}

protected abstract int[] getInputSlots();
public abstract int[] getInputSlots();

protected abstract int[] getOutputSlots();
public abstract int[] getOutputSlots();

protected abstract boolean canRun();

Expand Down Expand Up @@ -350,16 +349,4 @@ public void setRedstoneMode(RedstoneMode redstoneMode) {
public Container getUpgradeContainer() {
return this.upgradeContainer;
}

public int[] getInputsForJade() {
return this.getInputSlots();
}

public int[] getOutputsForJade() {
return this.getOutputSlots();
}

public boolean showJadeProgress() {
return Arrays.stream(ArrayUtils.addAll(this.getInputSlots(), this.getOutputSlots())).mapToObj(this::getItem).anyMatch(itemStack -> !itemStack.isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ public CoalGeneratorBlockEntity(BlockPos pos, BlockState state) {
}

@Override
protected int[] getInputSlots() {
public int[] getInputSlots() {
return new int[]{0};
}

@Override
protected int[] getOutputSlots() {
public int[] getOutputSlots() {
return new int[0];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ public CompactorBlockEntity(BlockPos pos, BlockState state) {
}

@Override
protected int[] getInputSlots() {
public int[] getInputSlots() {
return new int[]{0};
}

@Override
protected int[] getOutputSlots() {
public int[] getOutputSlots() {
return new int[]{1};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public CrusherBlockEntity(BlockPos pos, BlockState state) {
}

@Override
protected int[] getInputSlots() {
public int[] getInputSlots() {
return new int[]{0};
}

@Override
protected int[] getOutputSlots() {
public int[] getOutputSlots() {
return new int[]{1, 2};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public ElectricSmelterBlockEntity(BlockPos pos, BlockState state) {
}

@Override
protected int[] getInputSlots() {
public int[] getInputSlots() {
return new int[]{0};
}

@Override
protected int[] getOutputSlots() {
public int[] getOutputSlots() {
return new int[]{1};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import net.neoforged.neoforge.fluids.FluidUtil;
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
import net.neoforged.neoforge.fluids.capability.templates.FluidTank;
import net.themcbrothers.usefulmachinery.block.AbstractMachineBlock;
import net.themcbrothers.usefulmachinery.core.MachineryBlockEntities;
import net.themcbrothers.usefulmachinery.core.MachineryItems;
import net.themcbrothers.usefulmachinery.machine.RedstoneMode;
Expand Down Expand Up @@ -76,12 +75,12 @@ public LavaGeneratorBlockEntity(BlockPos pos, BlockState state) {
}

@Override
protected int[] getInputSlots() {
public int[] getInputSlots() {
return new int[]{0};
}

@Override
protected int[] getOutputSlots() {
public int[] getOutputSlots() {
return new int[0];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec2;
import net.themcbrothers.usefulmachinery.UsefulMachinery;
import net.themcbrothers.usefulmachinery.block.entity.AbstractMachineBlockEntity;
import net.themcbrothers.usefulmachinery.core.MachineryItems;
import net.themcbrothers.usefulmachinery.machine.MachineTier;
import snownee.jade.api.BlockAccessor;
import snownee.jade.api.IBlockComponentProvider;
import snownee.jade.api.IServerDataProvider;
import snownee.jade.api.ITooltip;
import snownee.jade.api.config.IPluginConfig;
import snownee.jade.api.ui.IElementHelper;

import java.util.Arrays;
import java.util.Optional;
import java.util.stream.IntStream;

public enum MachineProvider implements IBlockComponentProvider, IServerDataProvider<BlockAccessor> {
INSTANCE;

Expand All @@ -23,6 +30,8 @@ public enum MachineProvider implements IBlockComponentProvider, IServerDataProvi
@Override
public void appendTooltip(ITooltip tooltip, BlockAccessor accessor, IPluginConfig pluginConfig) {
CompoundTag data = accessor.getServerData();
IElementHelper helper = IElementHelper.get();

if (data.contains("progress")) {
ListTag machineItems = data.getList("machine", ListTag.TAG_COMPOUND);
NonNullList<ItemStack> inventory = NonNullList.withSize(data.getInt("size"), ItemStack.EMPTY);
Expand All @@ -31,12 +40,11 @@ public void appendTooltip(ITooltip tooltip, BlockAccessor accessor, IPluginConfi
inventory.set(i, ItemStack.of(machineItems.getCompound(i)));
}

IElementHelper helper = IElementHelper.get();

int progress = data.getInt("progress");
int total = data.getInt("total");

int count = 0;

for (int slot : data.getIntArray("inputs")) {
if (count++ == 0) {
tooltip.add(helper.item(inventory.get(slot)));
Expand All @@ -45,36 +53,61 @@ public void appendTooltip(ITooltip tooltip, BlockAccessor accessor, IPluginConfi
}
}

// progress bar
// Progress Bar
tooltip.append(helper.spacer(4, 0));
tooltip.append(helper.progress((float) progress / (float) total).translate(new Vec2(-2.0F, 0.0F)));
tooltip.append(helper.progress((float) progress / (float) total)
.translate(new Vec2(-2.0F, 0.0F)));

for (int slot : data.getIntArray("outputs")) {
tooltip.append(helper.item(inventory.get(slot)));
}
Arrays.stream(data.getIntArray("outputs"))
.mapToObj(inventory::get)
.map(helper::item)
.forEach(tooltip::append);
}

MachineTier tier = MachineTier.byOrdinal(data.getInt("tier"));

if (accessor.getPlayer().isShiftKeyDown() && tier.ordinal() > 0) {
CompoundTag tierTag = new CompoundTag();
tierTag.putInt("Tier", tier.ordinal());

// Tier
tooltip.add(helper.smallItem(new ItemStack(MachineryItems.TIER_UPGRADE.get(), 1, Optional.of(tierTag))));
tooltip.append(helper.spacer(4, 0));
tooltip.append(helper.text(Component.literal(tier.getSerializedName())));
}

}

@Override
public void appendServerData(CompoundTag data, BlockAccessor accessor) {
AbstractMachineBlockEntity machine = (AbstractMachineBlockEntity) accessor.getBlockEntity();
if (machine.showJadeProgress()) {

boolean showJadeProgress = Arrays.stream(machine.getSlotsForFace(null))
.mapToObj(machine::getItem)
.anyMatch(itemStack -> !itemStack.isEmpty());

if (showJadeProgress) {
ListTag items = new ListTag();

for (int i = 0; i < machine.getContainerSize(); ++i) {
items.add(machine.getItem(i).save(new CompoundTag()));
}
IntStream.range(0, machine.getContainerSize())
.mapToObj(machine::getItem)
.map(stack -> stack.save(new CompoundTag()))
.forEach(items::add);

data.putIntArray("inputs", machine.getInputsForJade());
data.putIntArray("outputs", machine.getOutputsForJade());
data.putIntArray("inputs", machine.getInputSlots());
data.putIntArray("outputs", machine.getOutputSlots());

data.putInt("size", machine.getContainerSize());
data.put("machine", items);

CompoundTag furnaceTag = machine.saveWithoutMetadata();

data.putInt("progress", furnaceTag.getInt("ProcessTime"));
data.putInt("total", furnaceTag.getInt("ProcessTimeTotal"));

}

data.putInt("tier", machine.getMachineTier(accessor.getBlockState()).ordinal());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ public void register(IWailaCommonRegistration registration) {
@Override
public void registerClient(IWailaClientRegistration registration) {
registration.registerBlockComponent(MachineProvider.INSTANCE, AbstractMachineBlock.class);
registration.registerBlockComponent(RemoveVanillaStuffProvider.INSTANCE, AbstractMachineBlock.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.themcbrothers.usefulmachinery.compat.jade;

import net.minecraft.resources.ResourceLocation;
import net.themcbrothers.usefulmachinery.UsefulMachinery;
import net.themcbrothers.usefulmachinery.block.entity.AbstractMachineBlockEntity;
import snownee.jade.api.BlockAccessor;
import snownee.jade.api.IBlockComponentProvider;
import snownee.jade.api.ITooltip;
import snownee.jade.api.Identifiers;
import snownee.jade.api.config.IPluginConfig;

public enum RemoveVanillaStuffProvider implements IBlockComponentProvider {
INSTANCE;

private static final ResourceLocation UID = UsefulMachinery.rl("remove_vanilla_stuff");

@Override
public void appendTooltip(ITooltip tooltip, BlockAccessor accessor, IPluginConfig pluginConfig) {
if (accessor.getBlockEntity() instanceof AbstractMachineBlockEntity) {
tooltip.remove(Identifiers.UNIVERSAL_ITEM_STORAGE);
}
}

@Override
public ResourceLocation getUid() {
return UID;
}

@Override
public int getDefaultPriority() {
return Integer.MAX_VALUE;
}

@Override
public boolean isRequired() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ protected void addTranslations() {

// Jade
this.add("config.jade.plugin_usefulmachinery.machine", "Machine");
this.add("config.jade.plugin_usefulmachinery.remove_vanilla_stuff", "Remove Vanilla Stuff");

// Misc
this.add("misc.usefulmachinery.energy", "%s FE");
Expand Down

0 comments on commit cf36092

Please sign in to comment.