From 70188889bebbe7506766b437cde3cd3f7fdc328d Mon Sep 17 00:00:00 2001 From: JozsefA Date: Fri, 23 Apr 2021 17:20:16 -0700 Subject: [PATCH] Persistence is key - Projectors now remember their setting and sync between clients --- .../projector/ChromaticProjectorScreen.java | 13 +-- .../ChromaticProjectorTileEntity.java | 41 +++++++-- .../projector/ConfigureProjectorPacket.java | 76 +++++++++++++++++ .../curiosities/projector/FilterStep.java | 39 ++++++++- .../foundation/networking/AllPackets.java | 84 +++++++++---------- .../render/backend/effects/FilterSphere.java | 2 +- 6 files changed, 192 insertions(+), 63 deletions(-) create mode 100644 src/main/java/com/simibubi/create/content/curiosities/projector/ConfigureProjectorPacket.java diff --git a/src/main/java/com/simibubi/create/content/curiosities/projector/ChromaticProjectorScreen.java b/src/main/java/com/simibubi/create/content/curiosities/projector/ChromaticProjectorScreen.java index ed898a196..a941f922b 100644 --- a/src/main/java/com/simibubi/create/content/curiosities/projector/ChromaticProjectorScreen.java +++ b/src/main/java/com/simibubi/create/content/curiosities/projector/ChromaticProjectorScreen.java @@ -12,28 +12,23 @@ import com.simibubi.create.foundation.gui.GuiGameElement; import com.simibubi.create.foundation.gui.widgets.IconButton; import com.simibubi.create.foundation.gui.widgets.ScrollInput; import com.simibubi.create.foundation.gui.widgets.SelectionScrollInput; +import com.simibubi.create.foundation.networking.AllPackets; import com.simibubi.create.foundation.tileEntity.behaviour.scrollvalue.ScrollValueBehaviour; import com.simibubi.create.foundation.utility.Lang; import net.minecraft.client.Minecraft; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.ListNBT; -import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.StringTextComponent; public class ChromaticProjectorScreen extends AbstractSimiScreen { - public static final int MAX_STEPS = 4; - private final ItemStack renderedItem = AllBlocks.CHROMATIC_PROJECTOR.asStack(); private final AllGuiTextures background = AllGuiTextures.PROJECTOR; private IconButton confirmButton; private final ITextComponent title = Lang.translate("gui.chromatic_projector.title"); - private ListNBT compareTag; private Vector stages; - private BlockPos pos; private Vector> inputs; @@ -47,8 +42,6 @@ public class ChromaticProjectorScreen extends AbstractSimiScreen { public ChromaticProjectorScreen(ChromaticProjectorTileEntity te) { this.tile = te; this.stages = te.stages; - this.pos = te.getPos(); - //compareTag = Instruction.serializeAll(stages); } private static Integer step(ScrollValueBehaviour.StepContext ctx, int base) { @@ -62,7 +55,7 @@ public class ChromaticProjectorScreen extends AbstractSimiScreen { super.init(); widgets.clear(); - inputs = new Vector<>(MAX_STEPS); + inputs = new Vector<>(FilterStep.MAX_STEPS); for (int row = 0; row < inputs.capacity(); row++) inputs.add(new Vector<>(2)); @@ -203,7 +196,7 @@ public class ChromaticProjectorScreen extends AbstractSimiScreen { } public void sendPacket() { - + AllPackets.channel.sendToServer(new ConfigureProjectorPacket(tile)); } @Override diff --git a/src/main/java/com/simibubi/create/content/curiosities/projector/ChromaticProjectorTileEntity.java b/src/main/java/com/simibubi/create/content/curiosities/projector/ChromaticProjectorTileEntity.java index d2ebe9f6f..338c2fd20 100644 --- a/src/main/java/com/simibubi/create/content/curiosities/projector/ChromaticProjectorTileEntity.java +++ b/src/main/java/com/simibubi/create/content/curiosities/projector/ChromaticProjectorTileEntity.java @@ -4,20 +4,23 @@ import java.util.Vector; import com.simibubi.create.foundation.render.backend.effects.FilterSphere; import com.simibubi.create.foundation.render.backend.instancing.IInstanceRendered; +import com.simibubi.create.foundation.tileEntity.SyncedTileEntity; -import net.minecraft.tileentity.TileEntity; +import net.minecraft.block.BlockState; +import net.minecraft.nbt.CompoundNBT; import net.minecraft.tileentity.TileEntityType; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.vector.Matrix4f; +import net.minecraftforge.common.util.Constants; -public class ChromaticProjectorTileEntity extends TileEntity implements IInstanceRendered { +public class ChromaticProjectorTileEntity extends SyncedTileEntity implements IInstanceRendered { Vector stages = FilterStep.createDefault(); - float radius = 10f; - float density = 1f; - float feather = 3; - float fade = 1.3f; + float radius = 3f; + float density = 1; + float feather = 1; + float fade = 1; boolean blend = true; public ChromaticProjectorTileEntity(TileEntityType te) { @@ -62,4 +65,30 @@ public class ChromaticProjectorTileEntity extends TileEntity implements IInstanc this.blend = blend; return this; } + + @Override + public CompoundNBT write(CompoundNBT tag) { + super.write(tag); + + tag.put("filters", FilterStep.writeAll(stages)); + + tag.putFloat("radius", radius); + tag.putFloat("density", density); + tag.putFloat("feather", feather); + tag.putFloat("fade", fade); + + return tag; + } + + @Override + public void fromTag(BlockState state, CompoundNBT tag) { + super.fromTag(state, tag); + + stages = FilterStep.readAll(tag.getList("filters", Constants.NBT.TAG_COMPOUND)); + + radius = tag.getFloat("radius"); + density = tag.getFloat("density"); + feather = tag.getFloat("feather"); + fade = tag.getFloat("fade"); + } } diff --git a/src/main/java/com/simibubi/create/content/curiosities/projector/ConfigureProjectorPacket.java b/src/main/java/com/simibubi/create/content/curiosities/projector/ConfigureProjectorPacket.java new file mode 100644 index 000000000..be616532b --- /dev/null +++ b/src/main/java/com/simibubi/create/content/curiosities/projector/ConfigureProjectorPacket.java @@ -0,0 +1,76 @@ +package com.simibubi.create.content.curiosities.projector; + +import java.util.Vector; +import java.util.stream.Collectors; + +import com.simibubi.create.foundation.networking.TileEntityConfigurationPacket; + +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.network.PacketBuffer; + +public class ConfigureProjectorPacket extends TileEntityConfigurationPacket { + + Vector stages; + float radius; + float density; + float feather; + float fade; + + public ConfigureProjectorPacket(PacketBuffer buffer) { + super(buffer); + } + + public ConfigureProjectorPacket(ChromaticProjectorTileEntity tile) { + super(tile.getPos()); + + stages = tile.stages.stream() + .map(FilterStep::write) + .collect(Collectors.toCollection(Vector::new)); + this.radius = tile.radius; + this.density = tile.density; + this.feather = tile.feather; + this.fade = tile.fade; + } + + @Override + protected void writeSettings(PacketBuffer buffer) { + buffer.writeFloat(radius); + buffer.writeFloat(density); + buffer.writeFloat(feather); + buffer.writeFloat(fade); + + buffer.writeInt(stages.size()); + for (CompoundNBT stage : stages) { + buffer.writeCompoundTag(stage); + } + } + + @Override + protected void readSettings(PacketBuffer buffer) { + radius = buffer.readFloat(); + density = buffer.readFloat(); + feather = buffer.readFloat(); + fade = buffer.readFloat(); + + int count = buffer.readInt(); + stages = new Vector<>(FilterStep.MAX_STEPS); + + for (int i = 0; i < count; i++) { + stages.add(buffer.readCompoundTag()); + } + } + + @Override + protected void applySettings(ChromaticProjectorTileEntity tile) { + tile.stages = stages.stream() + .map(FilterStep::new) + .collect(Collectors.toCollection(Vector::new)); + + tile.radius = this.radius; + tile.density = this.density; + tile.feather = this.feather; + tile.fade = this.fade; + + tile.sendData(); + } +} diff --git a/src/main/java/com/simibubi/create/content/curiosities/projector/FilterStep.java b/src/main/java/com/simibubi/create/content/curiosities/projector/FilterStep.java index e2c900236..a02d3bede 100644 --- a/src/main/java/com/simibubi/create/content/curiosities/projector/FilterStep.java +++ b/src/main/java/com/simibubi/create/content/curiosities/projector/FilterStep.java @@ -5,10 +5,13 @@ import java.util.Vector; import com.simibubi.create.foundation.render.backend.effects.ColorMatrices; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.nbt.ListNBT; import net.minecraft.util.math.vector.Matrix4f; public class FilterStep { + public static final int MAX_STEPS = 4; ColorEffect filter; int value; @@ -21,10 +24,44 @@ public class FilterStep { this.value = value; } + public FilterStep(CompoundNBT nbt) { + this.filter = ColorEffect.lookup.get(nbt.getString("id")); + this.value = nbt.getInt("value"); + } + public Matrix4f createFilter() { return filter.filter.create(value / filter.divisor); } + public CompoundNBT write() { + CompoundNBT nbt = new CompoundNBT(); + + nbt.putString("id", filter.name); + nbt.putInt("value", value); + + return nbt; + } + + public static Vector readAll(ListNBT list) { + Vector steps = new Vector<>(MAX_STEPS); + + for (int i = 0; i < list.size(); i++) { + steps.add(new FilterStep(list.getCompound(i))); + } + + return steps; + } + + public static ListNBT writeAll(Vector filters) { + ListNBT out = new ListNBT(); + + for (FilterStep filter : filters) { + out.add(filter.write()); + } + + return out; + } + public static Matrix4f fold(Vector filters) { Iterator stepIterator = filters.stream().filter(it -> it != null && it.filter != ColorEffect.END).iterator(); @@ -40,7 +77,7 @@ public class FilterStep { } public static Vector createDefault() { - Vector instructions = new Vector<>(ChromaticProjectorScreen.MAX_STEPS); + Vector instructions = new Vector<>(MAX_STEPS); instructions.add(new FilterStep(ColorEffect.SEPIA, 100)); instructions.add(new FilterStep(ColorEffect.END)); return instructions; diff --git a/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java b/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java index 3e417d17f..667ac2f5d 100644 --- a/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java +++ b/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java @@ -1,49 +1,5 @@ package com.simibubi.create.foundation.networking; -import static net.minecraftforge.fml.network.NetworkDirection.PLAY_TO_CLIENT; -import static net.minecraftforge.fml.network.NetworkDirection.PLAY_TO_SERVER; - -import java.util.function.BiConsumer; -import java.util.function.Function; -import java.util.function.Supplier; - -import com.simibubi.create.Create; -import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionDisassemblyPacket; -import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionStallPacket; -import com.simibubi.create.content.contraptions.components.structureMovement.gantry.GantryContraptionUpdatePacket; -import com.simibubi.create.content.contraptions.components.structureMovement.glue.GlueEffectPacket; -import com.simibubi.create.content.contraptions.components.structureMovement.sync.ClientMotionPacket; -import com.simibubi.create.content.contraptions.components.structureMovement.sync.ContraptionFluidPacket; -import com.simibubi.create.content.contraptions.components.structureMovement.sync.ContraptionInteractionPacket; -import com.simibubi.create.content.contraptions.components.structureMovement.sync.ContraptionSeatMappingPacket; -import com.simibubi.create.content.contraptions.components.structureMovement.sync.LimbSwingUpdatePacket; -import com.simibubi.create.content.contraptions.components.structureMovement.train.CouplingCreationPacket; -import com.simibubi.create.content.contraptions.components.structureMovement.train.capability.MinecartControllerUpdatePacket; -import com.simibubi.create.content.contraptions.fluids.actors.FluidSplashPacket; -import com.simibubi.create.content.contraptions.relays.advanced.sequencer.ConfigureSequencedGearshiftPacket; -import com.simibubi.create.content.curiosities.symmetry.SymmetryEffectPacket; -import com.simibubi.create.content.curiosities.tools.ExtendoGripInteractionPacket; -import com.simibubi.create.content.curiosities.zapper.ZapperBeamPacket; -import com.simibubi.create.content.logistics.block.depot.EjectorElytraPacket; -import com.simibubi.create.content.logistics.block.depot.EjectorPlacementPacket; -import com.simibubi.create.content.logistics.block.depot.EjectorTriggerPacket; -import com.simibubi.create.content.logistics.block.mechanicalArm.ArmPlacementPacket; -import com.simibubi.create.content.logistics.item.filter.FilterScreenPacket; -import com.simibubi.create.content.logistics.packet.ConfigureFlexcratePacket; -import com.simibubi.create.content.logistics.packet.ConfigureStockswitchPacket; -import com.simibubi.create.content.logistics.packet.FunnelFlapPacket; -import com.simibubi.create.content.logistics.packet.TunnelFlapPacket; -import com.simibubi.create.content.schematics.packet.ConfigureSchematicannonPacket; -import com.simibubi.create.content.schematics.packet.InstantSchematicPacket; -import com.simibubi.create.content.schematics.packet.SchematicPlacePacket; -import com.simibubi.create.content.schematics.packet.SchematicSyncPacket; -import com.simibubi.create.content.schematics.packet.SchematicUploadPacket; -import com.simibubi.create.foundation.command.ConfigureConfigPacket; -import com.simibubi.create.foundation.command.HighlightPacket; -import com.simibubi.create.foundation.tileEntity.behaviour.filtering.FilteringCountUpdatePacket; -import com.simibubi.create.foundation.tileEntity.behaviour.scrollvalue.ScrollValueUpdatePacket; -import com.simibubi.create.foundation.utility.ServerSpeedProvider; - import net.minecraft.network.PacketBuffer; import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; @@ -55,6 +11,43 @@ import net.minecraftforge.fml.network.PacketDistributor; import net.minecraftforge.fml.network.PacketDistributor.TargetPoint; import net.minecraftforge.fml.network.simple.SimpleChannel; +import java.util.function.BiConsumer; +import java.util.function.Function; +import java.util.function.Supplier; + +import com.simibubi.create.Create; +import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionDisassemblyPacket; +import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionStallPacket; +import com.simibubi.create.content.contraptions.components.structureMovement.gantry.GantryContraptionUpdatePacket; +import com.simibubi.create.content.contraptions.components.structureMovement.glue.GlueEffectPacket; +import com.simibubi.create.content.contraptions.components.structureMovement.sync.*; +import com.simibubi.create.content.contraptions.components.structureMovement.train.CouplingCreationPacket; +import com.simibubi.create.content.contraptions.components.structureMovement.train.capability.MinecartControllerUpdatePacket; +import com.simibubi.create.content.contraptions.fluids.actors.FluidSplashPacket; +import com.simibubi.create.content.contraptions.relays.advanced.sequencer.ConfigureSequencedGearshiftPacket; +import com.simibubi.create.content.curiosities.projector.ConfigureProjectorPacket; +import com.simibubi.create.content.curiosities.symmetry.SymmetryEffectPacket; +import com.simibubi.create.content.curiosities.tools.ExtendoGripInteractionPacket; +import com.simibubi.create.content.curiosities.zapper.ZapperBeamPacket; +import com.simibubi.create.content.logistics.block.depot.EjectorElytraPacket; +import com.simibubi.create.content.logistics.block.depot.EjectorPlacementPacket; +import com.simibubi.create.content.logistics.block.depot.EjectorTriggerPacket; +import com.simibubi.create.content.logistics.block.mechanicalArm.ArmPlacementPacket; +import com.simibubi.create.content.logistics.item.filter.FilterScreenPacket; +import com.simibubi.create.content.logistics.packet.ConfigureFlexcratePacket; +import com.simibubi.create.content.logistics.packet.ConfigureStockswitchPacket; +import com.simibubi.create.content.logistics.packet.FunnelFlapPacket; +import com.simibubi.create.content.logistics.packet.TunnelFlapPacket; +import com.simibubi.create.content.schematics.packet.*; +import com.simibubi.create.foundation.command.ConfigureConfigPacket; +import com.simibubi.create.foundation.command.HighlightPacket; +import com.simibubi.create.foundation.tileEntity.behaviour.filtering.FilteringCountUpdatePacket; +import com.simibubi.create.foundation.tileEntity.behaviour.scrollvalue.ScrollValueUpdatePacket; +import com.simibubi.create.foundation.utility.ServerSpeedProvider; + +import static net.minecraftforge.fml.network.NetworkDirection.PLAY_TO_CLIENT; +import static net.minecraftforge.fml.network.NetworkDirection.PLAY_TO_SERVER; + public enum AllPackets { // Client to Server @@ -63,7 +56,8 @@ public enum AllPackets { CONFIGURE_FLEXCRATE(ConfigureFlexcratePacket.class, ConfigureFlexcratePacket::new, PLAY_TO_SERVER), CONFIGURE_STOCKSWITCH(ConfigureStockswitchPacket.class, ConfigureStockswitchPacket::new, PLAY_TO_SERVER), CONFIGURE_SEQUENCER(ConfigureSequencedGearshiftPacket.class, ConfigureSequencedGearshiftPacket::new, - PLAY_TO_SERVER), + PLAY_TO_SERVER), + CONFIGURE_PROJECTOR(ConfigureProjectorPacket.class, ConfigureProjectorPacket::new, PLAY_TO_SERVER), PLACE_SCHEMATIC(SchematicPlacePacket.class, SchematicPlacePacket::new, PLAY_TO_SERVER), UPLOAD_SCHEMATIC(SchematicUploadPacket.class, SchematicUploadPacket::new, PLAY_TO_SERVER), CONFIGURE_FILTER(FilterScreenPacket.class, FilterScreenPacket::new, PLAY_TO_SERVER), diff --git a/src/main/java/com/simibubi/create/foundation/render/backend/effects/FilterSphere.java b/src/main/java/com/simibubi/create/foundation/render/backend/effects/FilterSphere.java index 77aa77123..6cf354755 100644 --- a/src/main/java/com/simibubi/create/foundation/render/backend/effects/FilterSphere.java +++ b/src/main/java/com/simibubi/create/foundation/render/backend/effects/FilterSphere.java @@ -85,7 +85,7 @@ public class FilterSphere { 0.5f, //r, 0.1f, //g, 0.1f, //b, - 0.12f, //colorFeather, + 0f, //colorFeather, }); buf.put(RenderUtil.writeMatrix(filter));