From 1830d686ab30b08f286956028ea8787555910293 Mon Sep 17 00:00:00 2001 From: Dragonoidzero Date: Thu, 18 Feb 2021 21:37:06 -0600 Subject: [PATCH] Added a null check to URLs --- gradle.properties | 2 +- .../lookingglass/LookingGlassClient.java | 11 --- .../lookingglass/LookingGlassCommon.java | 4 - .../lookingglass/block/GrowthCoreBlock.java | 9 +-- .../blockentity/PowerPipeEntity.java | 74 ++++++++++++++----- .../render/ProjectorRenderer.java | 10 ++- .../util/ClientNetworkingUtils.java | 16 +++- .../lookingglass/util/GeneralNetworking.java | 10 ++- .../lookingglass/util/client/RenderCache.java | 14 +++- src/main/resources/fabric.mod.json | 2 +- 10 files changed, 101 insertions(+), 51 deletions(-) diff --git a/gradle.properties b/gradle.properties index 63ae980..6331957 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx4G loader_version=0.11.1 # Mod Properties - mod_version = ALPHA.3 + mod_version = ALPHA.4 maven_group = azzy.ttlg archives_base_name = TTLG libgui_version = 3.3.3+1.16.5 diff --git a/src/main/java/azzy/fabric/lookingglass/LookingGlassClient.java b/src/main/java/azzy/fabric/lookingglass/LookingGlassClient.java index ab2d111..f213a34 100644 --- a/src/main/java/azzy/fabric/lookingglass/LookingGlassClient.java +++ b/src/main/java/azzy/fabric/lookingglass/LookingGlassClient.java @@ -56,16 +56,5 @@ public void onInitializeClient() { // layerCleanLock = RenderCache.cleanLayerCache(); // } //}); - - ClientSidePacketRegistry.INSTANCE.register(BLOCKPOS_TO_CLIENT_PACKET, (((packetContext, packetByteBuf) -> { - - BlockPos pos = packetByteBuf.readBlockPos(); - int count = packetByteBuf.readInt(); - World world = packetContext.getPlayer().getEntityWorld(); - - packetContext.getTaskQueue().execute(() -> { - BoneMealItem.createParticles(world, pos, count); - }); - }))); } } diff --git a/src/main/java/azzy/fabric/lookingglass/LookingGlassCommon.java b/src/main/java/azzy/fabric/lookingglass/LookingGlassCommon.java index 12254aa..3f57e4f 100644 --- a/src/main/java/azzy/fabric/lookingglass/LookingGlassCommon.java +++ b/src/main/java/azzy/fabric/lookingglass/LookingGlassCommon.java @@ -51,10 +51,6 @@ public class LookingGlassCommon implements ModInitializer { public static final boolean DEV_ENV = FabricLoader.getInstance().isDevelopmentEnvironment(); public static final boolean REGEN_RECIPES = false, REGEN_ITEMS = false, REGEN_BLOCKS = false, REGEN_LOOT = false; - public static final Identifier STRING_TO_SERVER_PACKET = new Identifier(MODID, "stringtoserver"); - public static final Identifier DOUBLES_TO_SERVER_PACKET = new Identifier(MODID, "doubletoserver"); - public static final Identifier BLOCKPOS_TO_CLIENT_PACKET = new Identifier(MODID, "postoclient"); - @Override public void onInitialize() { FFLog.info(LookingGlassInit.BLESSED_CONST); diff --git a/src/main/java/azzy/fabric/lookingglass/block/GrowthCoreBlock.java b/src/main/java/azzy/fabric/lookingglass/block/GrowthCoreBlock.java index ae85e02..0f78084 100644 --- a/src/main/java/azzy/fabric/lookingglass/block/GrowthCoreBlock.java +++ b/src/main/java/azzy/fabric/lookingglass/block/GrowthCoreBlock.java @@ -1,10 +1,12 @@ package azzy.fabric.lookingglass.block; import azzy.fabric.lookingglass.LookingGlassCommon; +import azzy.fabric.lookingglass.util.GeneralNetworking; import io.netty.buffer.Unpooled; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.fabric.api.network.ServerSidePacketRegistry; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.Fertilizable; @@ -63,10 +65,7 @@ public static void sendGrowthParticlePacket(ServerWorld world, BlockPos pos, int PacketByteBuf byteBuf = new PacketByteBuf(Unpooled.buffer()); byteBuf.writeBlockPos(pos); byteBuf.writeInt(count); - List players = world.getPlayers(); - for (ServerPlayerEntity player : players) { - if(player.getBlockPos().isWithinDistance(pos, 64.0D)) - ServerSidePacketRegistry.INSTANCE.sendToPlayer(player, LookingGlassCommon.BLOCKPOS_TO_CLIENT_PACKET, byteBuf); - } + world.getPlayers(player -> world.isPlayerInRange(pos.getX(), pos.getY(), pos.getZ(), 64)) + .forEach(player -> ServerPlayNetworking.send(player, GeneralNetworking.BONEMEAL_PARTICLES, byteBuf)); } } diff --git a/src/main/java/azzy/fabric/lookingglass/blockentity/PowerPipeEntity.java b/src/main/java/azzy/fabric/lookingglass/blockentity/PowerPipeEntity.java index cbb18fd..ba0d132 100644 --- a/src/main/java/azzy/fabric/lookingglass/blockentity/PowerPipeEntity.java +++ b/src/main/java/azzy/fabric/lookingglass/blockentity/PowerPipeEntity.java @@ -1,17 +1,16 @@ package azzy.fabric.lookingglass.blockentity; -import azzy.fabric.lookingglass.block.TTLGBlocks; import dev.technici4n.fasttransferlib.api.Simulation; import dev.technici4n.fasttransferlib.api.energy.EnergyApi; import dev.technici4n.fasttransferlib.api.energy.EnergyIo; import net.minecraft.block.BlockState; +import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntityType; import net.minecraft.nbt.CompoundTag; +import net.minecraft.util.math.Direction; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Set; +import java.util.*; +import java.util.stream.Stream; public class PowerPipeEntity extends BasePipeEntity implements EnergyIo { @@ -26,22 +25,23 @@ public PowerPipeEntity(BlockEntityType type, double transferRate) { @Override public void performTransfers(Set participants) { - EnergyIo[] io = participants.toArray(new EnergyIo[0]); - List ioList = Arrays.asList(io); - Collections.shuffle(Arrays.asList(io)); - io = ioList.toArray(new EnergyIo[0]); - for (EnergyIo energyIo : io) { - boolean pipe = energyIo instanceof PowerPipeEntity; - if(power > 0 && energyIo.supportsInsertion()) { - if(!(pipe && energyIo.getEnergy() > power)) { - double selfDrain = extract(transferRate, Simulation.SIMULATE); - double cap = energyIo.insert(selfDrain, Simulation.SIMULATE); - double transfer = selfDrain - cap; - power -= transfer; - energyIo.insert(transfer, Simulation.ACT); + List cables = new ArrayList<>(); + cables.add(this); + for (EnergyIo energyIo : participants.toArray(new EnergyIo[0])) { + if(energyIo instanceof PowerPipeEntity) { + if(((PowerPipeEntity) energyIo).getType() == getType()) { + cables.add((PowerPipeEntity) energyIo); } + continue; + } + if(power > 0 && energyIo.supportsInsertion()) { + double selfDrain = extract(transferRate, Simulation.SIMULATE); + double cap = energyIo.insert(selfDrain, Simulation.SIMULATE); + double transfer = selfDrain - cap; + power -= transfer; + energyIo.insert(transfer, Simulation.ACT); } - else if (!pipe && energyIo.getEnergy() > 0 && energyIo.supportsExtraction()) { + else if (energyIo.getEnergy() > 0 && energyIo.supportsExtraction()) { double succ = energyIo.extract(transferRate, Simulation.SIMULATE); double selfCap = insert(succ, Simulation.SIMULATE); double transfer = succ - selfCap; @@ -49,12 +49,48 @@ else if (!pipe && energyIo.getEnergy() > 0 && energyIo.supportsExtraction()) { energyIo.insert(transfer, Simulation.ACT); } } + + //Taken from Techreborn's CableBlockEntity + + if(cables.size() > 1) { + double totalPower = cables.stream().mapToDouble(EnergyIo::getEnergy).sum(); + double distributedpower = totalPower / cables.size(); + + cables.forEach(cable -> cable.setEnergy(distributedpower)); + } + markDirty(); if(!world.isClient()) { sync(); } } + public Set getNeighbours() { + Set neighbours = new HashSet<>(); + neighbours.add(this); + for(Direction direction : Direction.values()) { + BlockEntity entity = world.getBlockEntity(pos.offset(direction)); + if(entity != null && entity.getType() == getType()) + ((PowerPipeEntity)entity).getNeighbours(neighbours); + } + return neighbours; + } + + public void getNeighbours(Set neighbours) { + neighbours.add(this); + Arrays.stream(Direction.values()) + .map(direction -> world.getBlockEntity(pos.offset(direction))) + .filter(blockEntity -> blockEntity.getType() == getType()) + .forEach(entity -> { + if(!neighbours.contains(entity)) + ((PowerPipeEntity)entity).getNeighbours(neighbours); + }); + } + + private void setEnergy(double power) { + this.power = power; + } + @Override public double getEnergyCapacity() { return maxPower; diff --git a/src/main/java/azzy/fabric/lookingglass/render/ProjectorRenderer.java b/src/main/java/azzy/fabric/lookingglass/render/ProjectorRenderer.java index e520319..9b816c0 100644 --- a/src/main/java/azzy/fabric/lookingglass/render/ProjectorRenderer.java +++ b/src/main/java/azzy/fabric/lookingglass/render/ProjectorRenderer.java @@ -110,6 +110,12 @@ public void render(ProjectorEntity blockEntity, float tickDelta, MatrixStack mat } final RenderLayer cursedLayer = getRenderLayer(rawUrl, blockEntity.getWorld()); + + if(cursedLayer == null) { + matrices.pop(); + return; + } + VertexConsumer consumer = vertexConsumers.getBuffer(cursedLayer); MatrixStack.Entry matrix = matrices.peek(); @@ -206,7 +212,9 @@ public RenderLayer getRenderLayer(String url, World world){ renderLayer = RenderCache.getBakedLayer(url).getRenderLayer(); } else { - return RenderCache.bakeRenderLayer(url, texture).getRenderLayer(); + RenderCache.BakedRenderLayer layer = RenderCache.bakeRenderLayer(url, texture); + if(layer != null) + return layer.getRenderLayer(); } } return renderLayer; diff --git a/src/main/java/azzy/fabric/lookingglass/util/ClientNetworkingUtils.java b/src/main/java/azzy/fabric/lookingglass/util/ClientNetworkingUtils.java index 2b5a8f1..9a2a410 100644 --- a/src/main/java/azzy/fabric/lookingglass/util/ClientNetworkingUtils.java +++ b/src/main/java/azzy/fabric/lookingglass/util/ClientNetworkingUtils.java @@ -2,10 +2,10 @@ import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.minecraft.client.MinecraftClient; -import net.minecraft.client.resource.language.I18n; -import net.minecraft.text.LiteralText; +import net.minecraft.item.BoneMealItem; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; +import net.minecraft.util.math.BlockPos; public class ClientNetworkingUtils { @@ -13,12 +13,22 @@ public static void init() { ClientPlayNetworking.registerGlobalReceiver(GeneralNetworking.TRANSLATEABLE_PACKET, ((minecraftClient, clientPlayNetworkHandler, packetByteBuf, packetSender) -> { - String translationKey = packetByteBuf.readString(); + final String translationKey = packetByteBuf.readString(); minecraftClient.execute(() -> { Text text = new TranslatableText(translationKey); MinecraftClient.getInstance().player.sendMessage(text, true); }); })); + + ClientPlayNetworking.registerGlobalReceiver(GeneralNetworking.BONEMEAL_PARTICLES, (((minecraftClient, clientPlayNetworkHandler, packetByteBuf, packetSender) -> { + + final BlockPos pos = packetByteBuf.readBlockPos(); + final int count = packetByteBuf.readInt(); + + minecraftClient.execute(() -> { + BoneMealItem.createParticles(minecraftClient.world, pos, count); + }); + }))); } } diff --git a/src/main/java/azzy/fabric/lookingglass/util/GeneralNetworking.java b/src/main/java/azzy/fabric/lookingglass/util/GeneralNetworking.java index 2aa2398..3fdff93 100644 --- a/src/main/java/azzy/fabric/lookingglass/util/GeneralNetworking.java +++ b/src/main/java/azzy/fabric/lookingglass/util/GeneralNetworking.java @@ -12,8 +12,12 @@ public class GeneralNetworking { + //BE Sync + public static final Identifier PROJECTOR_SYNC_PACKET = new Identifier(MODID, "p_projector_sync"); + + //Client actions public static final Identifier TRANSLATEABLE_PACKET = new Identifier(MODID, "p_translateable"); - public static final Identifier PROJECTOR_SYNC_PACKET = new Identifier(MODID, "p_general"); + public static final Identifier BONEMEAL_PARTICLES = new Identifier(MODID, "p_meal_particles"); public static void init() { ServerPlayNetworking.registerGlobalReceiver(PROJECTOR_SYNC_PACKET, ((minecraftServer, serverPlayerEntity, serverPlayNetworkHandler, packet, packetSender) -> { @@ -41,10 +45,10 @@ public static void init() { projector.disX = disX; projector.disY = disY; projector.disZ = disZ; - projector.scale =scale; + projector.scale = scale; projector.url = url; projector.sign = sign ; - projector.color =color; + projector.color = color; projector.sync(); } }); diff --git a/src/main/java/azzy/fabric/lookingglass/util/client/RenderCache.java b/src/main/java/azzy/fabric/lookingglass/util/client/RenderCache.java index bc4a636..d2fb07f 100644 --- a/src/main/java/azzy/fabric/lookingglass/util/client/RenderCache.java +++ b/src/main/java/azzy/fabric/lookingglass/util/client/RenderCache.java @@ -49,8 +49,9 @@ static private Identifier generateId() { public static BakedRenderLayer bakeRenderLayer(String url, NativeImageBackedTexture texture) { Identifier backingTextureId = generateId(); - BakedRenderLayer bakedRenderLayer = new BakedRenderLayer(backingTextureId, texture); - bakedLayerCache.put(url, bakedRenderLayer); + BakedRenderLayer bakedRenderLayer = BakedRenderLayer.of(backingTextureId, texture); + if(bakedRenderLayer != null) + bakedLayerCache.put(url, bakedRenderLayer); return bakedRenderLayer; } @@ -88,13 +89,20 @@ public static class BakedRenderLayer implements Closeable { private final NativeImageBackedTexture texture; private long lastTickStamp; - public BakedRenderLayer(Identifier id, NativeImageBackedTexture texture) { + private BakedRenderLayer(Identifier id, NativeImageBackedTexture texture) { this.id = id; this.texture = texture; MinecraftClient.getInstance().getTextureManager().registerTexture(id, texture); this.renderLayer = TTLGRenderLayers.getTransNoDiff(id); } + public static BakedRenderLayer of(Identifier id, NativeImageBackedTexture texture) { + if(id != null && texture != null) { + return new BakedRenderLayer(id, texture); + } + return null; + } + public Identifier getId() { return id; } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 5ed3cca..75aa9ee 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -1,7 +1,7 @@ { "schemaVersion": 1, "id": "lookingglass", - "version": "2.0.0-alpha.3", + "version": "2.0.0-alpha.4", "name": "Through the Looking Glass", "description": "Not a mod about glass working",