Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/twilightforest/util/ArmorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ public float getShroudedArmorPercentage(LivingEntity entity) {
return armorSlots > 0 ? (float) shroudedArmor / (float) armorSlots : 0.0F;
}
}

Comment thread
albazavr-alba marked this conversation as resolved.
Outdated
14 changes: 7 additions & 7 deletions src/main/java/twilightforest/util/BoundingBoxUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public static CompoundTag boundingBoxToExistingNBT(BoundingBox box, CompoundTag

public static BoundingBox NBTToBoundingBox(CompoundTag nbt) {
return new BoundingBox(
nbt.getInt("minX"),
Comment thread
albazavr-alba marked this conversation as resolved.
nbt.getInt("minY"),
nbt.getInt("minZ"),
nbt.getInt("maxX"),
nbt.getInt("maxY"),
nbt.getInt("maxZ")
nbt.getInt("minX").get(),
nbt.getInt("minY").get(),
nbt.getInt("minZ").get(),
nbt.getInt("maxX").get(),
nbt.getInt("maxY").get(),
nbt.getInt("maxZ").get()
);
}

Expand Down Expand Up @@ -92,7 +92,7 @@ public static BoundingBox getComponentToAddBoundingBox(int x, int y, int z, int
public static AABB vectorsMinMax(List<Vec3> vec3List, double expand) {
if (vec3List.isEmpty()) return null;

Vec3 first = vec3List.get(0);
Vec3 first = vec3List.getFirst();

return new AABB(
vec3List.stream().mapToDouble(Vec3::x).reduce(first.x, Math::min) - expand,
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/twilightforest/util/DisplayUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@

import com.mojang.math.Transformation;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.JsonOps;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.*;
import net.minecraft.network.chat.ComponentSerialization;
import net.minecraft.resources.RegistryOps;
import net.minecraft.util.ProblemReporter;
import net.minecraft.world.entity.Display;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntitySpawnReason;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.levelgen.structure.BoundingBox;
import net.minecraft.world.level.storage.TagValueInput;
import net.minecraft.world.level.storage.ValueInput;
import org.joml.Matrix4f;
import tamaized.beanification.Component;

Expand Down Expand Up @@ -43,7 +50,9 @@ public boolean spawnBlockDisplay(Level level, BoundingBox box, BlockState displa

entityNBT.putString("id", BuiltInRegistries.ENTITY_TYPE.getKey(EntityType.BLOCK_DISPLAY).toString());

Optional<Entity> spawned = EntityType.create(entityNBT, level);
ProblemReporter.Collector entitySpawnReporter = new ProblemReporter.Collector();
ValueInput valueInput = TagValueInput.create(entitySpawnReporter, level.registryAccess(), entityNBT);
Optional<Entity> spawned = EntityType.create(valueInput, level, EntitySpawnReason.LOAD);

if (spawned.isEmpty()) return false;
Entity entity = spawned.get();
Expand All @@ -65,7 +74,9 @@ public void setTextEntity(Level level, double x, double y, double z, Display.Bil
CompoundTag entityNBT = new CompoundTag();

entityNBT.put("Pos", this.newDoubleList(x, y, z));
entityNBT.putString("text", net.minecraft.network.chat.Component.Serializer.toJson(name, level.registryAccess()));
var registryOps = RegistryOps.create(JsonOps.INSTANCE, level.registryAccess());
String jsonText = ComponentSerialization.CODEC.encodeStart(registryOps, name).getOrThrow().toString();
entityNBT.putString("text", jsonText);

DataResult<Tag> serializedAlignment = Display.TextDisplay.Align.CODEC.encodeStart(NbtOps.INSTANCE, Display.TextDisplay.Align.CENTER);
if (serializedAlignment.isSuccess()) {
Expand All @@ -83,7 +94,9 @@ public void setTextEntity(Level level, double x, double y, double z, Display.Bil

entityNBT.putString("id", BuiltInRegistries.ENTITY_TYPE.getKey(EntityType.TEXT_DISPLAY).toString());

Optional<Entity> spawned = EntityType.create(entityNBT, level);
ProblemReporter.Collector entitySpawnReporter = new ProblemReporter.Collector();
ValueInput valueInput = TagValueInput.create(entitySpawnReporter, level.registryAccess(), entityNBT);
Optional<Entity> spawned = EntityType.create(valueInput, level, EntitySpawnReason.LOAD);

if (spawned.isEmpty()) return;
Entity entity = spawned.get();
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/twilightforest/util/MoonPhaseConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package twilightforest.util;

public class MoonPhaseConverter {
private static final String[] moonPhases = new String[]{
"full", "waning_gibbous", "third_quarter", "waning_crescent",
"new", "waxing_crescent", "first_quarter", "waxing_gibbous"
};


public static int convertPhaseToIndex(String phaseType) {
for (int i = 0; i < moonPhases.length; i++) {
if (moonPhases[i].equals(phaseType)) return i;
}
return 0;
}

public static String convertIndexToPhase(int index) {
return moonPhases[index];
}
}
Comment thread
albazavr-alba marked this conversation as resolved.
Outdated
9 changes: 4 additions & 5 deletions src/main/java/twilightforest/util/PlayerHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class PlayerHelper {
@Deprecated
public static void grantAdvancement(ServerPlayer player, Identifier id) {
PlayerAdvancements advancements = player.getAdvancements();
AdvancementHolder holder = player.getServer().getAdvancements().get(id);
AdvancementHolder holder = player.level().getServer().getAdvancements().get(id);
if (holder != null) {
for (String criterion : advancements.getOrStartProgress(holder).getRemainingCriteria()) {
advancements.award(holder, criterion);
Expand All @@ -33,7 +33,7 @@ public static void grantAdvancement(ServerPlayer player, Identifier id) {
@Deprecated
public static void grantCriterion(ServerPlayer player, Identifier id, String criterion) {
PlayerAdvancements advancements = player.getAdvancements();
AdvancementHolder holder = player.getAdvancements().get(id);
AdvancementHolder holder = player.level().getServer().getAdvancements().get(id);
if (holder != null) {
advancements.award(holder, criterion);
}
Expand All @@ -46,7 +46,7 @@ public static AdvancementHolder getAdvancement(Player player, Identifier advance
ClientAdvancements manager = localPlayer.connection.getAdvancements();
return manager.get(advancementLocation);
} else if (player instanceof ServerPlayer serverPlayer) {
ServerLevel world = (ServerLevel) serverPlayer.level();
ServerLevel world = serverPlayer.level();
return world.getServer().getAdvancements().get(advancementLocation);
}

Expand All @@ -62,13 +62,12 @@ public static boolean doesPlayerHaveRequiredAdvancement(Player player, @Nullable
AdvancementProgress progress = manager.progress.get(holder);
return progress != null && progress.isDone();
}
return false;
} else {
if (player instanceof ServerPlayer) {
return holder != null && ((ServerPlayer) player).getAdvancements().getOrStartProgress(holder).isDone();
}
return false;
}
return false;
}

public static boolean doesPlayerHaveRequiredAdvancements(Player player, List<Identifier> requiredAdvancements) {
Expand Down
56 changes: 42 additions & 14 deletions src/main/java/twilightforest/util/Restriction.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package twilightforest.util;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.serialization.Codec;
import com.mojang.serialization.Dynamic;
import com.mojang.serialization.DynamicOps;
import com.mojang.serialization.JsonOps;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.RegistryOps;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.Identifier;
import net.minecraft.util.ExtraCodecs;
Expand All @@ -13,6 +19,7 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.levelgen.structure.Structure;
import org.jetbrains.annotations.NotNull;
import twilightforest.TFRegistries;

import javax.annotation.Nullable;
Expand All @@ -26,22 +33,34 @@
* @param lockedBiomeToast Item that is used as an icon for the notification that tells the player that the area is locked
* @param advancements List of advancements that are required to make a biome no longer restricted
*/
Comment thread
albazavr-alba marked this conversation as resolved.

public record Restriction(@Nullable ResourceKey<Structure> hintStructureKey, ResourceKey<Enforcement> enforcement,
public record Restriction(@Nullable ResourceKey<@NotNull Structure> hintStructureKey, ResourceKey<@NotNull Enforcement> enforcement,
float multiplier, @Nullable ItemStack lockedBiomeToast, List<Identifier> advancements) {

public static final Codec<Restriction> CODEC = RecordCodecBuilder.create((recordCodecBuilder) -> recordCodecBuilder.group(
ResourceKey.codec(Registries.STRUCTURE).optionalFieldOf("structure_key").forGetter((restriction) -> Optional.ofNullable(restriction.hintStructureKey())),
ResourceKey.codec(TFRegistries.Keys.ENFORCEMENT).fieldOf("enforcement").forGetter(Restriction::enforcement),
Codec.FLOAT.fieldOf("multiplier").forGetter(Restriction::multiplier),
ItemStack.CODEC.optionalFieldOf("locked_biome_toast").forGetter((restriction) -> Optional.ofNullable(restriction.lockedBiomeToast())),
Comment thread
albazavr-alba marked this conversation as resolved.
Outdated
Codec.PASSTHROUGH.optionalFieldOf("locked_biome_toast").xmap(
optionalDynamic -> optionalDynamic.map(dynamic -> {
JsonElement json = (JsonElement) dynamic.getValue();
DynamicOps<JsonElement> ops = RegistryOps.create(
JsonOps.INSTANCE,
RegistryAccess.EMPTY
);
return ItemStack.CODEC.parse(ops, json).result().orElse(ItemStack.EMPTY);
}),
optionalStack -> optionalStack.map(stack -> {
DynamicOps<JsonElement> ops = RegistryOps.create(
JsonOps.INSTANCE,
RegistryAccess.EMPTY
);
JsonElement json = ItemStack.CODEC.encodeStart(ops, stack).result().orElse(new JsonObject());
return new Dynamic<>(JsonOps.INSTANCE, json);
})
).forGetter((restriction) -> Optional.ofNullable(restriction.lockedBiomeToast())),
Comment thread
albazavr-alba marked this conversation as resolved.
Outdated

ExtraCodecs.nonEmptyList(Identifier.CODEC.listOf()).fieldOf("advancements").forGetter(Restriction::advancements)
).apply(recordCodecBuilder, Restriction::create));

@SuppressWarnings("OptionalUsedAsFieldOrParameterType") // Vanilla does this too
private static Restriction create(Optional<ResourceKey<Structure>> hintStructureKey, ResourceKey<Enforcement> enforcer, float multiplier, Optional<ItemStack> lockedBiomeToast, List<Identifier> advancements) {
return new Restriction(hintStructureKey.orElse(null), enforcer, multiplier, lockedBiomeToast.orElse(null), advancements);
}

public static Optional<Restriction> getRestrictionForBiome(Biome biome, Entity entity) {
if (!(entity instanceof Player player))
Expand All @@ -52,19 +71,28 @@ public static Optional<Restriction> getRestrictionForBiome(Biome biome, Entity e
if (biomeLocation == null)
return Optional.empty();

Optional<Registry<Restriction>> restrictionsRegistry = access.lookup(TFRegistries.Keys.RESTRICTIONS);
Optional<Registry<@NotNull Restriction>> restrictionsRegistry = access.lookup(TFRegistries.Keys.RESTRICTIONS);
if (restrictionsRegistry.isEmpty())
return Optional.empty();

Restriction restrictions = restrictionsRegistry.get().get(biomeLocation);
if (restrictions == null || PlayerHelper.doesPlayerHaveRequiredAdvancements(player, restrictions.advancements())) {
return Optional.empty();
if (restrictionsRegistry.get().get(biomeLocation).isPresent()) {
Restriction restrictions = restrictionsRegistry.get().get(biomeLocation).get().value();
if (PlayerHelper.doesPlayerHaveRequiredAdvancements(player, restrictions.advancements())) {
return Optional.empty();
}

return Optional.of(restrictions);
}

return Optional.of(restrictions);
return Optional.empty();
}

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
private static Restriction create(Optional<ResourceKey<@NotNull Structure>> hintStructureKey, ResourceKey<@NotNull Enforcement> enforcer, float multiplier, Optional<ItemStack> lockedBiomeToast, List<Identifier> advancements) {
return new Restriction(hintStructureKey.orElse(null), enforcer, multiplier, lockedBiomeToast.orElse(null), advancements);
}

public static boolean isBiomeSafeFor(Biome biome, Entity entity) {
return getRestrictionForBiome(biome, entity).isEmpty();
}
}
}
Loading
Loading