Skip to content

Commit dbebc89

Browse files
committed
Move shrinking configuration to a data component instead of a tag
This deprecates the usage of the tag for MC 22
1 parent ccafa5e commit dbebc89

File tree

14 files changed

+323
-141
lines changed

14 files changed

+323
-141
lines changed

core-api/src/main/java/dev/compactmods/machines/api/codec/CodecExtensions.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package dev.compactmods.machines.api.codec;
22

33
import com.mojang.serialization.Codec;
4+
import io.netty.buffer.ByteBuf;
45
import net.minecraft.Util;
6+
import net.minecraft.network.codec.ByteBufCodecs;
7+
import net.minecraft.network.codec.StreamCodec;
8+
import net.minecraft.util.StringRepresentable;
59
import net.minecraft.world.level.ChunkPos;
610
import net.minecraft.world.phys.Vec2;
711

812
import java.util.List;
13+
import java.util.function.Function;
914
import java.util.stream.IntStream;
1015

1116
public abstract class CodecExtensions {
@@ -19,4 +24,8 @@ public abstract class CodecExtensions {
1924
.comapFlatMap(i -> Util.fixedSize(i, 2)
2025
.map(arr -> new ChunkPos(arr[0], arr[1])), pos -> IntStream.of(pos.x, pos.z));
2126

27+
public static <T extends Enum<T> & StringRepresentable> StreamCodec<ByteBuf, T> stringRepresentableStreamCodec(T[] values) {
28+
final var lookup = StringRepresentable.createNameLookup(values, Function.identity());
29+
return ByteBufCodecs.STRING_UTF8.map(lookup, T::getSerializedName);
30+
}
2231
}

core-api/src/main/java/dev/compactmods/machines/api/dimension/CompactDimension.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.minecraft.server.MinecraftServer;
88
import net.minecraft.server.level.ServerLevel;
99
import net.minecraft.util.datafix.DataFixers;
10+
import net.minecraft.world.entity.LivingEntity;
1011
import net.minecraft.world.level.Level;
1112
import net.minecraft.world.level.dimension.DimensionType;
1213
import net.minecraft.world.level.storage.DimensionDataStorage;
@@ -56,4 +57,9 @@ public static boolean isLevelCompact(Level level) {
5657
public static boolean isLevelCompact(ResourceKey<Level> level) {
5758
return level.equals(LEVEL_KEY);
5859
}
60+
61+
public static boolean isInServerDimension(@NotNull LivingEntity entity) {
62+
final var l = entity.level();
63+
return !l.isClientSide() && l.dimension().equals(LEVEL_KEY);
64+
}
5965
}
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
package dev.compactmods.machines.api.room.history;
22

33
public enum RoomEntryResult {
4-
SUCCESS,
5-
FAILED_TOO_FAR_DOWN,
6-
FAILED_ROOM_INVALID
4+
SUCCESS(true),
5+
FAILED_TOO_FAR_DOWN(false),
6+
FAILED_ROOM_INVALID(false);
7+
8+
private final boolean success;
9+
10+
RoomEntryResult(boolean successful) {
11+
this.success = successful;
12+
}
13+
14+
public boolean successful() {
15+
return success;
16+
}
717
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package dev.compactmods.machines.api.room.history;
2+
3+
public enum RoomExitResult {
4+
SUCCESS_WENT_TO_LAST_ENTRYPOINT(true),
5+
SUCCESS_WENT_TO_SPAWN(true),
6+
FAILED_NOT_IN_COMPACT_DIM(false),
7+
FAILED_ROOM_NOT_FOUND(false);
8+
9+
private final boolean success;
10+
11+
RoomExitResult(boolean success) {
12+
this.success = success;
13+
}
14+
15+
public boolean successful() {
16+
return this.success;
17+
}
18+
}

core-api/src/main/java/dev/compactmods/machines/api/shrinking/PSDTags.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ public interface PSDTags {
99
/**
1010
* Marks an item as a personal shrinking device.
1111
*/
12+
@Deprecated(forRemoval = true)
1213
TagKey<Item> ITEM = TagKey.create(Registries.ITEM, CompactMachines.modRL("shrinking_device"));
1314
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package dev.compactmods.machines.api.shrinking.component;
2+
3+
import com.mojang.serialization.Codec;
4+
import com.mojang.serialization.codecs.RecordCodecBuilder;
5+
import dev.compactmods.machines.api.codec.CodecExtensions;
6+
import io.netty.buffer.ByteBuf;
7+
import net.minecraft.network.codec.ByteBufCodecs;
8+
import net.minecraft.network.codec.StreamCodec;
9+
import net.minecraft.util.Mth;
10+
import net.minecraft.util.StringRepresentable;
11+
12+
public record ShrinkingDeviceConfiguration(
13+
int maxAllowedDepth,
14+
boolean allowLoops,
15+
AfterUseAction afterUseAction
16+
) {
17+
public static final int ALLOWED_DEPTH_MIN = 1;
18+
public static final int ALLOWED_DEPTH_DEFAULT = 5;
19+
public static final int ALLOWED_DEPTH_MAX = 25;
20+
21+
public static final Codec<ShrinkingDeviceConfiguration> CODEC = RecordCodecBuilder.create(instance -> instance.group(
22+
Codec.intRange(ALLOWED_DEPTH_MIN, ALLOWED_DEPTH_MAX).fieldOf("max_allowed_depth").forGetter(ShrinkingDeviceConfiguration::maxAllowedDepth),
23+
Codec.BOOL.fieldOf("allow_loops").forGetter(ShrinkingDeviceConfiguration::allowLoops),
24+
AfterUseAction.CODEC.fieldOf("after_use").forGetter(ShrinkingDeviceConfiguration::afterUseAction)
25+
).apply(instance, ShrinkingDeviceConfiguration::new));
26+
27+
public static final StreamCodec<ByteBuf, ShrinkingDeviceConfiguration> STREAM_CODEC = StreamCodec.composite(
28+
ByteBufCodecs.INT, ShrinkingDeviceConfiguration::maxAllowedDepth,
29+
ByteBufCodecs.BOOL, ShrinkingDeviceConfiguration::allowLoops,
30+
AfterUseAction.STREAM_CODEC, ShrinkingDeviceConfiguration::afterUseAction,
31+
ShrinkingDeviceConfiguration::new
32+
);
33+
34+
public static ShrinkingDeviceConfiguration basicNoDamage(int maxAllowedDepth) {
35+
int clamped = Mth.clamp(maxAllowedDepth, ALLOWED_DEPTH_MIN, ALLOWED_DEPTH_MAX);
36+
return new ShrinkingDeviceConfiguration(clamped, true, AfterUseAction.DO_NOTHING);
37+
}
38+
39+
public static ShrinkingDeviceConfiguration basicDamaging(int maxAllowedDepth) {
40+
int clamped = Mth.clamp(maxAllowedDepth, ALLOWED_DEPTH_MIN, ALLOWED_DEPTH_MAX);
41+
return new ShrinkingDeviceConfiguration(clamped, true, AfterUseAction.DAMAGE);
42+
}
43+
44+
public static ShrinkingDeviceConfiguration basicOneOff() {
45+
return new ShrinkingDeviceConfiguration(ALLOWED_DEPTH_MAX, true, AfterUseAction.BREAK);
46+
}
47+
48+
public static final ShrinkingDeviceConfiguration DEFAULT_CONFIG = basicNoDamage(ALLOWED_DEPTH_DEFAULT);
49+
50+
public enum AfterUseAction implements StringRepresentable {
51+
DO_NOTHING,
52+
DAMAGE,
53+
BREAK;
54+
55+
public static final Codec<AfterUseAction> CODEC = StringRepresentable.fromValues(AfterUseAction::values);
56+
57+
public static final StreamCodec<ByteBuf, AfterUseAction> STREAM_CODEC = CodecExtensions.stringRepresentableStreamCodec(AfterUseAction.values());
58+
59+
@Override
60+
public String getSerializedName() {
61+
return name().toLowerCase();
62+
}
63+
}
64+
}

neoforge-main/src/main/java/dev/compactmods/machines/CMRegistries.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import dev.compactmods.machines.api.CompactMachines;
55
import dev.compactmods.machines.shrinking.Shrinking;
66
import net.minecraft.commands.synchronization.ArgumentTypeInfo;
7+
import net.minecraft.core.component.DataComponentType;
78
import net.minecraft.core.registries.BuiltInRegistries;
9+
import net.minecraft.core.registries.Registries;
810
import net.minecraft.world.entity.ai.village.poi.PoiType;
911
import net.minecraft.world.entity.npc.VillagerProfession;
1012
import net.minecraft.world.inventory.MenuType;
@@ -48,7 +50,7 @@ public interface CMRegistries {
4850

4951
DeferredRegister<AttachmentType<?>> ATTACHMENT_TYPES = DeferredRegister.create(NeoForgeRegistries.ATTACHMENT_TYPES, CompactMachines.MOD_ID);
5052

51-
DeferredRegister.DataComponents DATA_COMPONENTS = DeferredRegister.createDataComponents(CompactMachines.MOD_ID);
53+
DeferredRegister.DataComponents DATA_COMPONENTS = DeferredRegister.createDataComponents(Registries.DATA_COMPONENT_TYPE, CompactMachines.MOD_ID);
5254

5355
static Item basicItem() {
5456
return new Item(new Item.Properties());

neoforge-main/src/main/java/dev/compactmods/machines/gamerule/CMGameRules.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,18 @@ public class CMGameRules {
1010
public static GameRules.Key<GameRules.BooleanValue> ALLOW_SPECTATORS_OUT_OF_BOUNDS;
1111
public static GameRules.Key<GameRules.BooleanValue> DAMAGE_OOB_PLAYERS;
1212

13+
/**
14+
* For hardcore-style packs. If a shrinking item is successfully used to LEAVE a room,
15+
* it will also be damaged. Off by default.
16+
*/
17+
public static GameRules.Key<GameRules.BooleanValue> DAMAGE_PSD_ITEMS_ON_ROOM_EXIT;
18+
1319
public static void register() {
1420
ALLOW_SURVIVAL_OUT_OF_BOUNDS = GameRules.register(CompactMachines.dotPrefix("allow_survival_oob"), GameRules.Category.PLAYER, GameRules.BooleanValue.create(false));
1521
ALLOW_CREATIVE_OUT_OF_BOUNDS = GameRules.register(CompactMachines.dotPrefix("allow_creative_oob"), GameRules.Category.PLAYER, GameRules.BooleanValue.create(true));
1622
ALLOW_SPECTATORS_OUT_OF_BOUNDS = GameRules.register(CompactMachines.dotPrefix("allow_spectator_oob"), GameRules.Category.PLAYER, GameRules.BooleanValue.create(true));
1723
DAMAGE_OOB_PLAYERS = GameRules.register(CompactMachines.dotPrefix("damage_oob"), GameRules.Category.PLAYER, GameRules.BooleanValue.create(true));
24+
25+
DAMAGE_PSD_ITEMS_ON_ROOM_EXIT = GameRules.register(CompactMachines.dotPrefix("damage_psd_on_exit"), GameRules.Category.PLAYER, GameRules.BooleanValue.create(false));
1826
}
1927
}

neoforge-main/src/main/java/dev/compactmods/machines/machine/block/BoundCompactMachineBlock.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
import dev.compactmods.machines.LoggingUtil;
44
import dev.compactmods.machines.api.CompactMachines;
5-
import dev.compactmods.machines.api.dimension.MissingDimensionException;
6-
import dev.compactmods.machines.api.room.exceptions.NonexistentRoomException;
75
import dev.compactmods.machines.api.shrinking.PSDTags;
6+
import dev.compactmods.machines.gamerule.CMGameRules;
87
import dev.compactmods.machines.machine.Machines;
98
import dev.compactmods.machines.network.machine.OpenMachinePreviewScreenPacket;
109
import dev.compactmods.machines.room.RoomBlocks;
1110
import dev.compactmods.machines.room.RoomHelper;
1211
import dev.compactmods.machines.room.Rooms;
12+
import dev.compactmods.machines.shrinking.PersonalShrinkingDevice;
13+
import dev.compactmods.machines.shrinking.Shrinking;
14+
import dev.compactmods.machines.util.PlayerUtil;
1315
import net.minecraft.core.BlockPos;
1416
import net.minecraft.core.GlobalPos;
15-
import net.minecraft.network.FriendlyByteBuf;
1617
import net.minecraft.server.level.ServerLevel;
1718
import net.minecraft.server.level.ServerPlayer;
1819
import net.minecraft.world.InteractionHand;
@@ -32,7 +33,6 @@
3233
import net.neoforged.neoforge.network.PacketDistributor;
3334
import org.jetbrains.annotations.Nullable;
3435

35-
import java.util.Optional;
3636
import java.util.concurrent.ExecutionException;
3737

3838
public class BoundCompactMachineBlock extends CompactMachineBlock implements EntityBlock {
@@ -48,9 +48,7 @@ public ItemStack getCloneItemStack(BlockState state, HitResult target, LevelRead
4848
}
4949

5050
return Machines.Items.unbound();
51-
}
52-
53-
catch(Exception ex) {
51+
} catch (Exception ex) {
5452
LoggingUtil.modLog().warn("Warning: tried to pick block on a bound machine that does not have a room bound.", ex);
5553
return Machines.Items.unbound();
5654
}
@@ -101,12 +99,18 @@ protected ItemInteractionResult useItemOn(ItemStack mainItem, BlockState state,
10199
return tryDyingMachine(sl, pos, player, dye, mainItem);
102100
}
103101

104-
if (mainItem.is(PSDTags.ITEM)
105-
&& player instanceof ServerPlayer sp
102+
if ((mainItem.is(PSDTags.ITEM) || mainItem.has(Shrinking.DataComponents.SHRINKING_CONFIG))
103+
&& player instanceof ServerPlayer serverPlayer
106104
&& level.getBlockEntity(pos) instanceof BoundCompactMachineBlockEntity tile) {
107105

108106
// Try to teleport player into room
109-
RoomHelper.teleportPlayerIntoMachine(level, sp, tile.getLevelPosition(), tile.connectedRoom());
107+
RoomHelper.teleportPlayerIntoMachine(level, serverPlayer, tile.getLevelPosition(), tile.connectedRoom()).thenAccept(result -> {
108+
if (result.successful()) {
109+
var config = PersonalShrinkingDevice.config(mainItem);
110+
PersonalShrinkingDevice.handleSuccessfulAtomicShift(mainItem, serverPlayer, config);
111+
}
112+
});
113+
110114
return ItemInteractionResult.SUCCESS;
111115
}
112116

neoforge-main/src/main/java/dev/compactmods/machines/room/RoomEventHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static void entityChangedDimensions(final EntityTravelToDimensionEvent di
2929

3030
if(!CompactDimension.isLevelCompact(dimensionEvent.getDimension())) {
3131
if(p instanceof ServerPlayer sp) {
32-
LOGS.debug("Resetting player {} room history due to dimension change.", sp.getDisplayName());
32+
LOGS.debug("Resetting player {}'s room history due to dimension change.", sp.getGameProfile().getName());
3333
PlayerUtil.resetPlayerHistory(sp);
3434
}
3535
}

0 commit comments

Comments
 (0)