diff --git a/common/api/core/src/main/java/com/dfsek/terra/api/handle/WorldHandle.java b/common/api/core/src/main/java/com/dfsek/terra/api/handle/WorldHandle.java index 6d6b6f317..b32874428 100644 --- a/common/api/core/src/main/java/com/dfsek/terra/api/handle/WorldHandle.java +++ b/common/api/core/src/main/java/com/dfsek/terra/api/handle/WorldHandle.java @@ -10,8 +10,6 @@ package com.dfsek.terra.api.handle; import com.dfsek.terra.api.block.entity.BlockEntity; import com.dfsek.terra.api.block.state.BlockState; import com.dfsek.terra.api.entity.EntityType; -import com.dfsek.terra.api.entity.Player; -import com.dfsek.terra.api.util.generic.pair.Pair; import com.dfsek.terra.api.util.vector.Vector3; diff --git a/common/api/util/src/main/java/com/dfsek/terra/api/util/generic/pair/ImmutablePair.java b/common/api/util/src/main/java/com/dfsek/terra/api/util/generic/pair/ImmutablePair.java deleted file mode 100644 index c61febdaa..000000000 --- a/common/api/util/src/main/java/com/dfsek/terra/api/util/generic/pair/ImmutablePair.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2020-2021 Polyhedral Development - * - * The Terra API is licensed under the terms of the MIT License. For more details, - * reference the LICENSE file in the common/api directory. - */ - -package com.dfsek.terra.api.util.generic.pair; - -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; - -import java.util.Objects; - - -public final class ImmutablePair { - private static final ImmutablePair NULL = new ImmutablePair<>(null, null); - private final L left; - private final R right; - - private ImmutablePair(L left, R right) { - this.left = left; - this.right = right; - } - - @Contract("_, _ -> new") - public static ImmutablePair of(L1 left, R1 right) { - return new ImmutablePair<>(left, right); - } - - @Contract("-> new") - @SuppressWarnings("unchecked") - public static ImmutablePair ofNull() { - return (ImmutablePair) NULL; - } - - @NotNull - @Contract("-> new") - public Pair mutable() { - return Pair.of(left, right); - } - - public R getRight() { - return right; - } - - public L getLeft() { - return left; - } - - @Override - public int hashCode() { - return Objects.hash(left, right); - } - - @Override - public boolean equals(Object obj) { - if(!(obj instanceof ImmutablePair)) return false; - - ImmutablePair that = (ImmutablePair) obj; - return Objects.equals(this.left, that.left) && Objects.equals(this.right, that.right); - } -} diff --git a/common/api/util/src/main/java/com/dfsek/terra/api/util/generic/pair/Pair.java b/common/api/util/src/main/java/com/dfsek/terra/api/util/generic/pair/Pair.java index 96819756c..bf6bb13d7 100644 --- a/common/api/util/src/main/java/com/dfsek/terra/api/util/generic/pair/Pair.java +++ b/common/api/util/src/main/java/com/dfsek/terra/api/util/generic/pair/Pair.java @@ -13,40 +13,39 @@ import org.jetbrains.annotations.NotNull; import java.util.Objects; -public class Pair { - private L left; - private R right; +public final class Pair { + private static final Pair NULL = new Pair<>(null, null); + private final L left; + private final R right; private Pair(L left, R right) { this.left = left; this.right = right; } - @NotNull @Contract("_, _ -> new") public static Pair of(L1 left, R1 right) { return new Pair<>(left, right); } @Contract("-> new") - public ImmutablePair immutable() { - return ImmutablePair.of(left, right); + @SuppressWarnings("unchecked") + public static Pair ofNull() { + return (Pair) NULL; } - public L getLeft() { - return left; - } - - public void setLeft(L left) { - this.left = left; + @NotNull + @Contract("-> new") + public Pair.Mutable mutable() { + return Mutable.of(left, right); } public R getRight() { return right; } - public void setRight(R right) { - this.right = right; + public L getLeft() { + return left; } @Override @@ -56,9 +55,57 @@ public class Pair { @Override public boolean equals(Object obj) { - if(!(obj instanceof Pair)) return false; - - Pair that = (Pair) obj; + if(!(obj instanceof Pair that)) return false; + return Objects.equals(this.left, that.left) && Objects.equals(this.right, that.right); } + + public static class Mutable { + private L left; + private R right; + + private Mutable(L left, R right) { + this.left = left; + this.right = right; + } + + @NotNull + @Contract("_, _ -> new") + public static Pair.Mutable of(L1 left, R1 right) { + return new Mutable<>(left, right); + } + + @Contract("-> new") + public Pair immutable() { + return Pair.of(left, right); + } + + public L getLeft() { + return left; + } + + public void setLeft(L left) { + this.left = left; + } + + public R getRight() { + return right; + } + + public void setRight(R right) { + this.right = right; + } + + @Override + public int hashCode() { + return Objects.hash(left, right); + } + + @Override + public boolean equals(Object obj) { + if(!(obj instanceof Mutable that)) return false; + + return Objects.equals(this.left, that.left) && Objects.equals(this.right, that.right); + } + } } diff --git a/common/implementation/base/src/main/java/com/dfsek/terra/config/pack/ConfigPackImpl.java b/common/implementation/base/src/main/java/com/dfsek/terra/config/pack/ConfigPackImpl.java index 4ea7ffcb9..60d16ccc3 100644 --- a/common/implementation/base/src/main/java/com/dfsek/terra/config/pack/ConfigPackImpl.java +++ b/common/implementation/base/src/main/java/com/dfsek/terra/config/pack/ConfigPackImpl.java @@ -71,7 +71,7 @@ import com.dfsek.terra.api.registry.OpenRegistry; import com.dfsek.terra.api.registry.Registry; import com.dfsek.terra.api.registry.exception.DuplicateEntryException; import com.dfsek.terra.api.registry.meta.RegistryFactory; -import com.dfsek.terra.api.util.generic.pair.ImmutablePair; +import com.dfsek.terra.api.util.generic.pair.Pair; import com.dfsek.terra.api.util.reflection.ReflectionUtil; import com.dfsek.terra.api.world.World; import com.dfsek.terra.api.world.biome.generation.BiomeProvider; @@ -116,12 +116,12 @@ public class ConfigPackImpl implements ConfigPack { private final BiomeProvider seededBiomeProvider; - private final Map, CheckedRegistry>> registryMap = new HashMap<>(); + private final Map, CheckedRegistry>> registryMap = new HashMap<>(); private final ConfigTypeRegistry configTypeRegistry; - private final TreeMap>>> configTypes = new TreeMap<>(); + private final TreeMap>>> configTypes = new TreeMap<>(); public ConfigPackImpl(File folder, Platform platform) throws ConfigException { try { @@ -255,7 +255,7 @@ public class ConfigPackImpl implements ConfigPack { if(contained.contains(pair.getLeft())) throw new IllegalArgumentException("Duplicate config ID: " + id); contained.add(id); })); - configTypes.computeIfAbsent(priority, p -> new ArrayList<>()).add(ImmutablePair.of(id, type)); + configTypes.computeIfAbsent(priority, p -> new ArrayList<>()).add(Pair.of(id, type)); } @Override @@ -327,7 +327,7 @@ public class ConfigPackImpl implements ConfigPack { } } - return ImmutablePair.of(registry, new CheckedRegistryImpl<>(registry)); + return Pair.of(registry, new CheckedRegistryImpl<>(registry)); }).getRight(); } @@ -378,7 +378,7 @@ public class ConfigPackImpl implements ConfigPack { } selfLoader.registerLoader(configType.getTypeKey().getType(), openRegistry); abstractConfigLoader.registerLoader(configType.getTypeKey().getType(), openRegistry); - registryMap.put(configType.getTypeKey().getType(), ImmutablePair.of(openRegistry, new CheckedRegistryImpl<>(openRegistry))); + registryMap.put(configType.getTypeKey().getType(), Pair.of(openRegistry, new CheckedRegistryImpl<>(openRegistry))); }); } @@ -453,7 +453,7 @@ public class ConfigPackImpl implements ConfigPack { template.getID(), template.getVersion(), template.getAuthor(), (System.nanoTime() - start) / 1000000.0D); } - protected Map, CheckedRegistry>> getRegistryMap() { + protected Map, CheckedRegistry>> getRegistryMap() { return registryMap; } @@ -464,18 +464,18 @@ public class ConfigPackImpl implements ConfigPack { @Override @SuppressWarnings("unchecked") public CheckedRegistry getRegistry(Type type) { - return (CheckedRegistry) registryMap.getOrDefault(type, ImmutablePair.ofNull()).getRight(); + return (CheckedRegistry) registryMap.getOrDefault(type, Pair.ofNull()).getRight(); } @SuppressWarnings("unchecked") @Override public CheckedRegistry getCheckedRegistry(Type type) throws IllegalStateException { - return (CheckedRegistry) registryMap.getOrDefault(type, ImmutablePair.ofNull()).getRight(); + return (CheckedRegistry) registryMap.getOrDefault(type, Pair.ofNull()).getRight(); } @SuppressWarnings("unchecked") protected OpenRegistry getOpenRegistry(Class clazz) { - return (OpenRegistry) registryMap.getOrDefault(clazz, ImmutablePair.ofNull()).getLeft(); + return (OpenRegistry) registryMap.getOrDefault(clazz, Pair.ofNull()).getLeft(); } @Override diff --git a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/FabricAddon.java b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/FabricAddon.java index 600623a56..2dd8747f2 100644 --- a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/FabricAddon.java +++ b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/FabricAddon.java @@ -23,6 +23,8 @@ import com.dfsek.tectonic.exception.ConfigException; import com.dfsek.terra.api.addon.BaseAddon; +import com.dfsek.terra.api.util.generic.pair.Pair.Mutable; + import net.minecraft.util.Identifier; import net.minecraft.util.registry.BuiltinRegistries; import net.minecraft.util.registry.Registry; @@ -40,7 +42,6 @@ import com.dfsek.terra.api.event.events.config.pack.ConfigPackPreLoadEvent; import com.dfsek.terra.api.event.functional.FunctionalEventHandler; import com.dfsek.terra.api.registry.CheckedRegistry; import com.dfsek.terra.api.registry.exception.DuplicateEntryException; -import com.dfsek.terra.api.util.generic.pair.Pair; import com.dfsek.terra.api.world.Tree; import com.dfsek.terra.api.world.biome.TerraBiome; import com.dfsek.terra.fabric.config.PostLoadCompatibilityOptions; @@ -54,7 +55,7 @@ public final class FabricAddon implements BaseAddon { private final PlatformImpl terraFabricPlugin; private static final Logger logger = LoggerFactory.getLogger(FabricAddon.class); - private final Map> templates = new HashMap<>(); + private final Map> templates = new HashMap<>(); public FabricAddon(PlatformImpl terraFabricPlugin) { this.terraFabricPlugin = terraFabricPlugin; @@ -88,7 +89,7 @@ public final class FabricAddon implements BaseAddon { } }); } - templates.put(event.getPack(), Pair.of(template, null)); + templates.put(event.getPack(), Mutable.of(template, null)); }) .global(); @@ -143,7 +144,7 @@ public final class FabricAddon implements BaseAddon { } } - public Map> getTemplates() { + public Map> getTemplates() { return templates; } diff --git a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/block/FabricBlockState.java b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/block/FabricBlockState.java index 21243bba0..12f93439e 100644 --- a/platforms/fabric/src/main/java/com/dfsek/terra/fabric/block/FabricBlockState.java +++ b/platforms/fabric/src/main/java/com/dfsek/terra/fabric/block/FabricBlockState.java @@ -35,52 +35,52 @@ import com.dfsek.terra.api.block.state.BlockState; import com.dfsek.terra.api.block.state.properties.Property; import com.dfsek.terra.api.block.state.properties.base.Properties; import com.dfsek.terra.api.util.generic.Construct; -import com.dfsek.terra.api.util.generic.pair.ImmutablePair; +import com.dfsek.terra.api.util.generic.pair.Pair; import com.dfsek.terra.fabric.mixin.access.StateAccessor; import com.dfsek.terra.fabric.util.FabricAdapter; public class FabricBlockState implements BlockState { - private static final Map, ImmutablePair, Function>> + private static final Map, Pair, Function>> PROPERTY_DELEGATES_T2M = Construct.construct(() -> { - Map, ImmutablePair, Function>> map = new HashMap<>(); - map.put(Properties.AXIS, ImmutablePair.of(net.minecraft.state.property.Properties.AXIS, + Map, Pair, Function>> map = new HashMap<>(); + map.put(Properties.AXIS, Pair.of(net.minecraft.state.property.Properties.AXIS, a -> FabricAdapter.adapt((net.minecraft.util.math.Direction.Axis) a))); - map.put(Properties.NORTH, ImmutablePair.of(net.minecraft.state.property.Properties.NORTH, Function.identity())); - map.put(Properties.SOUTH, ImmutablePair.of(net.minecraft.state.property.Properties.SOUTH, Function.identity())); - map.put(Properties.EAST, ImmutablePair.of(net.minecraft.state.property.Properties.EAST, Function.identity())); - map.put(Properties.WEST, ImmutablePair.of(net.minecraft.state.property.Properties.WEST, Function.identity())); + map.put(Properties.NORTH, Pair.of(net.minecraft.state.property.Properties.NORTH, Function.identity())); + map.put(Properties.SOUTH, Pair.of(net.minecraft.state.property.Properties.SOUTH, Function.identity())); + map.put(Properties.EAST, Pair.of(net.minecraft.state.property.Properties.EAST, Function.identity())); + map.put(Properties.WEST, Pair.of(net.minecraft.state.property.Properties.WEST, Function.identity())); - map.put(Properties.NORTH_CONNECTION, ImmutablePair.of(net.minecraft.state.property.Properties.NORTH_WIRE_CONNECTION, + map.put(Properties.NORTH_CONNECTION, Pair.of(net.minecraft.state.property.Properties.NORTH_WIRE_CONNECTION, c -> FabricAdapter.adapt((WireConnection) c))); - map.put(Properties.SOUTH_CONNECTION, ImmutablePair.of(net.minecraft.state.property.Properties.SOUTH_WIRE_CONNECTION, + map.put(Properties.SOUTH_CONNECTION, Pair.of(net.minecraft.state.property.Properties.SOUTH_WIRE_CONNECTION, c -> FabricAdapter.adapt((WireConnection) c))); - map.put(Properties.EAST_CONNECTION, ImmutablePair.of(net.minecraft.state.property.Properties.EAST_WIRE_CONNECTION, + map.put(Properties.EAST_CONNECTION, Pair.of(net.minecraft.state.property.Properties.EAST_WIRE_CONNECTION, c -> FabricAdapter.adapt((WireConnection) c))); - map.put(Properties.WEST_CONNECTION, ImmutablePair.of(net.minecraft.state.property.Properties.WEST_WIRE_CONNECTION, + map.put(Properties.WEST_CONNECTION, Pair.of(net.minecraft.state.property.Properties.WEST_WIRE_CONNECTION, c -> FabricAdapter.adapt((WireConnection) c))); map.put(Properties.NORTH_HEIGHT, - ImmutablePair.of(net.minecraft.state.property.Properties.NORTH_WALL_SHAPE, h -> FabricAdapter.adapt((WallShape) h))); + Pair.of(net.minecraft.state.property.Properties.NORTH_WALL_SHAPE, h -> FabricAdapter.adapt((WallShape) h))); map.put(Properties.SOUTH_HEIGHT, - ImmutablePair.of(net.minecraft.state.property.Properties.SOUTH_WALL_SHAPE, h -> FabricAdapter.adapt((WallShape) h))); + Pair.of(net.minecraft.state.property.Properties.SOUTH_WALL_SHAPE, h -> FabricAdapter.adapt((WallShape) h))); map.put(Properties.EAST_HEIGHT, - ImmutablePair.of(net.minecraft.state.property.Properties.EAST_WALL_SHAPE, h -> FabricAdapter.adapt((WallShape) h))); + Pair.of(net.minecraft.state.property.Properties.EAST_WALL_SHAPE, h -> FabricAdapter.adapt((WallShape) h))); map.put(Properties.WEST_HEIGHT, - ImmutablePair.of(net.minecraft.state.property.Properties.WEST_WALL_SHAPE, h -> FabricAdapter.adapt((WallShape) h))); + Pair.of(net.minecraft.state.property.Properties.WEST_WALL_SHAPE, h -> FabricAdapter.adapt((WallShape) h))); map.put(Properties.DIRECTION, - ImmutablePair.of(net.minecraft.state.property.Properties.FACING, d -> FabricAdapter.adapt((Direction) d))); + Pair.of(net.minecraft.state.property.Properties.FACING, d -> FabricAdapter.adapt((Direction) d))); - map.put(Properties.WATERLOGGED, ImmutablePair.of(net.minecraft.state.property.Properties.WATERLOGGED, Function.identity())); + map.put(Properties.WATERLOGGED, Pair.of(net.minecraft.state.property.Properties.WATERLOGGED, Function.identity())); map.put(Properties.RAIL_SHAPE, - ImmutablePair.of(net.minecraft.state.property.Properties.RAIL_SHAPE, s -> FabricAdapter.adapt((RailShape) s))); + Pair.of(net.minecraft.state.property.Properties.RAIL_SHAPE, s -> FabricAdapter.adapt((RailShape) s))); map.put(Properties.HALF, - ImmutablePair.of(net.minecraft.state.property.Properties.BLOCK_HALF, h -> FabricAdapter.adapt((BlockHalf) h))); + Pair.of(net.minecraft.state.property.Properties.BLOCK_HALF, h -> FabricAdapter.adapt((BlockHalf) h))); return map; }); @@ -110,7 +110,7 @@ public class FabricBlockState implements BlockState { @SuppressWarnings("unchecked") @Override public T get(Property property) { - ImmutablePair, Function> pair = PROPERTY_DELEGATES_T2M.get(property); + Pair, Function> pair = PROPERTY_DELEGATES_T2M.get(property); return (T) pair.getRight().apply(delegate.get(pair.getLeft())); }