From 9e950f9c866ce17000d1a997dcba6f3d9dd44fdf Mon Sep 17 00:00:00 2001 From: screret <68943070+screret@users.noreply.github.com> Date: Thu, 2 Nov 2023 11:08:53 +0200 Subject: [PATCH] space station launching thing --- .../api/capability/ISpaceStationHolder.java | 5 +- .../space/station/StationWorldSavedData.java | 9 ++- .../gcyr/common/entity/RocketEntity.java | 67 +++++++++++++++---- .../gcyr/common/item/KeyCardBehaviour.java | 11 +-- .../common/item/PlanetIdChipBehaviour.java | 9 +-- .../SpaceStationPackagerMachine.java | 16 +++-- .../common/worldgen/SpaceLevelSource.java | 6 +- fabric/build.gradle | 2 +- .../gcyr/fabric/mixin/MenuEntryMixin.java | 38 +++++++++++ fabric/src/main/resources/gcyr.mixins.json | 1 + forge/build.gradle | 2 +- 11 files changed, 132 insertions(+), 34 deletions(-) create mode 100644 fabric/src/main/java/argent_matter/gcyr/fabric/mixin/MenuEntryMixin.java diff --git a/common/src/main/java/argent_matter/gcyr/api/capability/ISpaceStationHolder.java b/common/src/main/java/argent_matter/gcyr/api/capability/ISpaceStationHolder.java index beed8bb0..211976d8 100644 --- a/common/src/main/java/argent_matter/gcyr/api/capability/ISpaceStationHolder.java +++ b/common/src/main/java/argent_matter/gcyr/api/capability/ISpaceStationHolder.java @@ -34,6 +34,7 @@ public interface ISpaceStationHolder { * @param id the station's id * @return the center of this station */ + @Nullable Vec2i getStationPos(int id); /** @@ -41,8 +42,10 @@ public interface ISpaceStationHolder { * @param id the station's id * @return this station. */ - SpaceStation getStation(int id); + @Nullable + SpaceStation getStation(@Nullable Integer id); + @Nullable BlockPos getStationWorldPos(int id); /** diff --git a/common/src/main/java/argent_matter/gcyr/api/space/station/StationWorldSavedData.java b/common/src/main/java/argent_matter/gcyr/api/space/station/StationWorldSavedData.java index 1122baf1..5d7ab06c 100644 --- a/common/src/main/java/argent_matter/gcyr/api/space/station/StationWorldSavedData.java +++ b/common/src/main/java/argent_matter/gcyr/api/space/station/StationWorldSavedData.java @@ -66,21 +66,26 @@ public int getClosestStationId(Vec2i position) { return result == null ? -1 : result.getIntKey(); } + @Nullable @Override public Vec2i getStationPos(int id) { var station = stations.get(id); - if (station == null) return Vec2i.MAX_NEGATIVE; + if (station == null) return null; return station.position(); } + @Nullable @Override - public SpaceStation getStation(int id) { + public SpaceStation getStation(@Nullable Integer id) { + if (id == null) return null; return stations.get(id); } + @Nullable @Override public BlockPos getStationWorldPos(int id) { Vec2i stationPos = getStationPos(id); + if (stationPos == null) return null; return new BlockPos(stationPos.x() * 16 * 16, SpaceLevelSource.PLATFORM_HEIGHT, stationPos.y() * 16 * 16); } diff --git a/common/src/main/java/argent_matter/gcyr/common/entity/RocketEntity.java b/common/src/main/java/argent_matter/gcyr/common/entity/RocketEntity.java index f7734e5b..76f1f1a7 100644 --- a/common/src/main/java/argent_matter/gcyr/common/entity/RocketEntity.java +++ b/common/src/main/java/argent_matter/gcyr/common/entity/RocketEntity.java @@ -42,9 +42,13 @@ import net.minecraft.MethodsReturnNonnullByDefault; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; +import net.minecraft.core.GlobalPos; +import net.minecraft.core.Registry; import net.minecraft.core.particles.ParticleTypes; +import net.minecraft.core.registries.Registries; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.NbtUtils; import net.minecraft.nbt.Tag; import net.minecraft.network.chat.Component; import net.minecraft.network.protocol.Packet; @@ -52,6 +56,8 @@ import net.minecraft.network.syncher.EntityDataAccessor; import net.minecraft.network.syncher.EntityDataSerializers; import net.minecraft.network.syncher.SynchedEntityData; +import net.minecraft.resources.ResourceKey; +import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.sounds.SoundSource; @@ -80,7 +86,6 @@ import java.util.List; import java.util.Set; -@SuppressWarnings("resource") @MethodsReturnNonnullByDefault @ParametersAreNonnullByDefault public class RocketEntity extends Entity implements HasCustomInventoryScreen, IUIHolder { @@ -95,7 +100,7 @@ public class RocketEntity extends Entity implements HasCustomInventoryScreen, IU private final FluidStorage fuelTank; - private final ItemStackTransfer configSlot; + private final ItemStackTransfer configSlot, satelliteSlot; private Planet destination; private boolean destinationIsSpaceStation; private final Object2IntMap partCounts = new Object2IntOpenHashMap<>(); @@ -108,6 +113,8 @@ public RocketEntity(EntityType entityType, Level level) { super(entityType, level); this.configSlot = new ItemStackTransfer(1); this.configSlot.setFilter(stack -> GCyRItems.ID_CHIP.isIn(stack) || GCyRItems.KEYCARD.isIn(stack)); + this.satelliteSlot = new ItemStackTransfer(1); + this.satelliteSlot.setFilter(stack -> GCyRItems.SPACE_STATION_PACKAGE.isIn(stack) || stack.is(GCyRTags.SATELLITES)); //noinspection deprecation this.fuelTank = new FluidStorage(0, fluid -> fluid.getFluid().is(GCyRTags.VEHICLE_FUELS)); } @@ -174,7 +181,8 @@ public ModularUI createUI(Player entityPlayer) { return new ModularUI(176, 166, this, entityPlayer) .widget(new LabelWidget(7, 7, this.getDisplayName().getString())) .widget(new TankWidget(this.fuelTank, 16, 20, 20, 58, true, true).setBackground(GuiTextures.FLUID_TANK_BACKGROUND)) - .widget(new SlotWidget(configSlot, 0, 40, 20, true, true)) + .widget(new SlotWidget(configSlot, 0, 40, 20)) + .widget(new SlotWidget(satelliteSlot, 0, 60, 20)) .widget(new ButtonWidget(40, 60, 36, 18, new GuiTextureGroup(GuiTextures.BUTTON.copy().setColor(0xFFAA0000), new TextTexture("menu.gcyr.launch")), (clickData) -> this.startRocket())) .widget(new ButtonWidget(40, 40, 36, 18, new GuiTextureGroup(GuiTextures.BUTTON.copy().setColor(0xFFE0B900), new TextTexture("gcyr.multiblock.rocket.unbuild")), (clickData) -> this.unBuild())) .widget(UITemplate.bindPlayerInventory(entityPlayer.getInventory(), GuiTextures.SLOT, 7, 84, true)) @@ -440,8 +448,16 @@ private void goToDestination() { ItemStack configStack = configSlot.getStackInSlot(0); if (this.destination == null && GCyRItems.ID_CHIP.isIn(configStack)) { this.destination = PlanetIdChipBehaviour.getPlanetFromStack(configStack); - } else if (GCyRItems.KEYCARD.isIn(configStack) && KeyCardBehaviour.getSavedStation(configStack) != SpaceStation.ID_EMPTY) { + } else if (GCyRItems.KEYCARD.isIn(configStack) && KeyCardBehaviour.getSavedStation(configStack) != null) { this.destinationIsSpaceStation = true; + // return if no valid station & no station kit + if (!this.satelliteSlot.getStackInSlot(0).is(GCyRItems.SPACE_STATION_PACKAGE.get()) && GCyRCapabilityHelper.getSpaceStations(this.getServer().getLevel(destination.orbitWorld())).getStation(KeyCardBehaviour.getSavedStation(configStack)) == null) { + this.destination = null; + this.destinationIsSpaceStation = false; + this.entityData.set(ROCKET_STARTED, false); + this.setDeltaMovement(0, -0.5, 0); + return; + } } ResourceKey destinationDim = this.destinationIsSpaceStation ? destination.orbitWorld() : destination.level(); if (this.fuelTank.drain(computeRequiredFuelAmountForDestination(this.destination) / 3, true).isEmpty()) { @@ -485,7 +501,7 @@ private void goToDestination() { Vec3 pos = this.position(); if (this.destinationIsSpaceStation) { ISpaceStationHolder stations = GCyRCapabilityHelper.getSpaceStations(destinationLevel); - int stationId; + Integer stationId; boolean didChange = false; if (GCyRItems.KEYCARD.isIn(configStack)) { stationId = KeyCardBehaviour.getSavedStation(configStack); @@ -506,7 +522,7 @@ private void goToDestination() { didChange = true; } } else { - stationId = SpaceStation.ID_EMPTY; + stationId = null; } if (didChange) { @@ -518,17 +534,32 @@ private void goToDestination() { } BlockPos stationPos = stations.getStationWorldPos(stationId); - newRocket.setPos(stationPos.getX(), pos.y, stationPos.getZ()); + if (destinationPos == null) { + destinationPos = new BlockPos(stationPos.getX(), (int) pos.y, stationPos.getZ()); + } + + if (GCyRItems.SPACE_STATION_PACKAGE.isIn(this.satelliteSlot.getStackInSlot(0))) { + ItemStack stack = this.satelliteSlot.getStackInSlot(0); + buildSpaceStation(stack, new BlockPos(destinationPos.getX(), 64, destinationPos.getZ())); + } } else { double scale = DimensionType.getTeleportationScale(this.level.dimensionType(), destinationLevel.dimensionType()); - newRocket.setPos(pos.multiply(scale, 1, scale)); + if (destinationPos == null) { + destinationPos = new BlockPos(pos.multiply(scale, 1, scale)); + } } + newRocket.setPos(destinationPos.getX(), destinationPos.getY(), destinationPos.getZ()); Vec3 delta = this.getDeltaMovement(); newRocket.setDeltaMovement(delta.x, -0.5, delta.z); if (newRocket instanceof RocketEntity rocketEntity) { rocketEntity.destination = null; rocketEntity.destinationIsSpaceStation = false; + if (this.destinationIsSpaceStation) { + rocketEntity.lastPos = GlobalPos.of(destinationDim, this.getOnPos()); + } else { + rocketEntity.lastPos = null; + } rocketEntity.entityData.set(ROCKET_STARTED, false); rocketEntity.entityData.set(START_TIMER, 0); } @@ -544,11 +575,7 @@ public void unBuild() { for (PosWithState state : this.getBlocks()) { BlockPos offset = origin.offset(state.pos()); BlockHitResult result = new BlockHitResult( - new Vec3( - offset.getX() + 0.5, - offset.getY() + 0.5, - offset.getZ() + 0.5 - ), + Vec3.atCenterOf(offset), Direction.DOWN, offset, false @@ -743,6 +770,13 @@ protected void readAdditionalSaveData(CompoundTag compound) { this.setStartTimer(compound.getInt("startTimer")); this.entityData.set(ROCKET_STARTED, compound.getBoolean("isStarted")); this.setWeight(compound.getInt("weight")); + + if (compound.contains("lastPos")) { + CompoundTag tag = compound.getCompound("lastPos"); + BlockPos pos = NbtUtils.readBlockPos(tag.getCompound("pos")); + ResourceKey level = ResourceKey.create(Registries.DIMENSION, new ResourceLocation(tag.getString("level"))); + this.lastPos = GlobalPos.of(level, pos); + } } @Override @@ -763,6 +797,13 @@ protected void addAdditionalSaveData(CompoundTag compound) { compound.putInt("startTimer", this.getStartTimer()); compound.putBoolean("isStarted", this.entityData.get(ROCKET_STARTED)); compound.putInt("weight", this.getWeight()); + + if (this.lastPos != null) { + CompoundTag tag = new CompoundTag(); + tag.put("pos", NbtUtils.writeBlockPos(this.lastPos.pos())); + tag.putString("level", this.lastPos.dimension().location().toString()); + compound.put("lastPos", tag); + } } @Override diff --git a/common/src/main/java/argent_matter/gcyr/common/item/KeyCardBehaviour.java b/common/src/main/java/argent_matter/gcyr/common/item/KeyCardBehaviour.java index b43c66fe..63d03f0a 100644 --- a/common/src/main/java/argent_matter/gcyr/common/item/KeyCardBehaviour.java +++ b/common/src/main/java/argent_matter/gcyr/common/item/KeyCardBehaviour.java @@ -39,14 +39,15 @@ public static void setOwner(ItemStack stack, LivingEntity entity) { stack.getOrCreateTag().putUUID("KeyCardOwner", entity.getUUID()); } - public static void setSavedStation(ItemStack stack, int stationId, Planet planet) { + public static void setSavedStation(ItemStack stack, @Nullable Integer stationId, Planet planet) { if (!GCyRItems.KEYCARD.isIn(stack)) return; + if (stationId == null) return; stack.getOrCreateTag().putInt(PlanetIdChipBehaviour.CURRENT_STATION_KEY, stationId); stack.getTag().putString(PlanetIdChipBehaviour.CURRENT_PLANET_KEY, PlanetData.getPlanetId(planet).toString()); } - public static int getSavedStation(ItemStack stack) { - if (!GCyRItems.KEYCARD.isIn(stack)) return SpaceStation.ID_EMPTY; + public static Integer getSavedStation(ItemStack stack) { + if (!GCyRItems.KEYCARD.isIn(stack)) return null; return stack.getOrCreateTag().getInt(PlanetIdChipBehaviour.CURRENT_STATION_KEY); } @@ -72,8 +73,8 @@ public void appendHoverText(ItemStack stack, @Nullable Level level, List extends RegistryEntry> { + + public MenuEntryMixin(AbstractRegistrate owner, RegistryObject> delegate) { + super(owner, delegate); + } + + /** + * @author Screret + * @reason Fabric requires this. + */ + @Overwrite + public T create(int windowId, Inventory playerInv) { + return ((ExtendedScreenHandlerType)get()).create(windowId, playerInv, new FriendlyByteBuf(new EmptyByteBuf(ByteBufAllocator.DEFAULT))); + } +} diff --git a/fabric/src/main/resources/gcyr.mixins.json b/fabric/src/main/resources/gcyr.mixins.json index a721bd9f..b1c6d3bd 100644 --- a/fabric/src/main/resources/gcyr.mixins.json +++ b/fabric/src/main/resources/gcyr.mixins.json @@ -4,6 +4,7 @@ "package": "argent_matter.gcyr.fabric.mixin", "compatibilityLevel": "JAVA_17", "mixins": [ + "MenuEntryMixin" ], "client": [ "KeyboardHandlerMixin", diff --git a/forge/build.gradle b/forge/build.gradle index 639cec39..ae988335 100644 --- a/forge/build.gradle +++ b/forge/build.gradle @@ -97,7 +97,7 @@ dependencies { // LDLib modImplementation(forge.gtceu.forge) { transitive = false } - modImplementation(forge.ldlib.forge) //{ transitive = false } + modImplementation(forge.ldlib.forge) { transitive = false } // Registrate modImplementation("com.tterrag.registrate:Registrate:${forge.versions.registrate.get()}")