Skip to content

Commit

Permalink
Jade plugin (#16)
Browse files Browse the repository at this point in the history
Co-authored-by: TheMCCrazyMan <themccrazyman@mail.ch>
  • Loading branch information
TheMCLoveMan and TheMCCrazyMan authored Feb 26, 2024
1 parent 5167942 commit c4d5364
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 19 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 @@ -5,6 +5,7 @@
"block.usefulmachinery.crusher": "Crusher",
"block.usefulmachinery.electric_smelter": "Electric Smelter",
"block.usefulmachinery.lava_generator": "Lava Generator",
"config.jade.plugin_usefulmachinery.machine": "Machine",
"container.usefulmachinery.coal_generator": "Coal Generator",
"container.usefulmachinery.compactor": "Compactor",
"container.usefulmachinery.crusher": "Crusher",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,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
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
@@ -0,0 +1,119 @@
package net.themcbrothers.usefulmachinery.compat.jade;

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;

/**
* Adds the machine's progress and tier information
*/
public enum MachineProvider implements IBlockComponentProvider, IServerDataProvider<BlockAccessor> {
INSTANCE;

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

@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);

for (int i = 0; i < machineItems.size(); ++i) {
inventory.set(i, ItemStack.of(machineItems.getCompound(i)));
}

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)));
} else {
tooltip.append(helper.item(inventory.get(slot)));
}
}

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

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();

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

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

IntStream.range(0, machine.getContainerSize())
.mapToObj(machine::getItem)
.map(stack -> stack.save(new CompoundTag()))
.forEach(items::add);

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
public ResourceLocation getUid() {
return UID;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.themcbrothers.usefulmachinery.compat.jade;

import net.themcbrothers.usefulmachinery.block.AbstractMachineBlock;
import net.themcbrothers.usefulmachinery.block.entity.AbstractMachineBlockEntity;
import snownee.jade.api.IWailaClientRegistration;
import snownee.jade.api.IWailaCommonRegistration;
import snownee.jade.api.IWailaPlugin;
Expand All @@ -12,9 +14,12 @@
public class MachineryJadePlugin implements IWailaPlugin {
@Override
public void register(IWailaCommonRegistration registration) {
registration.registerBlockDataProvider(MachineProvider.INSTANCE, AbstractMachineBlockEntity.class);
}

@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,41 @@
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;

/**
* Provider that removes tooltip components from our machines
*/
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 @@ -59,6 +59,9 @@ protected void addTranslations() {
this.add("jei.usefulmachinery.compacting", "Compacting");
this.add("jei.usefulmachinery.fuel", "Fuel for RF");

// Jade
this.add("config.jade.plugin_usefulmachinery.machine", "Machine");

// Misc
this.add("misc.usefulmachinery.energy", "%s FE");
this.add("misc.usefulmachinery.energyWithMax", "%s / %s FE");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent;
import net.neoforged.neoforge.energy.IEnergyStorage;
import net.neoforged.neoforge.items.wrapper.InvWrapper;
import net.neoforged.neoforge.items.wrapper.SidedInvWrapper;
import net.themcbrothers.lib.energy.EnergyContainerItem;
import net.themcbrothers.lib.util.Version;
Expand Down Expand Up @@ -52,11 +53,11 @@ private void enqueueIMC(final InterModEnqueueEvent event) {

private void capabilities(final RegisterCapabilitiesEvent event) {
// Items
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, COAL_GENERATOR.get(), SidedInvWrapper::new);
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, COMPACTOR.get(), SidedInvWrapper::new);
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, CRUSHER.get(), SidedInvWrapper::new);
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, ELECTRIC_SMELTER.get(), SidedInvWrapper::new);
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, LAVA_GENERATOR.get(), SidedInvWrapper::new);
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, COAL_GENERATOR.get(), (sidedContainer, side) -> side == null ? new InvWrapper(sidedContainer) : new SidedInvWrapper(sidedContainer, side));
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, COMPACTOR.get(), (sidedContainer, side) -> side == null ? new InvWrapper(sidedContainer) : new SidedInvWrapper(sidedContainer, side));
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, CRUSHER.get(), (sidedContainer, side) -> side == null ? new InvWrapper(sidedContainer) : new SidedInvWrapper(sidedContainer, side));
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, ELECTRIC_SMELTER.get(), (sidedContainer, side) -> side == null ? new InvWrapper(sidedContainer) : new SidedInvWrapper(sidedContainer, side));
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, LAVA_GENERATOR.get(), (sidedContainer, side) -> side == null ? new InvWrapper(sidedContainer) : new SidedInvWrapper(sidedContainer, side));

// Energy
event.registerBlockEntity(Capabilities.EnergyStorage.BLOCK, COAL_GENERATOR.get(), (machine, context) -> machine.getEnergyStorage());
Expand Down

0 comments on commit c4d5364

Please sign in to comment.