From 1f55c8f174fa0ff2d49facf1ec2b268d570a5c20 Mon Sep 17 00:00:00 2001 From: Nikita Tomashevich Date: Fri, 8 Apr 2022 21:07:04 +0300 Subject: [PATCH] Initial model and graphics for Internet GateWay block --- .../java/li/cil/oc2/client/ClientSetup.java | 1 + .../oc2/client/renderer/ModRenderType.java | 18 +++ .../blockentity/InternetGateWayRenderer.java | 113 +++++++++++++++++ .../common/block/InternetGatewayBlock.java | 7 +- .../InternetGateWayBlockEntity.java | 95 +++++++++++++-- .../oc2/blockstates/internet_gateway.json | 7 ++ .../oc2/models/block/internet_gateway.json | 114 ++++++++++++++++++ .../oc2/models/item/internet_gateway.json | 3 + .../internet_gateway_atlas0.png | Bin 0 -> 281 bytes 9 files changed, 345 insertions(+), 13 deletions(-) create mode 100644 src/main/java/li/cil/oc2/client/renderer/blockentity/InternetGateWayRenderer.java create mode 100644 src/main/resources/assets/oc2/blockstates/internet_gateway.json create mode 100644 src/main/resources/assets/oc2/models/block/internet_gateway.json create mode 100644 src/main/resources/assets/oc2/models/item/internet_gateway.json create mode 100644 src/main/resources/assets/oc2/textures/block/internet_gateway/internet_gateway_atlas0.png diff --git a/src/main/java/li/cil/oc2/client/ClientSetup.java b/src/main/java/li/cil/oc2/client/ClientSetup.java index d704d3c2..0bc003b7 100644 --- a/src/main/java/li/cil/oc2/client/ClientSetup.java +++ b/src/main/java/li/cil/oc2/client/ClientSetup.java @@ -38,6 +38,7 @@ public final class ClientSetup { BlockEntityRenderers.register(BlockEntities.DISK_DRIVE.get(), DiskDriveRenderer::new); BlockEntityRenderers.register(BlockEntities.CHARGER.get(), ChargerRenderer::new); BlockEntityRenderers.register(BlockEntities.PROJECTOR.get(), ProjectorRenderer::new); + BlockEntityRenderers.register(BlockEntities.INTERNET_GATEWAY.get(), InternetGateWayRenderer::new); event.enqueueWork(() -> { CustomItemModelProperties.initialize(); diff --git a/src/main/java/li/cil/oc2/client/renderer/ModRenderType.java b/src/main/java/li/cil/oc2/client/renderer/ModRenderType.java index 2d1a86a2..b41477bd 100644 --- a/src/main/java/li/cil/oc2/client/renderer/ModRenderType.java +++ b/src/main/java/li/cil/oc2/client/renderer/ModRenderType.java @@ -41,6 +41,20 @@ public abstract class ModRenderType extends RenderType { .setCullState(NO_CULL) .createCompositeState(false)); + private static final RenderType GATEWAY_PARTICLE = create( + API.MOD_ID + "/gateway_particle", + DefaultVertexFormat.POSITION_COLOR, + VertexFormat.Mode.QUADS, + 256, + false, + true, + CompositeState.builder() + .setShaderState(RENDERTYPE_LIGHTNING_SHADER) + .setTransparencyState(LIGHTNING_TRANSPARENCY) + .setWriteMaskState(COLOR_WRITE) + .setCullState(CULL) + .createCompositeState(false)); + /////////////////////////////////////////////////////////////////// public static RenderType getNetworkCable() { @@ -87,6 +101,10 @@ public abstract class ModRenderType extends RenderType { state); } + public static RenderType getGateWayParticle() { + return GATEWAY_PARTICLE; + } + /////////////////////////////////////////////////////////////////// private ModRenderType(final String name, final VertexFormat format, final VertexFormat.Mode drawMode, final int bufferSize, final boolean useDelegate, final boolean needsSorting, final Runnable setupTask, final Runnable clearTask) { diff --git a/src/main/java/li/cil/oc2/client/renderer/blockentity/InternetGateWayRenderer.java b/src/main/java/li/cil/oc2/client/renderer/blockentity/InternetGateWayRenderer.java new file mode 100644 index 00000000..322a253d --- /dev/null +++ b/src/main/java/li/cil/oc2/client/renderer/blockentity/InternetGateWayRenderer.java @@ -0,0 +1,113 @@ +package li.cil.oc2.client.renderer.blockentity; + +import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexConsumer; +import com.mojang.math.Matrix4f; +import com.mojang.math.Vector3f; + +import li.cil.oc2.client.renderer.ModRenderType; +import li.cil.oc2.common.blockentity.InternetGateWayBlockEntity; +import net.minecraft.client.renderer.MultiBufferSource; +import net.minecraft.client.renderer.RenderType; +import net.minecraft.client.renderer.blockentity.BlockEntityRenderer; +import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider; + +public class InternetGateWayRenderer implements BlockEntityRenderer { + private static final float BLOCK_HEIGHT = 14f/16f; + private static final float PORTAL_POSITION = 0.4f; + private static final float EMITTER_POSITION = -3f/16f; + private static final float EMITTER_SIZE = 6f/16f; + private static final float EMITTER_PIXEL_SIZE = EMITTER_SIZE / 9f; + private static final int[] SCRAMBLER = {1, 2, 0, 3, 13, 5, 15, 12, 6, 14, 10, 11, 7, 8, 9, 4}; + + public InternetGateWayRenderer(final BlockEntityRendererProvider.Context context) { + //this.renderer = context.getBlockEntityRenderDispatcher(); + } + + @Override + public void render(InternetGateWayBlockEntity gateWay, final float partialTicks, final PoseStack stack, final MultiBufferSource bufferSource, final int light, final int overlay) { + stack.pushPose(); + stack.translate(0.5f, BLOCK_HEIGHT, 0.5f); + stack.pushPose(); + long time = System.currentTimeMillis(); + long dt = time - gateWay.lastRender; + gateWay.lastRender = time; + if (dt > 1000) { + //Catch up if rendering stopped + gateWay.handledInboundCount = gateWay.inboundCount; + gateWay.handledOutboundCount = gateWay.outboundCount; + } + double phase = ((double)time)/1000d; + stack.translate(0f, PORTAL_POSITION+Math.sin(phase/2)*0.03f, 0f); + //stack.mulPose(Vector3f.XN.rotationDegrees((float)Math.sin(phase)*5)); + //stack.mulPose(Vector3f.ZN.rotationDegrees((float)Math.sin(phase+2)*5)); + VertexConsumer portal = bufferSource.getBuffer(RenderType.endPortal()); + Matrix4f matrix = stack.last().pose(); + float halfSide = 0.2f; + + renderCube(portal, matrix, halfSide, 0, 0, 0, false); + stack.popPose(); + stack.translate(0, EMITTER_POSITION, 0); + matrix = stack.last().pose(); + VertexConsumer packet = bufferSource.getBuffer(ModRenderType.getGateWayParticle()); + for (int x=0;x=1f) { + gateWay.pointer += 1; + if (gateWay.pointer>=InternetGateWayBlockEntity.EMITTER_SIDE_PIXELS*InternetGateWayBlockEntity.EMITTER_SIDE_PIXELS) { + gateWay.pointer = 0; + } + gateWay.animProgress[scrambledPointer] = 0f; + if (gateWay.handledInboundCount outboundQueue; private InternetConnection internetConnection; - + private static final String STATE_TAG = "internet_adapter"; private Tag internetState; private final FixedEnergyStorage energy = new FixedEnergyStorage(Config.gatewayEnergyStorage); + // Animation stuff + public static final int EMITTER_SIDE_PIXELS = 4; + public float animProgress[]; + public boolean animReversed[]; + public int inboundCount = 0; + public int outboundCount = 0; + public int handledInboundCount = 0; + public int handledOutboundCount = 0; + public long lastRender = 0; + public int pointer = 0; + protected InternetGateWayBlockEntity(final BlockPos pos, final BlockState state) { super(BlockEntities.INTERNET_GATEWAY.get(), pos, state); inboundQueue = new ArrayDeque<>(); outboundQueue = new ArrayDeque<>(); - internetState = null; + animProgress = new float[EMITTER_SIDE_PIXELS*EMITTER_SIDE_PIXELS]; + animReversed = new boolean[EMITTER_SIDE_PIXELS*EMITTER_SIDE_PIXELS]; + internetState = EndTag.INSTANCE; setNeedsLevelUnloadEvent(); } @@ -60,7 +78,37 @@ public class InternetGateWayBlockEntity extends ModBlockEntity implements Networ .ifPresent(adapterState -> tag.put(Constants.INTERNET_ADAPTER_TAG_NAME, adapterState)); } tag.put(Constants.ENERGY_TAG_NAME, energy.serializeNBT()); - LOGGER.info("State saved"); + LOGGER.trace("State saved"); + } + + @Override + public CompoundTag getUpdateTag() { + final CompoundTag tag = super.getUpdateTag(); + tag.putInt("inbound_count", inboundCount); + tag.putInt("outbound_count", outboundCount); + return tag; + } + + @Override + public void onDataPacket(Connection net, ClientboundBlockEntityDataPacket pkt) + { + CompoundTag compoundtag = pkt.getTag(); + if (compoundtag != null) { + handleUpdateTag(compoundtag); + } + } + + @Override + public void handleUpdateTag(final CompoundTag tag) { + inboundCount = tag.getInt("inbound_count"); + outboundCount = tag.getInt("outbound_count"); + handledInboundCount = Math.max(handledInboundCount, inboundCount-128); + handledOutboundCount = Math.max(handledOutboundCount, outboundCount-128); + } + + @Override + public Packet getUpdatePacket() { + return ClientboundBlockEntityDataPacket.create(this); } @Override @@ -68,16 +116,16 @@ public class InternetGateWayBlockEntity extends ModBlockEntity implements Networ InternetManagerImpl.getInstance() .ifPresent(internetManager -> internetConnection = internetManager.connect(this, internetState)); if (internetConnection != null) { - LOGGER.info("Connected to the internet"); + LOGGER.trace("Connected to the internet"); } else { - LOGGER.info("Not connected to the internet"); + LOGGER.trace("Not connected to the internet"); } } protected void unloadServer(final boolean isRemove) { if (internetConnection != null) { internetConnection.stop(); - LOGGER.info("Connection stopped"); + LOGGER.trace("Connection stopped"); } } @@ -96,15 +144,30 @@ public class InternetGateWayBlockEntity extends ModBlockEntity implements Networ boolean hasEnough = energy.getEnergyStored() >= Config.gatewayEnergyPerPacket; if (hasEnough) { energy.extractEnergy(Config.gatewayEnergyPerPacket, false); + Level level = getLevel(); + if (level != null) { + ChunkUtils.setLazyUnsaved(level, getBlockPos()); + } } return hasEnough; } + private void notifyPlayers() { + Level level = getLevel(); + if (level != null) { + level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 2); + //setChanged(); + LOGGER.info("Notified clients"); + } + } + @Override public void sendEthernetFrame(byte[] frame) { - LOGGER.info("Got inbound packet"); + LOGGER.trace("Got inbound packet"); if (inboundQueue.size() < QUEUE_MAX) { if (tryUseEnergy()) { + inboundCount += 1; + notifyPlayers(); inboundQueue.addLast(frame); } } @@ -117,12 +180,22 @@ public class InternetGateWayBlockEntity extends ModBlockEntity implements Networ @Override public void writeEthernetFrame(NetworkInterface source, byte[] frame, int timeToLive) { - LOGGER.info("Got outbound packet"); + LOGGER.trace("Got outbound packet"); if (outboundQueue.size() < QUEUE_MAX) { if (tryUseEnergy()) { + outboundCount += 1; + notifyPlayers(); outboundQueue.addLast(frame); } } } + @Override + public AABB getRenderBoundingBox() { + return new AABB( + getBlockPos(), + getBlockPos().offset(1, 2, 1) + ); + } + } diff --git a/src/main/resources/assets/oc2/blockstates/internet_gateway.json b/src/main/resources/assets/oc2/blockstates/internet_gateway.json new file mode 100644 index 00000000..b9ffcd77 --- /dev/null +++ b/src/main/resources/assets/oc2/blockstates/internet_gateway.json @@ -0,0 +1,7 @@ +{ + "variants": { + "": { + "model": "oc2:block/internet_gateway" + } + } + } \ No newline at end of file diff --git a/src/main/resources/assets/oc2/models/block/internet_gateway.json b/src/main/resources/assets/oc2/models/block/internet_gateway.json new file mode 100644 index 00000000..c4cfd8d7 --- /dev/null +++ b/src/main/resources/assets/oc2/models/block/internet_gateway.json @@ -0,0 +1,114 @@ +{ + "credit": "Made with Blockbench", + "parent": "block/block", + "texture_size": [32, 32], + "textures": { + "0": "oc2:block/internet_gateway/internet_gateway_atlas0", + "particle": "oc2:block/internet_gateway/internet_gateway_atlas0" + }, + "elements": [ + { + "from": [3, 0, 3], + "to": [13, 10, 13], + "faces": { + "north": {"uv": [0, 0, 5, 5], "texture": "#0"}, + "east": {"uv": [0, 0, 5, 5], "texture": "#0"}, + "south": {"uv": [0, 0, 5, 5], "texture": "#0"}, + "west": {"uv": [1.5, 1.5, 6.5, 6.5], "texture": "#0"}, + "up": {"uv": [1.5, 1.5, 6.5, 6.5], "texture": "#0"}, + "down": {"uv": [1.5, 1.5, 6.5, 6.5], "texture": "#0"} + } + }, + { + "from": [0, 0, 0], + "to": [16, 13, 3], + "faces": { + "north": {"uv": [8, 0, 16, 6.5], "texture": "#0"}, + "east": {"uv": [14.5, 0, 16, 6.5], "texture": "#0"}, + "south": {"uv": [0, 0, 8, 6.5], "texture": "#0"}, + "west": {"uv": [8, 0, 9.5, 6.5], "texture": "#0"}, + "up": {"uv": [0, 8, 8, 9.5], "texture": "#0"}, + "down": {"uv": [0, 6.5, 8, 8], "texture": "#0"} + } + }, + { + "from": [0, 0, 13], + "to": [16, 13, 16], + "faces": { + "north": {"uv": [0, 0, 8, 6.5], "texture": "#0"}, + "east": {"uv": [8, 0, 9.5, 6.5], "texture": "#0"}, + "south": {"uv": [8, 0, 16, 6.5], "texture": "#0"}, + "west": {"uv": [14.5, 0, 16, 6.5], "texture": "#0"}, + "up": {"uv": [0, 14.5, 8, 16], "texture": "#0"}, + "down": {"uv": [0, 0, 8, 1.5], "texture": "#0"} + } + }, + { + "from": [0, 0, 3], + "to": [3, 13, 13], + "faces": { + "north": {"uv": [0, 0, 1.5, 6.5], "texture": "#0"}, + "east": {"uv": [1.5, 0, 6.5, 6.5], "texture": "#0"}, + "south": {"uv": [0, 0, 1.5, 6.5], "texture": "#0"}, + "west": {"uv": [9.5, 0, 14.5, 6.5], "texture": "#0"}, + "up": {"uv": [0, 9.5, 1.5, 14.5], "texture": "#0"}, + "down": {"uv": [0, 1.5, 1.5, 6.5], "texture": "#0"} + } + }, + { + "from": [13, 0, 3], + "to": [16, 13, 13], + "faces": { + "north": {"uv": [0, 0, 1.5, 6.5], "texture": "#0"}, + "east": {"uv": [9.5, 0, 14.5, 6.5], "texture": "#0"}, + "south": {"uv": [0, 0, 1.5, 6.5], "texture": "#0"}, + "west": {"uv": [1.5, 0, 6.5, 6.5], "texture": "#0"}, + "up": {"uv": [6.5, 9.5, 8, 14.5], "texture": "#0"}, + "down": {"uv": [6.5, 1.5, 8, 6.5], "texture": "#0"} + } + }, + { + "from": [5, 10, 5], + "to": [11, 11, 11], + "faces": { + "north": {"uv": [3, 0, 6, 0.5], "texture": "#0"}, + "east": {"uv": [3, 0.5, 6, 1], "texture": "#0"}, + "south": {"uv": [3, 1, 6, 1.5], "texture": "#0"}, + "west": {"uv": [3, 1.5, 6, 2], "texture": "#0"}, + "up": {"uv": [16, 11, 11.5, 6.5], "texture": "#0"}, + "down": {"uv": [3, 3, 0, 6], "texture": "#0"} + } + } + ], + "display": { + "thirdperson_righthand": { + "rotation": [75, 45, 0], + "translation": [0, 2.5, 0], + "scale": [0.375, 0.375, 0.375] + }, + "thirdperson_lefthand": { + "rotation": [75, 45, 0], + "translation": [0, 2.5, 0], + "scale": [0.375, 0.375, 0.375] + }, + "firstperson_righthand": { + "rotation": [0, 45, 0], + "scale": [0.4, 0.4, 0.4] + }, + "firstperson_lefthand": { + "rotation": [0, 225, 0], + "scale": [0.4, 0.4, 0.4] + }, + "ground": { + "translation": [0, 3, 0], + "scale": [0.25, 0.25, 0.25] + }, + "gui": { + "rotation": [30, 225, 0], + "scale": [0.625, 0.625, 0.625] + }, + "fixed": { + "scale": [0.5, 0.5, 0.5] + } + } +} \ No newline at end of file diff --git a/src/main/resources/assets/oc2/models/item/internet_gateway.json b/src/main/resources/assets/oc2/models/item/internet_gateway.json new file mode 100644 index 00000000..7a96d6a3 --- /dev/null +++ b/src/main/resources/assets/oc2/models/item/internet_gateway.json @@ -0,0 +1,3 @@ +{ + "parent": "oc2:block/internet_gateway" +} \ No newline at end of file diff --git a/src/main/resources/assets/oc2/textures/block/internet_gateway/internet_gateway_atlas0.png b/src/main/resources/assets/oc2/textures/block/internet_gateway/internet_gateway_atlas0.png new file mode 100644 index 0000000000000000000000000000000000000000..7b6e7aad3c817ffec5a0ac3a49564ae24a0f0110 GIT binary patch literal 281 zcmV+!0p|XRP)VUar!)%{t7T1$s_>QTDEpA zX3%+n+!Suf7z0Wv&{}^*LnU~vX~4Y&c5 zULFhmYwzbZu;_x&*UXhtH%h5sB(H&LC*hh9F&LWy#B3?~FzNsufE-}ah7>=l9w2uh fRRKsHL^bsWGp%VH44swL00000NkvXXu0mjf=+tm_ literal 0 HcmV?d00001