Skip to content

Commit

Permalink
Added a null check to URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragonoidzero committed Feb 19, 2021
1 parent 9fefadb commit 1830d68
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 51 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/azzy/fabric/lookingglass/LookingGlassClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
})));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<ServerPlayerEntity> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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<EnergyIo> implements EnergyIo {

Expand All @@ -26,35 +25,72 @@ public PowerPipeEntity(BlockEntityType type, double transferRate) {

@Override
public void performTransfers(Set<EnergyIo> participants) {
EnergyIo[] io = participants.toArray(new EnergyIo[0]);
List<EnergyIo> 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<PowerPipeEntity> 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;
power += transfer;
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<PowerPipeEntity> getNeighbours() {
Set<PowerPipeEntity> 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<PowerPipeEntity> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@

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 {

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);
});
})));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) -> {
Expand Down Expand Up @@ -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();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 1830d68

Please sign in to comment.