Skip to content

Commit

Permalink
Added pull mode for Energy Distributor module
Browse files Browse the repository at this point in the history
  • Loading branch information
desht committed Dec 6, 2024
1 parent a2f8d0b commit 716d356
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{
"parent": "minecraft:item/generated",
"overrides": [
{
"model": "modularrouters:item/energy_distributor_module_pull",
"predicate": {
"modularrouters:mode": 0.5
}
}
],
"textures": {
"layer0": "modularrouters:item/module/module_layer0",
"layer1": "modularrouters:item/module/module_layer1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "modularrouters:item/module/module_layer0",
"layer1": "modularrouters:item/module/module_layer1",
"layer2": "modularrouters:item/module/energy_distributor_module_pull"
}
}
17 changes: 11 additions & 6 deletions src/main/java/me/desht/modularrouters/client/ClientSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import net.neoforged.neoforge.common.NeoForge;
import org.lwjgl.glfw.GLFW;

import java.util.List;

import static me.desht.modularrouters.util.MiscUtil.RL;

@EventBusSubscriber(modid = ModularRouters.MODID, value = Dist.CLIENT, bus = EventBusSubscriber.Bus.MOD)
Expand Down Expand Up @@ -81,6 +83,7 @@ public static void registerScreens(RegisterMenuScreensEvent event) {
event.register(ModMenuTypes.ACTIVATOR_MENU.get(), ActivatorModuleScreen::new);
event.register(ModMenuTypes.BREAKER_MENU.get(), BreakerModuleScreen::new);
event.register(ModMenuTypes.DETECTOR_MENU.get(), DetectorModuleScreen::new);
event.register(ModMenuTypes.ENERGY_DISTRIBUTOR_MENU.get(), EnergyDistributorModuleScreen::new);
event.register(ModMenuTypes.DISTRIBUTOR_MENU.get(), DistributorModuleScreen::new);
event.register(ModMenuTypes.EXTRUDER2_MENU.get(), ExtruderModule2Screen::new);
event.register(ModMenuTypes.FLINGER_MENU.get(), FlingerModuleScreen::new);
Expand All @@ -94,11 +97,13 @@ public static void registerScreens(RegisterMenuScreensEvent event) {
}

private static void registerItemModelOverrides() {
ItemProperties.register(ModItems.DISTRIBUTOR_MODULE.get(), RL("mode"), (stack, world, entity, n) -> {
if (entity != null) {
return stack.getOrDefault(ModDataComponents.DISTRIBUTOR_SETTINGS, DistributorSettings.DEFAULT).isPulling() ? 1f : 0f;
}
return 0f;
});
for (var ro : List.of(ModItems.DISTRIBUTOR_MODULE, ModItems.ENERGY_DISTRIBUTOR_MODULE)) {
ItemProperties.register(ro.get(), RL("mode"), (stack, world, entity, n) -> {
if (entity != null) {
return stack.getOrDefault(ModDataComponents.DISTRIBUTOR_SETTINGS, DistributorSettings.DEFAULT).isPulling() ? 1f : 0f;
}
return 0f;
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.desht.modularrouters.client.gui.module;

import me.desht.modularrouters.client.gui.ISendToServer;
import me.desht.modularrouters.client.gui.widgets.button.ItemStackButton;
import me.desht.modularrouters.client.gui.widgets.button.TexturedCyclerButton;
import me.desht.modularrouters.client.gui.widgets.button.TexturedToggleButton;
Expand Down Expand Up @@ -38,9 +39,9 @@ public void init() {

CompiledDistributorModule cdm = new CompiledDistributorModule(null, moduleItemStack);

addRenderableWidget(new TooltipButton(leftPos + 127, topPos + 23));
addRenderableWidget(sb = new StrategyButton(leftPos + 147, topPos + 23, 16, 16, cdm.getDistributionStrategy()));
addRenderableWidget(db = new DirectionButton(leftPos + 147, topPos + 43, cdm.isPulling()));
addRenderableWidget(new TooltipButton(leftPos + 127, topPos + 23, ModItems.DISTRIBUTOR_MODULE.toStack()));
addRenderableWidget(sb = new StrategyButton(leftPos + 147, topPos + 23, 16, 16, cdm.getDistributionStrategy(), this));
addRenderableWidget(db = new DirectionButton(leftPos + 147, topPos + 43, cdm.isPulling(), this));

getMouseOverHelp().addHelpRegion(leftPos + 125, topPos + 21, leftPos + 165, topPos + 41,
xlate("modularrouters.guiText.popup.distributor.strategy").withStyle(ChatFormatting.YELLOW));
Expand All @@ -65,9 +66,9 @@ protected void renderBg(GuiGraphics graphics, float partialTicks, int mouseX, in
graphics.renderItem(ROUTER_STACK, leftPos + 127, topPos + 43);
}

private class StrategyButton extends TexturedCyclerButton<DistributionStrategy> {
StrategyButton(int x, int y, int width, int height, DistributionStrategy initialVal) {
super(x, y, width, height, initialVal, DistributorModuleScreen.this);
static class StrategyButton extends TexturedCyclerButton<DistributionStrategy> {
StrategyButton(int x, int y, int width, int height, DistributionStrategy initialVal, ISendToServer sendToServer) {
super(x, y, width, height, initialVal, sendToServer);
}

@Override
Expand All @@ -76,12 +77,12 @@ protected XYPoint getTextureXY() {
}
}

private class DirectionButton extends TexturedToggleButton {
static class DirectionButton extends TexturedToggleButton {
private static final XYPoint TEXTURE_XY = new XYPoint(176, 16);
private static final XYPoint TEXTURE_XY_TOGGLED = new XYPoint(160, 16);

public DirectionButton(int x, int y, boolean initialVal) {
super(x, y, 16, 16, initialVal, DistributorModuleScreen.this);
public DirectionButton(int x, int y, boolean initialVal, ISendToServer sender) {
super(x, y, 16, 16, initialVal, sender);

setTooltips(xlate("modularrouters.itemText.transfer_direction.from_router"),xlate("modularrouters.itemText.transfer_direction.to_router"));
}
Expand All @@ -92,11 +93,11 @@ protected XYPoint getTextureXY() {
}
}

private static class TooltipButton extends ItemStackButton {
static class TooltipButton extends ItemStackButton {
private static final XYPoint TEXTURE_XY = new XYPoint(176, 16);

TooltipButton(int x, int y) {
super(x, y, 16, 16, new ItemStack(ModItems.DISTRIBUTOR_MODULE.get()), true, p -> {});
TooltipButton(int x, int y, ItemStack stack) {
super(x, y, 16, 16, stack, true, p -> {});
setTooltip(Tooltip.create(xlate("modularrouters.guiText.tooltip.distributor.strategy")));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package me.desht.modularrouters.client.gui.module;

import me.desht.modularrouters.container.ModuleMenu;
import me.desht.modularrouters.core.ModBlocks;
import me.desht.modularrouters.core.ModDataComponents;
import me.desht.modularrouters.logic.compiled.CompiledDistributorModule;
import me.desht.modularrouters.logic.compiled.CompiledDistributorModule.DistributionStrategy;
import me.desht.modularrouters.logic.compiled.CompiledEnergyDistributorModule;
import me.desht.modularrouters.logic.settings.TransferDirection;
import net.minecraft.ChatFormatting;
import net.minecraft.Util;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;

import static me.desht.modularrouters.client.util.ClientUtil.xlate;

public class EnergyDistributorModuleScreen extends ModuleScreen {
private static final ItemStack ROUTER_STACK = new ItemStack(ModBlocks.MODULAR_ROUTER.get());

private DistributorModuleScreen.DirectionButton db;

public EnergyDistributorModuleScreen(ModuleMenu container, Inventory inv, Component displayText) {
super(container, inv, displayText);
}

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

CompiledEnergyDistributorModule cdm = new CompiledEnergyDistributorModule(null, moduleItemStack);

addRenderableWidget(db = new DistributorModuleScreen.DirectionButton(leftPos + 147, topPos + 43, cdm.isPulling(), this));

getMouseOverHelp().addHelpRegion(leftPos + 125, topPos + 41, leftPos + 165, topPos + 61,
xlate("modularrouters.guiText.popup.distributor.direction").withStyle(ChatFormatting.YELLOW));
}

@Override
protected ItemStack buildModifiedItemStack() {
return Util.make(super.buildModifiedItemStack(), stack ->
stack.set(ModDataComponents.DISTRIBUTOR_SETTINGS, new CompiledDistributorModule.DistributorSettings(
DistributionStrategy.ROUND_ROBIN,
db.isToggled() ? TransferDirection.TO_ROUTER : TransferDirection.FROM_ROUTER
))
);
}

@Override
protected void renderBg(GuiGraphics graphics, float partialTicks, int mouseX, int mouseY) {
super.renderBg(graphics, partialTicks, mouseX, mouseY);

graphics.renderItem(ROUTER_STACK, leftPos + 127, topPos + 43);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public static ModuleMenu createDistributorMenu(int windowId, Inventory inv, Frie
return new ModuleMenu(ModMenuTypes.DISTRIBUTOR_MENU.get(), windowId, inv, extra);
}

public static ModuleMenu createEnergyDistributorMenu(int windowId, Inventory inv, FriendlyByteBuf extra) {
return new ModuleMenu(ModMenuTypes.ENERGY_DISTRIBUTOR_MENU.get(), windowId, inv, extra);
}

public static Extruder2ModuleMenu createExtruder2Menu(int windowId, Inventory inv, FriendlyByteBuf extra) {
return new Extruder2ModuleMenu(windowId, inv, extra);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/me/desht/modularrouters/core/ModMenuTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class ModMenuTypes {
= register("module_detector", ModuleMenuFactories::createDetectorMenu);
public static final Supplier<MenuType<ModuleMenu>> DISTRIBUTOR_MENU
= register("module_distributor", ModuleMenuFactories::createDistributorMenu);
public static final Supplier<MenuType<ModuleMenu>> ENERGY_DISTRIBUTOR_MENU
= register("moduleenergy_distributor", ModuleMenuFactories::createEnergyDistributorMenu);
public static final Supplier<MenuType<Extruder2ModuleMenu>> EXTRUDER2_MENU
= register("module_extruder_2", ModuleMenuFactories::createExtruder2Menu);
public static final Supplier<MenuType<ModuleMenu>> FLINGER_MENU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ protected void registerModels() {
String name = registryObject.getId().getPath();
switch (registryObject.get()) {
case ModuleItem moduleItem -> {
if (moduleItem == ModItems.DISTRIBUTOR_MODULE.get()) {
if (moduleItem == ModItems.DISTRIBUTOR_MODULE.get() || moduleItem == ModItems.ENERGY_DISTRIBUTOR_MODULE.get()) {
// special case; distributor module has a model override based on its mode
ModelFile distributorPull = simpleItemVariant(ModItems.DISTRIBUTOR_MODULE, "_pull",
ModelFile pullVariant = simpleItemVariant(registryObject, "_pull",
modid("item/module/module_layer0"),
modid("item/module/module_layer1"),
modid("item/module/distributor_module_pull"));
simpleItem(ModItems.DISTRIBUTOR_MODULE,
modid("item/module/%s_pull", name));
simpleItem(registryObject,
modid("item/module/module_layer0"),
modid("item/module/module_layer1"),
modid("item/module/distributor_module"))
.override().predicate(modLoc("mode"), 0.5f).model(distributorPull);
modid("item/module/" + name))
.override().predicate(modLoc("mode"), 0.5f).model(pullVariant);
} else {
simpleItem(registryObject,
modid("item/module/module_layer0"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,52 @@
package me.desht.modularrouters.item.module;

import me.desht.modularrouters.client.render.area.IPositionProvider;
import me.desht.modularrouters.client.util.ClientUtil;
import me.desht.modularrouters.client.util.TintColor;
import me.desht.modularrouters.config.ConfigHolder;
import me.desht.modularrouters.container.ModuleMenu;
import me.desht.modularrouters.core.ModDataComponents;
import me.desht.modularrouters.core.ModItems;
import me.desht.modularrouters.core.ModMenuTypes;
import me.desht.modularrouters.logic.compiled.CompiledEnergyDistributorModule;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.neoforged.neoforge.capabilities.Capabilities;

import java.util.List;

import static me.desht.modularrouters.logic.compiled.CompiledDistributorModule.DistributorSettings;

public class EnergyDistributorModule extends ModuleItem implements IRangedModule, IPositionProvider, ITargetedModule {
private static final TintColor TINT_COLOR = new TintColor(79, 9, 90);

public EnergyDistributorModule() {
super(ModItems.moduleProps(),
super(ModItems.moduleProps()
.component(ModDataComponents.DISTRIBUTOR_SETTINGS, DistributorSettings.DEFAULT),
CompiledEnergyDistributorModule::new);
}

@Override
protected void addSettingsInformation(ItemStack stack, List<Component> list) {
super.addSettingsInformation(stack, list);

DistributorSettings settings = stack.getOrDefault(ModDataComponents.DISTRIBUTOR_SETTINGS, DistributorSettings.DEFAULT);
list.add(ClientUtil.xlate(settings.direction().getTranslationKey()).withStyle(ChatFormatting.YELLOW));
}

@Override
public TintColor getItemTint() {
return TINT_COLOR;
}

@Override
public MenuType<? extends ModuleMenu> getMenuType() {
return ModMenuTypes.ENERGY_DISTRIBUTOR_MENU.get();
}

@Override
public int getEnergyCost(ItemStack stack) {
return ConfigHolder.common.energyCosts.energydistributorModuleEnergyCost.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import java.util.Set;

public class CompiledDistributorModule extends CompiledSenderModule2 {
private final DistributorSettings settings;
private int nextTarget;
protected final DistributorSettings settings;
protected int nextTarget;

public CompiledDistributorModule(ModularRouterBlockEntity router, ItemStack stack) {
super(router, stack);
Expand All @@ -42,7 +42,7 @@ public boolean execute(@Nonnull ModularRouterBlockEntity router) {
return isPulling() ? executePull(router) : super.execute(router);
}

private boolean executePull(ModularRouterBlockEntity router) {
protected boolean executePull(ModularRouterBlockEntity router) {
if (router.isBufferFull()) return false;

ModuleTarget tgt = getEffectiveTarget(router);
Expand All @@ -59,6 +59,10 @@ private boolean executePull(ModularRouterBlockEntity router) {
}).orElse(false);
}

protected boolean executePush(ModularRouterBlockEntity router) {
return super.execute(router);
}

public boolean isPulling() {
return settings.direction == TransferDirection.TO_ROUTER;
}
Expand Down
Loading

0 comments on commit 716d356

Please sign in to comment.