diff --git a/src/generated/resources/assets/modularrouters/models/item/energy_distributor_module.json b/src/generated/resources/assets/modularrouters/models/item/energy_distributor_module.json index fb447b5e..e3c61174 100644 --- a/src/generated/resources/assets/modularrouters/models/item/energy_distributor_module.json +++ b/src/generated/resources/assets/modularrouters/models/item/energy_distributor_module.json @@ -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", diff --git a/src/generated/resources/assets/modularrouters/models/item/energy_distributor_module_pull.json b/src/generated/resources/assets/modularrouters/models/item/energy_distributor_module_pull.json new file mode 100644 index 00000000..01205bf8 --- /dev/null +++ b/src/generated/resources/assets/modularrouters/models/item/energy_distributor_module_pull.json @@ -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" + } +} \ No newline at end of file diff --git a/src/main/java/me/desht/modularrouters/client/ClientSetup.java b/src/main/java/me/desht/modularrouters/client/ClientSetup.java index dbdcdf81..b5e79041 100644 --- a/src/main/java/me/desht/modularrouters/client/ClientSetup.java +++ b/src/main/java/me/desht/modularrouters/client/ClientSetup.java @@ -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) @@ -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); @@ -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; + }); + } } } diff --git a/src/main/java/me/desht/modularrouters/client/gui/module/DistributorModuleScreen.java b/src/main/java/me/desht/modularrouters/client/gui/module/DistributorModuleScreen.java index 34020cbf..df8d898b 100644 --- a/src/main/java/me/desht/modularrouters/client/gui/module/DistributorModuleScreen.java +++ b/src/main/java/me/desht/modularrouters/client/gui/module/DistributorModuleScreen.java @@ -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; @@ -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)); @@ -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 { - StrategyButton(int x, int y, int width, int height, DistributionStrategy initialVal) { - super(x, y, width, height, initialVal, DistributorModuleScreen.this); + static class StrategyButton extends TexturedCyclerButton { + StrategyButton(int x, int y, int width, int height, DistributionStrategy initialVal, ISendToServer sendToServer) { + super(x, y, width, height, initialVal, sendToServer); } @Override @@ -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")); } @@ -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"))); } diff --git a/src/main/java/me/desht/modularrouters/client/gui/module/EnergyDistributorModuleScreen.java b/src/main/java/me/desht/modularrouters/client/gui/module/EnergyDistributorModuleScreen.java new file mode 100644 index 00000000..6abf4386 --- /dev/null +++ b/src/main/java/me/desht/modularrouters/client/gui/module/EnergyDistributorModuleScreen.java @@ -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); + } +} diff --git a/src/main/java/me/desht/modularrouters/container/ModuleMenuFactories.java b/src/main/java/me/desht/modularrouters/container/ModuleMenuFactories.java index d3038506..756c51e4 100644 --- a/src/main/java/me/desht/modularrouters/container/ModuleMenuFactories.java +++ b/src/main/java/me/desht/modularrouters/container/ModuleMenuFactories.java @@ -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); } diff --git a/src/main/java/me/desht/modularrouters/core/ModMenuTypes.java b/src/main/java/me/desht/modularrouters/core/ModMenuTypes.java index 58dddf97..de4dca03 100644 --- a/src/main/java/me/desht/modularrouters/core/ModMenuTypes.java +++ b/src/main/java/me/desht/modularrouters/core/ModMenuTypes.java @@ -27,6 +27,8 @@ public class ModMenuTypes { = register("module_detector", ModuleMenuFactories::createDetectorMenu); public static final Supplier> DISTRIBUTOR_MENU = register("module_distributor", ModuleMenuFactories::createDistributorMenu); + public static final Supplier> ENERGY_DISTRIBUTOR_MENU + = register("moduleenergy_distributor", ModuleMenuFactories::createEnergyDistributorMenu); public static final Supplier> EXTRUDER2_MENU = register("module_extruder_2", ModuleMenuFactories::createExtruder2Menu); public static final Supplier> FLINGER_MENU diff --git a/src/main/java/me/desht/modularrouters/datagen/ModItemModelProvider.java b/src/main/java/me/desht/modularrouters/datagen/ModItemModelProvider.java index 1068a4e1..3b0bbb41 100644 --- a/src/main/java/me/desht/modularrouters/datagen/ModItemModelProvider.java +++ b/src/main/java/me/desht/modularrouters/datagen/ModItemModelProvider.java @@ -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"), diff --git a/src/main/java/me/desht/modularrouters/item/module/EnergyDistributorModule.java b/src/main/java/me/desht/modularrouters/item/module/EnergyDistributorModule.java index ac238f89..100fe410 100644 --- a/src/main/java/me/desht/modularrouters/item/module/EnergyDistributorModule.java +++ b/src/main/java/me/desht/modularrouters/item/module/EnergyDistributorModule.java @@ -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 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 getMenuType() { + return ModMenuTypes.ENERGY_DISTRIBUTOR_MENU.get(); + } + @Override public int getEnergyCost(ItemStack stack) { return ConfigHolder.common.energyCosts.energydistributorModuleEnergyCost.get(); diff --git a/src/main/java/me/desht/modularrouters/logic/compiled/CompiledDistributorModule.java b/src/main/java/me/desht/modularrouters/logic/compiled/CompiledDistributorModule.java index 0fc7ca4e..250ac3bf 100644 --- a/src/main/java/me/desht/modularrouters/logic/compiled/CompiledDistributorModule.java +++ b/src/main/java/me/desht/modularrouters/logic/compiled/CompiledDistributorModule.java @@ -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); @@ -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); @@ -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; } diff --git a/src/main/java/me/desht/modularrouters/logic/compiled/CompiledEnergyDistributorModule.java b/src/main/java/me/desht/modularrouters/logic/compiled/CompiledEnergyDistributorModule.java index 9598b295..390b6813 100644 --- a/src/main/java/me/desht/modularrouters/logic/compiled/CompiledEnergyDistributorModule.java +++ b/src/main/java/me/desht/modularrouters/logic/compiled/CompiledEnergyDistributorModule.java @@ -1,6 +1,7 @@ package me.desht.modularrouters.logic.compiled; import me.desht.modularrouters.block.tile.ModularRouterBlockEntity; +import me.desht.modularrouters.core.ModDataComponents; import me.desht.modularrouters.core.ModItems; import me.desht.modularrouters.logic.ModuleTarget; import me.desht.modularrouters.util.BeamData; @@ -12,34 +13,65 @@ import java.util.List; public class CompiledEnergyDistributorModule extends CompiledModule { + protected final CompiledDistributorModule.DistributorSettings settings; + public CompiledEnergyDistributorModule(@Nullable ModularRouterBlockEntity router, ItemStack stack) { super(router, stack); + + settings = stack.getOrDefault(ModDataComponents.DISTRIBUTOR_SETTINGS, CompiledDistributorModule.DistributorSettings.DEFAULT); } @Override public boolean execute(@Nonnull ModularRouterBlockEntity router) { - List inRange = getTargets(); - if (inRange.isEmpty()) return false; + if (!getTargets().isEmpty()) { + IEnergyStorage storage = router.getEnergyStorage(); + if (storage != null) { + boolean doBeam = router.getUpgradeCount(ModItems.MUFFLER_UPGRADE.get()) < 2; + return settings.isPulling() ? + pullEnergy(router, storage, getTargets(), doBeam) > 0 : + sendEnergy(router, storage, getTargets(), doBeam) > 0; + } + } + return false; + } + private int sendEnergy(@Nonnull ModularRouterBlockEntity router, IEnergyStorage storage, List targets, boolean doBeam) { int total = 0; + int toSend = storage.getEnergyStored() / targets.size(); - IEnergyStorage storage = router.getEnergyStorage(); - if (storage != null) { - int toSend = storage.getEnergyStored() / inRange.size(); - boolean doBeam = router.getUpgradeCount(ModItems.MUFFLER_UPGRADE.get()) < 2; - for (ModuleTarget target : inRange) { - total += target.getEnergyHandler().map(handler -> { - int toExtract = storage.extractEnergy(toSend, true); - int sent = handler.receiveEnergy(toExtract, false); - storage.extractEnergy(sent, false); - if (sent > 0 && doBeam) { - router.addItemBeam(new BeamData.Builder(router, target.gPos.pos(), 0xE0404040).build()); - } - return sent; - }).orElse(0); - } + for (ModuleTarget target : targets) { + total += target.getEnergyHandler().map(handler -> { + int toExtract = storage.extractEnergy(toSend, true); + int sent = handler.receiveEnergy(toExtract, false); + storage.extractEnergy(sent, false); + if (sent > 0 && doBeam) { + router.addItemBeam(new BeamData.Builder(router, target.gPos.pos(), 0xE0FF4040).build()); + } + return sent; + }).orElse(0); } + return total; + } + + private int pullEnergy(@Nonnull ModularRouterBlockEntity router, IEnergyStorage storage, List targets, boolean doBeam) { + int total = 0; + int toPull = (storage.getMaxEnergyStored() - storage.getEnergyStored()) / targets.size(); + + for (ModuleTarget target : targets) { + total += target.getEnergyHandler().map(handler -> { + int toExtract = handler.extractEnergy(toPull, true); + int received = storage.receiveEnergy(toExtract, false); + handler.extractEnergy(received, false); + if (received > 0 && doBeam) { + router.addItemBeam(new BeamData.Builder(router, target.gPos.pos(), 0xE0C040A0).reversed(true).build()); + } + return received; + }).orElse(0); + } + return total; + } - return total > 0; + public boolean isPulling() { + return settings.isPulling(); } } diff --git a/src/main/resources/assets/modularrouters/textures/item/module/energy_distributor_module_pull.png b/src/main/resources/assets/modularrouters/textures/item/module/energy_distributor_module_pull.png new file mode 100644 index 00000000..a3c8ba99 Binary files /dev/null and b/src/main/resources/assets/modularrouters/textures/item/module/energy_distributor_module_pull.png differ