From d79d2846906e400f228917b101ae8f721bb9f952 Mon Sep 17 00:00:00 2001 From: yueh Date: Mon, 29 Jun 2020 22:17:25 +0200 Subject: [PATCH 01/11] Fix formatting --- src/main/java/appeng/block/AEBaseTileBlock.java | 2 +- src/main/java/appeng/block/misc/TinyTNTBlock.java | 3 ++- .../java/appeng/core/api/definitions/ApiItems.java | 9 ++++----- src/main/java/appeng/entity/TinyTNTPrimedEntity.java | 10 +++++----- src/main/java/appeng/fluids/util/AEFluidStack.java | 3 ++- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/main/java/appeng/block/AEBaseTileBlock.java b/src/main/java/appeng/block/AEBaseTileBlock.java index 8a1191b2d..76470f67a 100644 --- a/src/main/java/appeng/block/AEBaseTileBlock.java +++ b/src/main/java/appeng/block/AEBaseTileBlock.java @@ -279,7 +279,7 @@ public abstract class AEBaseTileBlock extends AEBase } public ActionResultType onActivated(final World w, final BlockPos pos, final PlayerEntity player, final Hand hand, - final @Nullable ItemStack heldItem, final BlockRayTraceResult hit) { + final @Nullable ItemStack heldItem, final BlockRayTraceResult hit) { return ActionResultType.PASS; } diff --git a/src/main/java/appeng/block/misc/TinyTNTBlock.java b/src/main/java/appeng/block/misc/TinyTNTBlock.java index 5e00e5016..41eda557d 100644 --- a/src/main/java/appeng/block/misc/TinyTNTBlock.java +++ b/src/main/java/appeng/block/misc/TinyTNTBlock.java @@ -67,7 +67,8 @@ public class TinyTNTBlock extends AEBaseBlock { } @Override - public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) { + public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, + Hand handIn, BlockRayTraceResult hit) { ItemStack heldItem = player.getHeldItem(handIn); if (!heldItem.isEmpty() && heldItem.getItem() == Items.FLINT_AND_STEEL) { this.startFuse(world, pos, player); diff --git a/src/main/java/appeng/core/api/definitions/ApiItems.java b/src/main/java/appeng/core/api/definitions/ApiItems.java index 4437cee71..019318434 100644 --- a/src/main/java/appeng/core/api/definitions/ApiItems.java +++ b/src/main/java/appeng/core/api/definitions/ApiItems.java @@ -20,9 +20,6 @@ package appeng.core.api.definitions; import java.util.function.Consumer; -import appeng.bootstrap.IItemRendering; -import appeng.bootstrap.ItemRenderingCustomizer; -import appeng.fluids.items.FluidDummyItemColor; import net.minecraft.entity.EntityClassification; import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; @@ -36,6 +33,8 @@ import appeng.api.features.AEFeature; import appeng.api.util.AEColor; import appeng.api.util.AEColoredItemDefinition; import appeng.bootstrap.FeatureFactory; +import appeng.bootstrap.IItemRendering; +import appeng.bootstrap.ItemRenderingCustomizer; import appeng.core.features.ActivityState; import appeng.core.features.ColoredItemDefinition; import appeng.core.features.ItemStackSrc; @@ -47,6 +46,7 @@ import appeng.debug.ReplicatorCardItem; import appeng.entity.GrowingCrystalEntity; import appeng.fluids.items.BasicFluidStorageCell; import appeng.fluids.items.FluidDummyItem; +import appeng.fluids.items.FluidDummyItemColor; import appeng.hooks.BlockToolDispenseItemBehavior; import appeng.hooks.MatterCannonDispenseItemBehavior; import appeng.items.materials.MaterialType; @@ -313,8 +313,7 @@ public final class ApiItems implements IItems { public void customize(IItemRendering rendering) { rendering.color(new FluidDummyItemColor()); } - }) - .build(); + }).build(); } private static AEColoredItemDefinition createPaintBalls(FeatureFactory registry, String idSuffix, boolean lumen) { diff --git a/src/main/java/appeng/entity/TinyTNTPrimedEntity.java b/src/main/java/appeng/entity/TinyTNTPrimedEntity.java index 646a233cc..55ad38ed2 100644 --- a/src/main/java/appeng/entity/TinyTNTPrimedEntity.java +++ b/src/main/java/appeng/entity/TinyTNTPrimedEntity.java @@ -20,6 +20,8 @@ package appeng.entity; import java.util.List; +import javax.annotation.Nullable; + import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.material.Material; @@ -41,6 +43,7 @@ import net.minecraft.world.Explosion; import net.minecraft.world.Explosion.Mode; import net.minecraft.world.World; import net.minecraftforge.fml.common.registry.IEntityAdditionalSpawnData; +import net.minecraftforge.fml.network.NetworkHooks; import appeng.api.AEApi; import appeng.api.features.AEFeature; @@ -48,9 +51,6 @@ import appeng.core.AEConfig; import appeng.core.AppEng; import appeng.core.sync.packets.MockExplosionPacket; import appeng.util.Platform; -import net.minecraftforge.fml.network.NetworkHooks; - -import javax.annotation.Nullable; public final class TinyTNTPrimedEntity extends TNTEntity implements IEntityAdditionalSpawnData { @@ -67,8 +67,8 @@ public final class TinyTNTPrimedEntity extends TNTEntity implements IEntityAddit final LivingEntity igniter) { super(TYPE, w); this.setPosition(x, y, z); - double d0 = w.rand.nextDouble() * (double)((float)Math.PI * 2F); - this.setMotion(-Math.sin(d0) * 0.02D, (double)0.2F, -Math.cos(d0) * 0.02D); + double d0 = w.rand.nextDouble() * (double) ((float) Math.PI * 2F); + this.setMotion(-Math.sin(d0) * 0.02D, (double) 0.2F, -Math.cos(d0) * 0.02D); this.setFuse(80); this.prevPosX = x; this.prevPosY = y; diff --git a/src/main/java/appeng/fluids/util/AEFluidStack.java b/src/main/java/appeng/fluids/util/AEFluidStack.java index 5355cf08d..9569c550d 100644 --- a/src/main/java/appeng/fluids/util/AEFluidStack.java +++ b/src/main/java/appeng/fluids/util/AEFluidStack.java @@ -254,7 +254,8 @@ public final class AEFluidStack extends AEStack implements IAEFlu @Override public void writeToPacket(final PacketBuffer buffer) { buffer.writeBoolean(this.isCraftable()); - // Cannot use writeFluidStack here because for FluidStacks with amount==0, it will not write the fluid + // Cannot use writeFluidStack here because for FluidStacks with amount==0, it + // will not write the fluid buffer.writeRegistryIdUnsafe(ForgeRegistries.FLUIDS, fluid); buffer.writeCompoundTag(getFluidStack().getTag()); buffer.writeVarLong(this.getStackSize()); From 66d00a460021245a3fc69b1ee6870d90224f5a55 Mon Sep 17 00:00:00 2001 From: yueh Date: Mon, 29 Jun 2020 22:17:39 +0200 Subject: [PATCH 02/11] Use our own TransparencyState. ObfuscationReflectionHelper is a massive performance issue. --- .../render/tesr/DriveLedTileEntityRenderer.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/appeng/client/render/tesr/DriveLedTileEntityRenderer.java b/src/main/java/appeng/client/render/tesr/DriveLedTileEntityRenderer.java index a599ddb84..8969a7b51 100644 --- a/src/main/java/appeng/client/render/tesr/DriveLedTileEntityRenderer.java +++ b/src/main/java/appeng/client/render/tesr/DriveLedTileEntityRenderer.java @@ -3,6 +3,7 @@ package appeng.client.render.tesr; import java.util.EnumMap; import com.mojang.blaze3d.matrix.MatrixStack; +import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.IVertexBuilder; import net.minecraft.client.renderer.IRenderTypeBuffer; @@ -14,7 +15,6 @@ import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; import net.minecraft.client.renderer.vertex.DefaultVertexFormats; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; -import net.minecraftforge.fml.common.ObfuscationReflectionHelper; import appeng.block.storage.DriveSlotState; import appeng.client.render.FacingToRotation; @@ -26,6 +26,14 @@ import appeng.tile.storage.DriveTileEntity; @OnlyIn(Dist.CLIENT) public class DriveLedTileEntityRenderer extends TileEntityRenderer { + private static final RenderState.TransparencyState TRANSLUCENT_TRANSPARENCY = new RenderState.TransparencyState( + "translucent_transparency", () -> { + RenderSystem.enableBlend(); + RenderSystem.defaultBlendFunc(); + }, () -> { + RenderSystem.disableBlend(); + }); + private static final EnumMap STATE_COLORS; // Color used for the cell indicator for blinking during recent activity @@ -80,8 +88,6 @@ public class DriveLedTileEntityRenderer extends TileEntityRenderer Date: Mon, 29 Jun 2020 22:39:34 +0200 Subject: [PATCH 03/11] Render all sides of the cell models regardless of cullface. --- .../java/appeng/client/render/model/DriveBakedModel.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/appeng/client/render/model/DriveBakedModel.java b/src/main/java/appeng/client/render/model/DriveBakedModel.java index d29b62445..f5070eff0 100644 --- a/src/main/java/appeng/client/render/model/DriveBakedModel.java +++ b/src/main/java/appeng/client/render/model/DriveBakedModel.java @@ -63,7 +63,7 @@ public class DriveBakedModel extends DelegateBakedModel { DriveSlotsState slotsState = driveModelData.getSlotsState(); - if (side == null && slotsState != null) { + if (slotsState != null) { for (int row = 0; row < 5; row++) { for (int col = 0; col < 2; col++) { Matrix4f transform = new Matrix4f(); @@ -80,7 +80,7 @@ public class DriveBakedModel extends DelegateBakedModel { // Add the drive chassis Item cell = slotsState.getCell(slot); IBakedModel cellChassisModel = getCellChassisModel(cell); - addModel(state, rand, extraData, result, cellChassisModel, transform); + addModel(state, rand, extraData, result, side, cellChassisModel, transform); } } } @@ -112,9 +112,9 @@ public class DriveBakedModel extends DelegateBakedModel { } private static void addModel(@Nullable BlockState state, @Nonnull Random rand, @Nonnull IModelData extraData, - List result, IBakedModel bakedCell, Matrix4f transform) { + List result, Direction side, IBakedModel bakedCell, Matrix4f transform) { MatrixVertexTransformer transformer = new MatrixVertexTransformer(transform); - for (BakedQuad bakedQuad : bakedCell.getQuads(state, null, rand, extraData)) { + for (BakedQuad bakedQuad : bakedCell.getQuads(state, side, rand, extraData)) { BakedQuadBuilder builder = new BakedQuadBuilder(); transformer.setParent(builder); transformer.setVertexFormat(builder.getVertexFormat()); From 003ef8672c17833010b6648905b76256227bcc7d Mon Sep 17 00:00:00 2001 From: yueh Date: Tue, 30 Jun 2020 10:27:22 +0200 Subject: [PATCH 04/11] Added cullface to drive cell models. --- .../models/block/drive/drive_cell.json | 22 +++++++++++++++++++ .../models/block/drive/drive_cell_fluids.json | 18 +++------------ .../models/block/drive/drive_cell_items.json | 18 +++------------ 3 files changed, 28 insertions(+), 30 deletions(-) create mode 100644 src/main/resources/assets/appliedenergistics2/models/block/drive/drive_cell.json diff --git a/src/main/resources/assets/appliedenergistics2/models/block/drive/drive_cell.json b/src/main/resources/assets/appliedenergistics2/models/block/drive/drive_cell.json new file mode 100644 index 000000000..fdeb22556 --- /dev/null +++ b/src/main/resources/assets/appliedenergistics2/models/block/drive/drive_cell.json @@ -0,0 +1,22 @@ +{ + "textures": { + "cell": "appliedenergistics2:block/drive/item_cell" + }, + "elements": [ + { + "name": "Cell Backdrop", + "from": [9, 13, 1], + "to": [15, 15, 3], + "rotation": { "angle": 0, "axis": "y", "origin": [9, 8, 8] }, + "faces": { + "north": { + "uv": [0, 2, 6, 4], + "texture": "#cell", + "cullface": "north" + }, + "up": { "uv": [0, 0, 6, 2], "texture": "#cell", "cullface": "north" }, + "down": { "uv": [0, 4, 6, 6], "texture": "#cell", "cullface": "north" } + } + } + ] +} diff --git a/src/main/resources/assets/appliedenergistics2/models/block/drive/drive_cell_fluids.json b/src/main/resources/assets/appliedenergistics2/models/block/drive/drive_cell_fluids.json index 178a52dc5..c1e46c708 100644 --- a/src/main/resources/assets/appliedenergistics2/models/block/drive/drive_cell_fluids.json +++ b/src/main/resources/assets/appliedenergistics2/models/block/drive/drive_cell_fluids.json @@ -1,18 +1,6 @@ { + "parent": "appliedenergistics2:block/drive/drive_cell", "textures": { - "front": "appliedenergistics2:block/drive/fluid_cell" - }, - "elements": [ - { - "name": "Cell Backdrop", - "from": [9, 13, 1], - "to": [15, 15, 3], - "rotation": { "angle": 0, "axis": "y", "origin": [9, 8, 8] }, - "faces": { - "north": { "uv": [0, 2, 6, 4], "texture": "#front" }, - "up": { "uv": [0, 0, 6, 2], "texture": "#front" }, - "down": { "uv": [0, 4, 6, 6], "texture": "#front" } - } - } - ] + "cell": "appliedenergistics2:block/drive/fluid_cell" + } } diff --git a/src/main/resources/assets/appliedenergistics2/models/block/drive/drive_cell_items.json b/src/main/resources/assets/appliedenergistics2/models/block/drive/drive_cell_items.json index a4f06fb19..5fcaa819c 100644 --- a/src/main/resources/assets/appliedenergistics2/models/block/drive/drive_cell_items.json +++ b/src/main/resources/assets/appliedenergistics2/models/block/drive/drive_cell_items.json @@ -1,18 +1,6 @@ { + "parent": "appliedenergistics2:block/drive/drive_cell", "textures": { - "front": "appliedenergistics2:block/drive/item_cell" - }, - "elements": [ - { - "name": "Cell Backdrop", - "from": [9, 13, 1], - "to": [15, 15, 3], - "rotation": { "angle": 0, "axis": "y", "origin": [9, 8, 8] }, - "faces": { - "north": { "uv": [0, 2, 6, 4], "texture": "#front" }, - "up": { "uv": [0, 0, 6, 2], "texture": "#front" }, - "down": { "uv": [0, 4, 6, 6], "texture": "#front" } - } - } - ] + "cell": "appliedenergistics2:block/drive/item_cell" + } } From 17b919a171a7797ec931d0e36e9335dfd41eaf2f Mon Sep 17 00:00:00 2001 From: yueh Date: Tue, 30 Jun 2020 15:37:34 +0200 Subject: [PATCH 05/11] Mark CableBusBlock as variableOpacity. --- src/main/java/appeng/block/networking/CableBusBlock.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/appeng/block/networking/CableBusBlock.java b/src/main/java/appeng/block/networking/CableBusBlock.java index 21e712a39..9210e8ff8 100644 --- a/src/main/java/appeng/block/networking/CableBusBlock.java +++ b/src/main/java/appeng/block/networking/CableBusBlock.java @@ -81,7 +81,7 @@ public class CableBusBlock extends AEBaseTileBlock implement private static final ICableBusContainer NULL_CABLE_BUS = new NullCableBusContainer(); public CableBusBlock() { - super(defaultProps(AEGlassMaterial.INSTANCE).notSolid().noDrops()); + super(defaultProps(AEGlassMaterial.INSTANCE).notSolid().noDrops().variableOpacity()); } @Override From d0acc9ccf1e03a5b0202c27992ad5b6c8eec73e5 Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Tue, 30 Jun 2020 18:34:36 +0200 Subject: [PATCH 06/11] Removed reliance of TickTracker on implementation detail to generate additional crash information. --- src/api/java/appeng/api/parts/IPart.java | 11 +++++++++++ .../java/appeng/me/cache/helpers/TickTracker.java | 6 +++--- src/main/java/appeng/parts/AEBasePart.java | 1 + 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/api/java/appeng/api/parts/IPart.java b/src/api/java/appeng/api/parts/IPart.java index 05db17b45..89c945f95 100644 --- a/src/api/java/appeng/api/parts/IPart.java +++ b/src/api/java/appeng/api/parts/IPart.java @@ -32,6 +32,7 @@ import javax.annotation.Nonnull; import com.mojang.blaze3d.matrix.MatrixStack; import net.minecraft.client.renderer.IRenderTypeBuffer; +import net.minecraft.crash.CrashReportCategory; import net.minecraft.entity.Entity; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; @@ -371,4 +372,14 @@ public interface IPart extends ICustomCableConnection { */ void getBoxes(final IPartCollisionHelper bch); + /** + * This will be used by the core to add information about this part to a crash + * report if it is attached to a host that caused a crash during tick + * processing. + * + * @param section The crash report section the information will be added to. + */ + default void addEntityCrashInfo(final CrashReportCategory section) { + } + } diff --git a/src/main/java/appeng/me/cache/helpers/TickTracker.java b/src/main/java/appeng/me/cache/helpers/TickTracker.java index 8c83f4a26..ce77c6faa 100644 --- a/src/main/java/appeng/me/cache/helpers/TickTracker.java +++ b/src/main/java/appeng/me/cache/helpers/TickTracker.java @@ -25,9 +25,9 @@ import net.minecraft.crash.CrashReportCategory; import appeng.api.networking.IGridNode; import appeng.api.networking.ticking.IGridTickable; import appeng.api.networking.ticking.TickingRequest; +import appeng.api.parts.IPart; import appeng.api.util.DimensionalCoord; import appeng.me.cache.TickManagerCache; -import appeng.parts.AEBasePart; public class TickTracker implements Comparable { @@ -72,8 +72,8 @@ public class TickTracker implements Comparable { } public void addEntityCrashInfo(final CrashReportCategory crashreportcategory) { - if (this.getGridTickable() instanceof AEBasePart) { - final AEBasePart part = (AEBasePart) this.getGridTickable(); + if (this.getGridTickable() instanceof IPart) { + final IPart part = (IPart) this.getGridTickable(); part.addEntityCrashInfo(crashreportcategory); } diff --git a/src/main/java/appeng/parts/AEBasePart.java b/src/main/java/appeng/parts/AEBasePart.java index 8bd1c8910..08a835301 100644 --- a/src/main/java/appeng/parts/AEBasePart.java +++ b/src/main/java/appeng/parts/AEBasePart.java @@ -182,6 +182,7 @@ public abstract class AEBasePart implements IPart, IGridProxyable, IActionHost, return this.getItemStack().hasDisplayName(); } + @Override public void addEntityCrashInfo(final CrashReportCategory crashreportcategory) { crashreportcategory.addDetail("Part Side", this.getSide()); } From d17f8cec92f2072760a36e73dd8ed018023bf226 Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Tue, 30 Jun 2020 18:39:34 +0200 Subject: [PATCH 07/11] Removed unused fields. --- .../java/appeng/me/cache/TickManagerCache.java | 17 +++++------------ .../appeng/me/cache/helpers/TickTracker.java | 10 +--------- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/src/main/java/appeng/me/cache/TickManagerCache.java b/src/main/java/appeng/me/cache/TickManagerCache.java index 12a3d9cd9..2134682c5 100644 --- a/src/main/java/appeng/me/cache/TickManagerCache.java +++ b/src/main/java/appeng/me/cache/TickManagerCache.java @@ -21,6 +21,7 @@ package appeng.me.cache; import java.util.HashMap; import java.util.PriorityQueue; +import appeng.helpers.Reflected; import com.google.common.base.Preconditions; import net.minecraft.crash.CrashReport; @@ -39,7 +40,6 @@ import appeng.me.cache.helpers.TickTracker; public class TickManagerCache implements ITickManager { - private final IGrid myGrid; private final HashMap alertable = new HashMap<>(); private final HashMap sleeping = new HashMap<>(); private final HashMap awake = new HashMap<>(); @@ -47,12 +47,8 @@ public class TickManagerCache implements ITickManager { private long currentTick = 0; - public TickManagerCache(final IGrid g) { - this.myGrid = g; - } - - public long getCurrentTick() { - return this.currentTick; + @Reflected + public TickManagerCache(@SuppressWarnings("unused") final IGrid g) { } public long getAvgNanoTime(final IGridNode node) { @@ -66,7 +62,7 @@ public class TickManagerCache implements ITickManager { return -1; } - return tt.getAvgNanos(); + return 0; } @Override @@ -146,7 +142,7 @@ public class TickManagerCache implements ITickManager { Preconditions.checkNotNull(tr); - final TickTracker tt = new TickTracker(tr, gridNode, (IGridTickable) machine, this.currentTick, this); + final TickTracker tt = new TickTracker(tr, gridNode, (IGridTickable) machine, this.currentTick); if (tr.canBeAlerted) { this.alertable.put(gridNode, tt); @@ -184,9 +180,6 @@ public class TickManagerCache implements ITickManager { if (tt == null) { return false; } - // throw new RuntimeException( - // "Invalid alerted device, this node is not marked as alertable, or part of - // this grid." ); // set to awake, this is for sanity. this.sleeping.remove(node); diff --git a/src/main/java/appeng/me/cache/helpers/TickTracker.java b/src/main/java/appeng/me/cache/helpers/TickTracker.java index ce77c6faa..e1e350ab1 100644 --- a/src/main/java/appeng/me/cache/helpers/TickTracker.java +++ b/src/main/java/appeng/me/cache/helpers/TickTracker.java @@ -27,7 +27,6 @@ import appeng.api.networking.ticking.IGridTickable; import appeng.api.networking.ticking.TickingRequest; import appeng.api.parts.IPart; import appeng.api.util.DimensionalCoord; -import appeng.me.cache.TickManagerCache; public class TickTracker implements Comparable { @@ -35,13 +34,10 @@ public class TickTracker implements Comparable { private final IGridTickable gt; private final IGridNode node; - private final long LastFiveTicksTime = 0; - private long lastTick; private int currentRate; - public TickTracker(final TickingRequest req, final IGridNode node, final IGridTickable gt, final long currentTick, - final TickManagerCache tickManagerCache) { + public TickTracker(final TickingRequest req, final IGridNode node, final IGridTickable gt, final long currentTick) { this.request = req; this.gt = gt; this.node = node; @@ -49,10 +45,6 @@ public class TickTracker implements Comparable { this.setLastTick(currentTick); } - public long getAvgNanos() { - return (this.LastFiveTicksTime / 5); - } - @Override public int compareTo(@Nonnull final TickTracker t) { int next = Long.compare(this.getNextTick(), t.getNextTick()); From 8d8d3dd2d79c1cb06c4fe978d36da61bd40bdb9e Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Tue, 30 Jun 2020 18:57:26 +0200 Subject: [PATCH 08/11] Minor refactoring. Replaced use of reflection with use of functional factory interface (API change). --- .../api/networking/IGridCacheFactory.java | 43 +++++++++++++ .../api/networking/IGridCacheRegistry.java | 6 +- src/main/java/appeng/core/Registration.java | 16 ++--- .../registries/GridCacheRegistry.java | 63 ++++++++++--------- src/main/java/appeng/me/Grid.java | 3 +- .../appeng/me/cache/TickManagerCache.java | 2 - 6 files changed, 89 insertions(+), 44 deletions(-) create mode 100644 src/api/java/appeng/api/networking/IGridCacheFactory.java diff --git a/src/api/java/appeng/api/networking/IGridCacheFactory.java b/src/api/java/appeng/api/networking/IGridCacheFactory.java new file mode 100644 index 000000000..3bab888ee --- /dev/null +++ b/src/api/java/appeng/api/networking/IGridCacheFactory.java @@ -0,0 +1,43 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2013 AlgorithmX2 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package appeng.api.networking; + +import javax.annotation.Nonnull; + +/** + * A factory for grid cache implementations. + */ +@FunctionalInterface +public interface IGridCacheFactory { + + /** + * Creates a grid cache for the given grid. + * + * @param grid The grid for which the cache should be created. + * @return A new grid cache instance. + */ + @Nonnull + T createCache(IGrid grid); + +} diff --git a/src/api/java/appeng/api/networking/IGridCacheRegistry.java b/src/api/java/appeng/api/networking/IGridCacheRegistry.java index 6febca132..6c6cadb8a 100644 --- a/src/api/java/appeng/api/networking/IGridCacheRegistry.java +++ b/src/api/java/appeng/api/networking/IGridCacheRegistry.java @@ -36,10 +36,10 @@ public interface IGridCacheRegistry { * Register a new grid cache for use during operation, must be called during the * loading phase. * - * @param iface grid cache class + * @param iface grid cache class + * @param factory Factory for creating a new instance for each constructed grid */ - void registerGridCache(@Nonnull Class iface, - @Nonnull Class implementation); + void registerGridCache(@Nonnull Class iface, @Nonnull IGridCacheFactory factory); /** * requests a new INSTANCE of a grid cache for use, used internally diff --git a/src/main/java/appeng/core/Registration.java b/src/main/java/appeng/core/Registration.java index 3ee9f956e..32959e85a 100644 --- a/src/main/java/appeng/core/Registration.java +++ b/src/main/java/appeng/core/Registration.java @@ -130,14 +130,14 @@ final class Registration { final IRegistryContainer registries = api.registries(); final IGridCacheRegistry gcr = registries.gridCache(); - gcr.registerGridCache(ITickManager.class, TickManagerCache.class); - gcr.registerGridCache(IEnergyGrid.class, EnergyGridCache.class); - gcr.registerGridCache(IPathingGrid.class, PathGridCache.class); - gcr.registerGridCache(IStorageGrid.class, GridStorageCache.class); - gcr.registerGridCache(P2PCache.class, P2PCache.class); - gcr.registerGridCache(ISpatialCache.class, SpatialPylonCache.class); - gcr.registerGridCache(ISecurityGrid.class, SecurityCache.class); - gcr.registerGridCache(ICraftingGrid.class, CraftingGridCache.class); + gcr.registerGridCache(ITickManager.class, TickManagerCache::new); + gcr.registerGridCache(IEnergyGrid.class, EnergyGridCache::new); + gcr.registerGridCache(IPathingGrid.class, PathGridCache::new); + gcr.registerGridCache(IStorageGrid.class, GridStorageCache::new); + gcr.registerGridCache(P2PCache.class, P2PCache::new); + gcr.registerGridCache(ISpatialCache.class, SpatialPylonCache::new); + gcr.registerGridCache(ISecurityGrid.class, SecurityCache::new); + gcr.registerGridCache(ICraftingGrid.class, CraftingGridCache::new); registries.cell().addCellHandler(new BasicCellHandler()); registries.cell().addCellHandler(new CreativeCellHandler()); diff --git a/src/main/java/appeng/core/features/registries/GridCacheRegistry.java b/src/main/java/appeng/core/features/registries/GridCacheRegistry.java index 35f6d2fc4..ab53f7999 100644 --- a/src/main/java/appeng/core/features/registries/GridCacheRegistry.java +++ b/src/main/java/appeng/core/features/registries/GridCacheRegistry.java @@ -18,54 +18,57 @@ package appeng.core.features.registries; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; +import javax.annotation.Nonnull; + import appeng.api.networking.IGrid; import appeng.api.networking.IGridCache; +import appeng.api.networking.IGridCacheFactory; import appeng.api.networking.IGridCacheRegistry; import appeng.core.AELog; public final class GridCacheRegistry implements IGridCacheRegistry { - private final Map, Class> caches = new HashMap<>(); + + private final List> registry = new ArrayList<>(); @Override - public void registerGridCache(final Class iface, - final Class implementation) { - if (iface.isAssignableFrom(implementation)) { - this.caches.put(iface, implementation); - } else { - throw new IllegalArgumentException( - "Invalid setup, grid cache must either be the same class, or an interface that the implementation implements. Gotten: " - + iface + " and " + implementation); + public synchronized void registerGridCache(@Nonnull Class iface, + @Nonnull IGridCacheFactory factory) { + + if (registry.stream().anyMatch(r -> r.cacheClass.equals(iface))) { + AELog.debug("Overriding grid cache factory for cache type %s", iface); } + + registry.add(new GridCacheRegistration<>(iface, factory)); + } @Override - public HashMap, IGridCache> createCacheInstance(final IGrid g) { - final HashMap, IGridCache> map = new HashMap<>(); + public Map, IGridCache> createCacheInstance(final IGrid g) { + final Map, IGridCache> map = new HashMap<>(registry.size()); - for (final Class iface : this.caches.keySet()) { - try { - final Constructor c = this.caches.get(iface).getConstructor(IGrid.class); - map.put(iface, c.newInstance(g)); - } catch (final NoSuchMethodException e) { - AELog.error("Grid Caches must have a constructor with IGrid as the single param."); - throw new IllegalArgumentException(e); - } catch (final InvocationTargetException e) { - AELog.error("Grid Caches must have a constructor with IGrid as the single param."); - throw new IllegalStateException(e); - } catch (final InstantiationException e) { - AELog.error("Grid Caches must have a constructor with IGrid as the single param."); - throw new IllegalStateException(e); - } catch (final IllegalAccessException e) { - AELog.error("Grid Caches must have a constructor with IGrid as the single param."); - throw new IllegalStateException(e); - } + for (GridCacheRegistration registration : registry) { + map.put(registration.cacheClass, registration.factory.createCache(g)); } return map; } + + private static class GridCacheRegistration { + + private final Class cacheClass; + + private final IGridCacheFactory factory; + + public GridCacheRegistration(Class cacheClass, IGridCacheFactory factory) { + this.cacheClass = cacheClass; + this.factory = factory; + } + + } + } diff --git a/src/main/java/appeng/me/Grid.java b/src/main/java/appeng/me/Grid.java index 1c86bba91..40caef967 100644 --- a/src/main/java/appeng/me/Grid.java +++ b/src/main/java/appeng/me/Grid.java @@ -42,7 +42,7 @@ import appeng.util.ReadOnlyCollection; public class Grid implements IGrid { private final NetworkEventBus eventBus = new NetworkEventBus(); private final Map, MachineSet> machines = new HashMap<>(); - private final Map, GridCacheWrapper> caches = new HashMap<>(); + private final Map, GridCacheWrapper> caches; private GridNode pivot; private int priority; // how import is this network? private GridStorage myStorage; @@ -52,6 +52,7 @@ public class Grid implements IGrid { final Map, IGridCache> myCaches = AEApi.instance().registries().gridCache() .createCacheInstance(this); + this.caches = new HashMap<>(myCaches.size()); for (final Entry, IGridCache> c : myCaches.entrySet()) { final Class key = c.getKey(); final IGridCache value = c.getValue(); diff --git a/src/main/java/appeng/me/cache/TickManagerCache.java b/src/main/java/appeng/me/cache/TickManagerCache.java index 2134682c5..c20605055 100644 --- a/src/main/java/appeng/me/cache/TickManagerCache.java +++ b/src/main/java/appeng/me/cache/TickManagerCache.java @@ -21,7 +21,6 @@ package appeng.me.cache; import java.util.HashMap; import java.util.PriorityQueue; -import appeng.helpers.Reflected; import com.google.common.base.Preconditions; import net.minecraft.crash.CrashReport; @@ -47,7 +46,6 @@ public class TickManagerCache implements ITickManager { private long currentTick = 0; - @Reflected public TickManagerCache(@SuppressWarnings("unused") final IGrid g) { } From 538965e74a77c3350ab3dafae38b7d7c08c54c2c Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Tue, 30 Jun 2020 20:36:43 +0200 Subject: [PATCH 09/11] Crash when someone tries to overwrite existing grid cache implementations. --- .../java/appeng/core/features/registries/GridCacheRegistry.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/appeng/core/features/registries/GridCacheRegistry.java b/src/main/java/appeng/core/features/registries/GridCacheRegistry.java index ab53f7999..f60386214 100644 --- a/src/main/java/appeng/core/features/registries/GridCacheRegistry.java +++ b/src/main/java/appeng/core/features/registries/GridCacheRegistry.java @@ -40,7 +40,7 @@ public final class GridCacheRegistry implements IGridCacheRegistry { @Nonnull IGridCacheFactory factory) { if (registry.stream().anyMatch(r -> r.cacheClass.equals(iface))) { - AELog.debug("Overriding grid cache factory for cache type %s", iface); + throw new IllegalArgumentException("Implementation for grid cache "+ iface + " is already registered!"); } registry.add(new GridCacheRegistration<>(iface, factory)); From fba157a7075ca2fac28b46211d28ca8f457dac19 Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Tue, 30 Jun 2020 20:42:55 +0200 Subject: [PATCH 10/11] Removed unused ItemPredicate --- src/main/java/appeng/core/Registration.java | 4 -- .../appeng/core/stats/PartItemPredicate.java | 59 ------------------- 2 files changed, 63 deletions(-) delete mode 100644 src/main/java/appeng/core/stats/PartItemPredicate.java diff --git a/src/main/java/appeng/core/Registration.java b/src/main/java/appeng/core/Registration.java index 32959e85a..d29bcae6e 100644 --- a/src/main/java/appeng/core/Registration.java +++ b/src/main/java/appeng/core/Registration.java @@ -45,7 +45,6 @@ import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.client.event.*; import net.minecraftforge.client.model.ModelLoader; import net.minecraftforge.common.ModDimension; -import net.minecraftforge.common.crafting.CraftingHelper; import net.minecraftforge.common.extensions.IForgeContainerType; import net.minecraftforge.event.RegistryEvent; import net.minecraftforge.eventbus.api.IEventBus; @@ -94,7 +93,6 @@ import appeng.core.features.registries.cell.BasicItemCellGuiHandler; import appeng.core.features.registries.cell.CreativeCellHandler; import appeng.core.stats.AdvancementTriggers; import appeng.core.stats.AeStats; -import appeng.core.stats.PartItemPredicate; import appeng.fluids.client.gui.*; import appeng.fluids.container.*; import appeng.fluids.registries.BasicFluidCellGuiHandler; @@ -145,8 +143,6 @@ final class Registration { registries.cell().addCellGuiHandler(new BasicFluidCellGuiHandler()); registries.matterCannon().registerAmmoItem(api.definitions().materials().matterBall().item(), 32); - - PartItemPredicate.register(); } @OnlyIn(Dist.CLIENT) diff --git a/src/main/java/appeng/core/stats/PartItemPredicate.java b/src/main/java/appeng/core/stats/PartItemPredicate.java deleted file mode 100644 index 198a9dd90..000000000 --- a/src/main/java/appeng/core/stats/PartItemPredicate.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * This file is part of Applied Energistics 2. - * Copyright (c) 2013 - 2017, AlgorithmX2, All rights reserved. - * - * Applied Energistics 2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Applied Energistics 2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Applied Energistics 2. If not, see . - */ - -package appeng.core.stats; - -import com.google.gson.JsonObject; - -import net.minecraft.advancements.criterion.ItemPredicate; -import net.minecraft.item.ItemStack; -import net.minecraft.util.JSONUtils; -import net.minecraft.util.ResourceLocation; - -import appeng.core.AppEng; -import appeng.items.parts.PartItem; -import appeng.items.parts.PartType; - -public class PartItemPredicate extends ItemPredicate { - private final PartType partType; - - public PartItemPredicate(String partName) { - this.partType = PartType.valueOf(partName.toUpperCase()); - } - - @Override - public boolean test(ItemStack item) { - if (item.getItem() instanceof PartItem) { - PartItem itemPart = (PartItem) item.getItem(); - return itemPart.getType() == partType; - } - return false; - } - - public static ItemPredicate deserialize(JsonObject jsonobject) { - if (jsonobject.has("part")) { - return new PartItemPredicate(JSONUtils.getString(jsonobject, "part")); - } else { - return ItemPredicate.ANY; - } - } - - public static void register() { - ItemPredicate.register(new ResourceLocation(AppEng.MOD_ID, "part"), PartItemPredicate::deserialize); - } -} From 400fcdb633812b58bb660ada1cd476a20c98f050 Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Wed, 1 Jul 2020 00:50:37 +0200 Subject: [PATCH 11/11] Formatting fix --- .../java/appeng/core/features/registries/GridCacheRegistry.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/appeng/core/features/registries/GridCacheRegistry.java b/src/main/java/appeng/core/features/registries/GridCacheRegistry.java index f60386214..520d06839 100644 --- a/src/main/java/appeng/core/features/registries/GridCacheRegistry.java +++ b/src/main/java/appeng/core/features/registries/GridCacheRegistry.java @@ -40,7 +40,7 @@ public final class GridCacheRegistry implements IGridCacheRegistry { @Nonnull IGridCacheFactory factory) { if (registry.stream().anyMatch(r -> r.cacheClass.equals(iface))) { - throw new IllegalArgumentException("Implementation for grid cache "+ iface + " is already registered!"); + throw new IllegalArgumentException("Implementation for grid cache " + iface + " is already registered!"); } registry.add(new GridCacheRegistration<>(iface, factory));