Skip to content

Commit 99b6254

Browse files
committed
BlockArrayCache improvements; reduce overhead for committing asynchronous operations; optimize large structure checks (Forge Server only); fix an issue that still checks structures when a blueprint is required for mechanical configuration and there is no blueprint in the controller; provide formatted text for the display of values in the energy input and output hatches.
1 parent 28233cc commit 99b6254

18 files changed

+531
-413
lines changed

src/main/java/github/kasuminova/mmce/common/concurrent/ActionExecutor.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package github.kasuminova.mmce.common.concurrent;
22

33
import hellfirepvp.modularmachinery.ModularMachinery;
4+
import io.netty.util.internal.ThrowableUtil;
45

56
import java.util.concurrent.RecursiveAction;
67
import java.util.concurrent.TimeUnit;
@@ -18,18 +19,23 @@ public ActionExecutor(Action action) {
1819
protected void compute() {
1920
long start = System.nanoTime() / 1000;
2021

21-
RecursiveAction computeAction = new RecursiveAction() {
22+
RecursiveAction computeAction = (RecursiveAction) new RecursiveAction() {
2223
@Override
2324
protected void compute() {
24-
action.doAction();
25+
try {
26+
action.doAction();
27+
} catch (Exception e) {
28+
ModularMachinery.log.warn("An error occurred during asynchronous task execution!");
29+
ModularMachinery.log.warn(ThrowableUtil.stackTraceToString(e));
30+
}
2531
}
26-
};
27-
computeAction.fork();
32+
}.fork();
33+
2834
try {
2935
computeAction.get(50, TimeUnit.MILLISECONDS);
3036
} catch (TimeoutException e) {
3137
ModularMachinery.log.warn("[Modular Machinery] Parallel action execute timeout for 50ms.");
32-
computeAction.cancel(true);
38+
computeAction.cancel(true); // May not work.
3339
} catch (Exception e) {
3440
ModularMachinery.log.warn(e);
3541
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package github.kasuminova.mmce.common.concurrent;
2+
3+
import hellfirepvp.modularmachinery.common.crafting.ActiveMachineRecipe;
4+
import hellfirepvp.modularmachinery.common.crafting.MachineRecipe;
5+
import hellfirepvp.modularmachinery.common.crafting.RecipeRegistry;
6+
import hellfirepvp.modularmachinery.common.crafting.helper.RecipeCraftingContext;
7+
import hellfirepvp.modularmachinery.common.machine.DynamicMachine;
8+
import hellfirepvp.modularmachinery.common.tiles.TileMachineController;
9+
10+
import java.util.concurrent.RecursiveTask;
11+
12+
public class RecipeSearchTask extends RecursiveTask<RecipeCraftingContext> {
13+
private final TileMachineController controller;
14+
private final DynamicMachine currentMachine;
15+
16+
public RecipeSearchTask(TileMachineController controller, DynamicMachine currentMachine) {
17+
this.controller = controller;
18+
this.currentMachine = currentMachine;
19+
}
20+
21+
@Override
22+
protected RecipeCraftingContext compute() {
23+
DynamicMachine foundMachine = controller.getFoundMachine();
24+
if (foundMachine == null) return null;
25+
Iterable<MachineRecipe> availableRecipes = RecipeRegistry.getRecipesFor(foundMachine);
26+
27+
MachineRecipe highestValidity = null;
28+
RecipeCraftingContext.CraftingCheckResult highestValidityResult = null;
29+
float validity = 0F;
30+
31+
for (MachineRecipe recipe : availableRecipes) {
32+
ActiveMachineRecipe activeRecipe = new ActiveMachineRecipe(recipe, controller.getMaxParallelism());
33+
RecipeCraftingContext context = controller.createContext(activeRecipe);
34+
RecipeCraftingContext.CraftingCheckResult result = controller.onCheck(context);
35+
if (result.isSuccess()) {
36+
//并发检查
37+
foundMachine = controller.getFoundMachine();
38+
if (foundMachine == null || !foundMachine.equals(currentMachine))
39+
return null;
40+
41+
controller.resetRecipeSearchRetryCount();
42+
43+
return context;
44+
} else if (highestValidity == null ||
45+
(result.getValidity() >= 0.5F && result.getValidity() > validity)) {
46+
highestValidity = recipe;
47+
highestValidityResult = result;
48+
validity = result.getValidity();
49+
}
50+
}
51+
52+
//并发检查
53+
foundMachine = controller.getFoundMachine();
54+
if (foundMachine == null || !foundMachine.equals(currentMachine))
55+
return null;
56+
57+
if (highestValidity != null) {
58+
controller.setCraftingStatus(TileMachineController.CraftingStatus.failure(highestValidityResult.getFirstErrorMessage("")));
59+
} else {
60+
controller.setCraftingStatus(TileMachineController.CraftingStatus.failure(TileMachineController.Type.NO_RECIPE.getUnlocalizedDescription()));
61+
}
62+
controller.incrementRecipeSearchRetryCount();
63+
64+
return null;
65+
}
66+
67+
public TileMachineController getController() {
68+
return controller;
69+
}
70+
71+
public DynamicMachine getCurrentMachine() {
72+
return currentMachine;
73+
}
74+
}

src/main/java/hellfirepvp/modularmachinery/common/CommonProxy.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
package hellfirepvp.modularmachinery.common;
1010

1111
import github.kasuminova.mmce.common.concurrent.TaskExecutor;
12+
import github.kasuminova.mmce.common.event.EventHandler;
1213
import hellfirepvp.modularmachinery.ModularMachinery;
1314
import hellfirepvp.modularmachinery.common.base.Mods;
1415
import hellfirepvp.modularmachinery.common.container.*;
1516
import hellfirepvp.modularmachinery.common.crafting.IntegrationTypeHelper;
1617
import hellfirepvp.modularmachinery.common.crafting.RecipeRegistry;
1718
import hellfirepvp.modularmachinery.common.crafting.adapter.RecipeAdapterRegistry;
1819
import hellfirepvp.modularmachinery.common.data.ModDataHolder;
19-
import github.kasuminova.mmce.common.event.EventHandler;
2020
import hellfirepvp.modularmachinery.common.integration.ModIntegrationCrafttweaker;
2121
import hellfirepvp.modularmachinery.common.integration.ModIntegrationTOP;
2222
import hellfirepvp.modularmachinery.common.integration.crafttweaker.MachineBuilder;
@@ -32,6 +32,7 @@
3232
import hellfirepvp.modularmachinery.common.tiles.base.TileEnergyHatch;
3333
import hellfirepvp.modularmachinery.common.tiles.base.TileFluidTank;
3434
import hellfirepvp.modularmachinery.common.tiles.base.TileItemBus;
35+
import hellfirepvp.modularmachinery.common.util.BlockArrayCache;
3536
import hellfirepvp.modularmachinery.common.util.FuelItemHelper;
3637
import ink.ikx.mmce.core.AssemblyEventHandler;
3738
import net.minecraft.block.Block;
@@ -49,6 +50,7 @@
4950

5051
import javax.annotation.Nullable;
5152
import java.io.File;
53+
import java.util.concurrent.CompletableFuture;
5254

5355
/**
5456
* This class is part of the Modular Machinery Mod
@@ -75,6 +77,19 @@ public static void loadModData(File configDir) {
7577
}
7678
}
7779

80+
private static void checkThirdPartyServer() {
81+
try {
82+
Class.forName("catserver.server.CatServer");
83+
ModularMachinery.log.warn("//////// Plugin Server Detected! ////////");
84+
ModularMachinery.log.warn("Plugin server will break MMCE's asynchronous functionality.");
85+
ModularMachinery.log.warn("Plugin server compatibility mode is enabled!");
86+
ModularMachinery.log.warn("This will cause asynchronous effects to drop and raise the overhead of the main thread!");
87+
ModularMachinery.pluginServerCompatibleMode = true;
88+
} catch (Exception e) {
89+
ModularMachinery.pluginServerCompatibleMode = false;
90+
}
91+
}
92+
7893
public void preInit() {
7994
creativeTabModularMachinery = new CreativeTabs(ModularMachinery.MODID) {
8095
@Override
@@ -106,11 +121,15 @@ public void init() {
106121

107122
MachineRegistry.registerMachines(MachineRegistry.loadMachines(null));
108123
MachineRegistry.registerMachines(MachineBuilder.WAIT_FOR_LOAD);
124+
CompletableFuture<Void> future = CompletableFuture.runAsync(() ->
125+
BlockArrayCache.buildCache(MachineRegistry.getLoadedMachines()));
126+
109127
MachineModifier.loadAll();
110128
MMEvents.registryAll();
111129
RecipeAdapterRegistry.registerDynamicMachineAdapters();
112130

113131
RecipeRegistry.getRegistry().loadRecipeRegistry(null, true);
132+
future.join();
114133

115134
if (Mods.TOP.isPresent()) {
116135
ModIntegrationTOP.registerProvider();
@@ -181,17 +200,4 @@ public enum GuiType {
181200
this.requiredTileEntity = requiredTileEntity;
182201
}
183202
}
184-
185-
private static void checkThirdPartyServer() {
186-
try {
187-
Class.forName("catserver.server.CatServer");
188-
ModularMachinery.log.warn("//////// Plugin Server Detected! ////////");
189-
ModularMachinery.log.warn("Plugin server will break MMCE's asynchronous functionality.");
190-
ModularMachinery.log.warn("Plugin server compatibility mode is enabled!");
191-
ModularMachinery.log.warn("This will cause asynchronous effects to drop and raise the overhead of the main thread!");
192-
ModularMachinery.pluginServerCompatibleMode = true;
193-
} catch (Exception e) {
194-
ModularMachinery.pluginServerCompatibleMode = false;
195-
}
196-
}
197203
}

src/main/java/hellfirepvp/modularmachinery/common/block/BlockController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,13 @@ public boolean hasTileEntity() {
208208
@Nullable
209209
@Override
210210
public TileEntity createTileEntity(World world, IBlockState state) {
211-
return new TileMachineController(this);
211+
return new TileMachineController(state);
212212
}
213213

214214
@Nullable
215215
@Override
216216
public TileEntity createNewTileEntity(World worldIn, int meta) {
217-
return new TileMachineController(this);
217+
return new TileMachineController(getStateFromMeta(meta));
218218
}
219219

220220
@Nonnull

src/main/java/hellfirepvp/modularmachinery/common/block/BlockEnergyInputHatch.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import hellfirepvp.modularmachinery.common.base.Mods;
1313
import hellfirepvp.modularmachinery.common.block.prop.EnergyHatchData;
1414
import hellfirepvp.modularmachinery.common.tiles.TileEnergyInputHatch;
15+
import hellfirepvp.modularmachinery.common.util.MiscUtils;
1516
import net.minecraft.block.state.IBlockState;
1617
import net.minecraft.client.resources.I18n;
1718
import net.minecraft.client.util.ITooltipFlag;
@@ -38,21 +39,21 @@ public class BlockEnergyInputHatch extends BlockEnergyHatch {
3839
@Optional.Method(modid = "gregtech")
3940
protected void addGTTooltip(List<String> tooltip, EnergyHatchData size) {
4041
tooltip.add(TextFormatting.GRAY + I18n.format("tooltip.energyhatch.gregtech.voltage.in",
41-
String.valueOf(size.getGTEnergyTransferVoltage()),
42+
MiscUtils.formatDecimal(size.getGTEnergyTransferVoltage()),
4243
size.getUnlocalizedGTEnergyTier()));
4344
tooltip.add(TextFormatting.GRAY + I18n.format("tooltip.energyhatch.gregtech.amperage",
4445
String.valueOf(size.getGtAmperage())));
4546
tooltip.add(TextFormatting.GRAY + I18n.format("tooltip.energyhatch.gregtech.storage",
46-
String.valueOf(EnergyDisplayUtil.EnergyType.GT_EU.formatEnergyForDisplay(size.maxEnergy))));
47+
MiscUtils.formatDecimal(EnergyDisplayUtil.EnergyType.GT_EU.formatEnergyForDisplay(size.maxEnergy))));
4748
}
4849

4950
@Override
5051
@SideOnly(Side.CLIENT)
5152
public void addInformation(ItemStack stack, @Nullable World player, List<String> tooltip, ITooltipFlag advanced) {
5253
EnergyHatchData size = EnergyHatchData.values()[MathHelper.clamp(stack.getMetadata(), 0, EnergyHatchData.values().length - 1)];
5354
if (EnergyDisplayUtil.displayFETooltip) {
54-
tooltip.add(TextFormatting.GRAY + I18n.format("tooltip.energyhatch.storage", size.maxEnergy));
55-
tooltip.add(TextFormatting.GRAY + I18n.format("tooltip.energyhatch.in.accept", size.transferLimit));
55+
tooltip.add(TextFormatting.GRAY + I18n.format("tooltip.energyhatch.storage", MiscUtils.formatDecimal(size.maxEnergy)));
56+
tooltip.add(TextFormatting.GRAY + I18n.format("tooltip.energyhatch.in.accept", MiscUtils.formatDecimal(size.transferLimit)));
5657
tooltip.add("");
5758
}
5859
if (Mods.IC2.isPresent() && EnergyDisplayUtil.displayIC2EUTooltip) {

src/main/java/hellfirepvp/modularmachinery/common/block/BlockEnergyOutputHatch.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import hellfirepvp.modularmachinery.common.base.Mods;
1313
import hellfirepvp.modularmachinery.common.block.prop.EnergyHatchData;
1414
import hellfirepvp.modularmachinery.common.tiles.TileEnergyOutputHatch;
15+
import hellfirepvp.modularmachinery.common.util.MiscUtils;
1516
import net.minecraft.block.state.IBlockState;
1617
import net.minecraft.client.resources.I18n;
1718
import net.minecraft.client.util.ITooltipFlag;
@@ -38,28 +39,28 @@ public class BlockEnergyOutputHatch extends BlockEnergyHatch {
3839
@Optional.Method(modid = "gregtech")
3940
protected void addGTTooltip(List<String> tooltip, EnergyHatchData size) {
4041
tooltip.add(TextFormatting.GRAY + I18n.format("tooltip.energyhatch.gregtech.voltage.out",
41-
String.valueOf(size.getGTEnergyTransferVoltage()),
42+
MiscUtils.formatDecimal(size.getGTEnergyTransferVoltage()),
4243
size.getUnlocalizedGTEnergyTier()));
4344
tooltip.add(TextFormatting.GRAY + I18n.format("tooltip.energyhatch.gregtech.amperage",
4445
String.valueOf(size.getGtAmperage())));
4546
tooltip.add(TextFormatting.GRAY + I18n.format("tooltip.energyhatch.gregtech.storage",
46-
String.valueOf(EnergyDisplayUtil.EnergyType.GT_EU.formatEnergyForDisplay(size.maxEnergy))));
47+
MiscUtils.formatDecimal(EnergyDisplayUtil.EnergyType.GT_EU.formatEnergyForDisplay(size.maxEnergy))));
4748
}
4849

4950
@Override
5051
@SideOnly(Side.CLIENT)
5152
public void addInformation(ItemStack stack, @Nullable World player, List<String> tooltip, ITooltipFlag advanced) {
5253
EnergyHatchData size = EnergyHatchData.values()[MathHelper.clamp(stack.getMetadata(), 0, EnergyHatchData.values().length - 1)];
5354
if (EnergyDisplayUtil.displayFETooltip) {
54-
tooltip.add(TextFormatting.GRAY + I18n.format("tooltip.energyhatch.storage", size.maxEnergy));
55-
tooltip.add(TextFormatting.GRAY + I18n.format("tooltip.energyhatch.out.transfer", size.transferLimit));
55+
tooltip.add(TextFormatting.GRAY + I18n.format("tooltip.energyhatch.storage", MiscUtils.formatDecimal(size.maxEnergy)));
56+
tooltip.add(TextFormatting.GRAY + I18n.format("tooltip.energyhatch.out.transfer", MiscUtils.formatDecimal(size.transferLimit)));
5657
tooltip.add("");
5758
}
5859
if (Mods.IC2.isPresent() && EnergyDisplayUtil.displayIC2EUTooltip) {
5960
tooltip.add(TextFormatting.GRAY + I18n.format("tooltip.energyhatch.ic2.out.voltage",
6061
TextFormatting.BLUE + I18n.format(size.getUnlocalizedEnergyDescriptor())));
6162
tooltip.add(TextFormatting.GRAY + I18n.format("tooltip.energyhatch.ic2.out.transfer",
62-
TextFormatting.BLUE + String.valueOf(size.getIC2EnergyTransmission()),
63+
TextFormatting.BLUE + MiscUtils.formatDecimal(size.getIC2EnergyTransmission()),
6364
TextFormatting.BLUE + I18n.format("tooltip.energyhatch.ic2.powerrate")));
6465
tooltip.add("");
6566
}

src/main/java/hellfirepvp/modularmachinery/common/crafting/helper/RecipeCraftingContext.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -134,30 +134,6 @@ public CraftingCheckResult ioTick(int currentTick) {
134134
}
135135
}
136136

137-
//Output tick
138-
// for (ComponentRequirement<?, ?> requirement : requirements) {
139-
// if (!(requirement instanceof ComponentRequirement.PerTick) ||
140-
// requirement.actionType == IOType.INPUT) continue;
141-
// ComponentRequirement.PerTick<?, ?> perTickRequirement = (ComponentRequirement.PerTick<?, ?>) requirement;
142-
//
143-
// perTickRequirement.resetIOTick(this);
144-
// perTickRequirement.startIOTick(this, durMultiplier);
145-
//
146-
// for (ProcessingComponent<?> component : getComponentsFor(requirement, requirement.tag)) {
147-
// CraftCheck result = perTickRequirement.doIOTick(component, this);
148-
// if (result.isSuccess()) {
149-
// break;
150-
// }
151-
// }
152-
//
153-
// CraftCheck result = perTickRequirement.resetIOTick(this);
154-
// if (!result.isSuccess()) {
155-
// CraftingCheckResult res = new CraftingCheckResult();
156-
// res.addError(result.getUnlocalizedMessage());
157-
// return res;
158-
// }
159-
// }
160-
161137
this.getParentRecipe().getCommandContainer().runTickCommands(this.commandSender, currentTick);
162138

163139
return CraftingCheckResult.SUCCESS;

src/main/java/hellfirepvp/modularmachinery/common/item/ItemDebugStruct.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import net.minecraft.util.text.TextComponentString;
2424
import net.minecraft.world.World;
2525

26+
import javax.annotation.Nonnull;
27+
2628
/**
2729
* This class is part of the Modular Machinery Mod
2830
* The complete source code for this mod can be found on github.
@@ -32,13 +34,19 @@
3234
*/
3335
public class ItemDebugStruct extends Item {
3436

37+
// TODO: Realize it.
3538
public ItemDebugStruct() {
3639
setMaxStackSize(1);
3740
setCreativeTab(CommonProxy.creativeTabModularMachinery);
3841
}
3942

43+
@Nonnull
4044
@Override
41-
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
45+
public EnumActionResult onItemUse(
46+
@Nonnull EntityPlayer player, World worldIn, @Nonnull BlockPos pos,
47+
@Nonnull EnumHand hand, @Nonnull EnumFacing facing,
48+
float hitX, float hitY, float hitZ)
49+
{
4250
if (worldIn.isRemote) return EnumActionResult.SUCCESS;
4351
TileEntity te = worldIn.getTileEntity(pos);
4452
if (te instanceof TileMachineController) {
@@ -59,7 +67,7 @@ public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos p
5967
}
6068
}
6169
face = face.rotateYCCW();
62-
pattern = pattern.rotateYCCW(face);
70+
pattern = pattern.rotateYCCW();
6371
replacements = replacements.rotateYCCW();
6472
} while (face != EnumFacing.NORTH);
6573
}

src/main/java/hellfirepvp/modularmachinery/common/machine/DynamicMachine.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import javax.annotation.Nullable;
3434
import java.lang.reflect.Type;
3535
import java.util.*;
36-
import java.util.stream.Collectors;
3736

3837
/**
3938
* This class is part of the Modular Machinery Mod
@@ -110,10 +109,13 @@ public Map<BlockPos, List<ModifierReplacement>> getModifiers() {
110109
public ModifierReplacementMap getModifiersAsMatchingReplacements() {
111110
ModifierReplacementMap infoMap = new ModifierReplacementMap();
112111
for (BlockPos pos : modifiers.keySet()) {
113-
infoMap.put(pos, modifiers.get(pos)
114-
.stream()
115-
.map(ModifierReplacement::getBlockInformation)
116-
.collect(Collectors.toList()));
112+
List<ModifierReplacement> replacements = modifiers.get(pos);
113+
List<BlockArray.BlockInformation> informationList = new ArrayList<>();
114+
for (ModifierReplacement replacement : replacements) {
115+
informationList.add(replacement.getBlockInformation());
116+
}
117+
118+
infoMap.put(pos, informationList);
117119
}
118120
return infoMap;
119121
}

0 commit comments

Comments
 (0)