Skip to content

Commit

Permalink
move from byte serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
PssbleTrngle committed Jul 30, 2023
1 parent b65841f commit 804001a
Show file tree
Hide file tree
Showing 19 changed files with 188 additions and 93 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ plugins {
java
id("maven-publish")
id("architectury-plugin") version ("3.4-SNAPSHOT")
id("dev.architectury.loom") version ("1.1-SNAPSHOT") apply (false)
id("dev.architectury.loom") version ("1.3-SNAPSHOT") apply (false)
id("org.sonarqube") version ("3.3")
id("net.darkhax.curseforgegradle") version ("1.0.8") apply (false)
id("com.modrinth.minotaur") version ("2.+") apply (false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.possible_triangle.brazier;

import com.possible_triangle.brazier.config.ClientConfig;
import com.possible_triangle.brazier.config.IServerConfig;
import com.possible_triangle.brazier.config.ServerConfig;
import com.possible_triangle.brazier.item.BrazierIndicator;
import com.possible_triangle.brazier.network.BrazierNetwork;
import com.possible_triangle.brazier.network.SyncConfigMessage;
import dev.architectury.event.events.common.LifecycleEvent;
import dev.architectury.event.events.common.PlayerEvent;
import dev.architectury.event.events.common.TickEvent;
import me.shedaniel.autoconfig.AutoConfig;
Expand All @@ -23,7 +23,7 @@ private Brazier() {}
public static final String MOD_ID = "brazier";

private static ConfigHolder<ServerConfig> localServerConfig;
private static ServerConfig syncedServerConfig;
private static IServerConfig syncedServerConfig;
private static ConfigHolder<ClientConfig> clientConfig;

public static final Logger LOGGER = LogManager.getLogger();
Expand All @@ -32,11 +32,11 @@ public static ClientConfig clientConfig() {
return clientConfig.get();
}

public static ServerConfig serverConfig() {
public static IServerConfig serverConfig() {
return Optional.ofNullable(syncedServerConfig).orElseGet(localServerConfig);
}

public static void setSyncedConfig(ServerConfig config) {
public static void setSyncedConfig(IServerConfig config) {
syncedServerConfig = config;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.possible_triangle.brazier;

import com.mojang.datafixers.util.Pair;
import com.possible_triangle.brazier.config.ServerConfig;
import com.possible_triangle.brazier.config.IServerConfig;
import dev.architectury.registry.registries.RegistrySupplier;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -26,15 +26,15 @@ public class Conditional {
private static final List<Conditional> CONDITIONALS = new ArrayList<>();

@SafeVarargs
public static Conditional when(Predicate<ServerConfig> condition, RegistrySupplier<? extends ItemLike>... items) {
public static Conditional when(Predicate<IServerConfig> condition, RegistrySupplier<? extends ItemLike>... items) {
Conditional conditional = new Conditional(condition);
CONDITIONALS.add(conditional);
if (items.length > 0) conditional.add(items);
return conditional;
}

public static Stream<ItemLike> disabled() {
ServerConfig config = Brazier.serverConfig();
var config = Brazier.serverConfig();
return CONDITIONALS.stream()
.filter(e -> !e.condition.test(config))
.map(c -> c.items)
Expand All @@ -44,7 +44,7 @@ public static Stream<ItemLike> disabled() {
}

public static void injectLoot(ResourceLocation target, Consumer<LootPool.Builder> table) {
ServerConfig config = Brazier.serverConfig();
var config = Brazier.serverConfig();

CONDITIONALS.stream()
.filter(e -> e.condition.test(config))
Expand Down Expand Up @@ -80,11 +80,11 @@ public static void removeHidden(Consumer<Collection<ItemStack>> consumer) {
consumer.accept(hidden);
}

public final Predicate<ServerConfig> condition;
public final Predicate<IServerConfig> condition;
public final List<RegistrySupplier<? extends ItemLike>> items = new ArrayList<>();
public final List<Pair<ResourceLocation, ResourceLocation>> loot = new ArrayList<>();

private Conditional(Predicate<ServerConfig> condition) {
private Conditional(Predicate<IServerConfig> condition) {
this.condition = condition;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.possible_triangle.brazier.block.LazyWallTorchBlock;
import com.possible_triangle.brazier.block.SpawnPowder;
import com.possible_triangle.brazier.block.tile.BrazierTile;
import com.possible_triangle.brazier.config.IServerConfig;
import com.possible_triangle.brazier.entity.Crazed;
import com.possible_triangle.brazier.entity.CrazedFlame;
import com.possible_triangle.brazier.entity.EntityUtil;
Expand Down Expand Up @@ -125,10 +126,10 @@ public static void init() {
}

public static void setup() {
Conditional.when(config -> config.DECORATION, LIVING_LANTERN, LIVING_TORCH);
Conditional.when(config -> config.SPAWN_POWDER, SPAWN_POWDER);
Conditional.when(IServerConfig::enableDecoration, LIVING_LANTERN, LIVING_TORCH);
Conditional.when(IServerConfig::enableSpawnPowder, SPAWN_POWDER);

Conditional.when(config -> config.JUNGLE_LOOT).loot(BuiltInLootTables.JUNGLE_TEMPLE, "flame_jungle_temple");
Conditional.when(IServerConfig::injectJungleLoot).loot(BuiltInLootTables.JUNGLE_TEMPLE, "flame_jungle_temple");

Conditional.when($ -> !Platform.isModLoaded("nether_extension") && !Platform.isModLoaded("supplementaries"))
.add(Content.ASH, Content.WARPED_NETHERWART)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.possible_triangle.brazier.Brazier;
import com.possible_triangle.brazier.Content;
import com.possible_triangle.brazier.block.BrazierBlock;
import com.possible_triangle.brazier.config.ServerConfig;
import com.possible_triangle.brazier.logic.BrazierLogic;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -61,7 +60,7 @@ private void checkStructure(BlockPos pos, BlockState state) {

private int findHeight() {
assert level != null;
int max = Brazier.serverConfig().MAX_HEIGHT;
int max = Brazier.serverConfig().maxHeight();
var pos = getBlockPos();
if (!level.getBlockState(pos.above()).isAir()) return 0;
for (int y = 1; y <= max; y++) {
Expand Down Expand Up @@ -97,8 +96,8 @@ public BrazierTile(BlockPos pos, BlockState state) {

public int getRange() {
if (height <= 0) return 0;
ServerConfig config = Brazier.serverConfig();
return config.BASE_RANGE + config.RANGE_PER_LEVEL * (height - 1);
var config = Brazier.serverConfig();
return config.baseRange() + config.rangePerLevel() * (height - 1);
}

public int getHeight() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public enum Type {
}

public static double getDistance(Vec3 from, BlockPos to) {
return Brazier.serverConfig().DISTANCE_CALC.calc.apply(from, to);
return Brazier.serverConfig().distanceCalculator().calc.apply(from, to);
}

public static double getDistance(BlockPos from, BlockPos to) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.possible_triangle.brazier.config;

public interface IServerConfig {

boolean injectJungleLoot();

boolean spawnCrazed();

double crazedSpawnChance();

int maxHeight();

int rangePerLevel();

int baseRange();

boolean protectAbove();

DistanceHandler.Type distanceCalculator();

boolean enableSpawnPowder();

boolean enableDecoration();


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,90 @@

@Config(name = "brazier-common")
@Config.Gui.Background("minecraft:textures/block/blackstone.png")
public class ServerConfig implements ConfigData {
public class ServerConfig implements ConfigData, IServerConfig {

@ConfigEntry.Category("acquisition")
public boolean JUNGLE_LOOT = true;

@Override
public boolean injectJungleLoot() {
return JUNGLE_LOOT;
}

@ConfigEntry.Category("acquisition")
public boolean SPAWN_CRAZED = true;

@Override
public boolean spawnCrazed() {
return SPAWN_CRAZED;
}

@ConfigEntry.Category("acquisition")
public double CRAZED_CHANCE = 0.6;

@Override
public double crazedSpawnChance() {
return CRAZED_CHANCE;
}

@ConfigEntry.Category("brazier")
@ConfigEntry.BoundedDiscrete(min = 1, max = Integer.MAX_VALUE)
public int MAX_HEIGHT = 10;

@Override
public int maxHeight() {
return MAX_HEIGHT;
}

@ConfigEntry.Category("brazier")
@ConfigEntry.BoundedDiscrete(min = 0, max = Integer.MAX_VALUE)
public int RANGE_PER_LEVEL = 10;

@Override
public int rangePerLevel() {
return RANGE_PER_LEVEL;
}

@ConfigEntry.Category("brazier")
@ConfigEntry.BoundedDiscrete(min = 0, max = Integer.MAX_VALUE)
public int BASE_RANGE = 20;

@Override
public int baseRange() {
return BASE_RANGE;
}

@ConfigEntry.Category("brazier")
public boolean PROTECT_ABOVE = false;

@Override
public boolean protectAbove() {
return PROTECT_ABOVE;
}

@ConfigEntry.Category("brazier")
@ConfigEntry.Gui.Tooltip
@ConfigEntry.Gui.EnumHandler(option = ConfigEntry.Gui.EnumHandler.EnumDisplayOption.BUTTON)
public DistanceHandler.Type DISTANCE_CALC = DistanceHandler.Type.CYLINDER;

@Override
public DistanceHandler.Type distanceCalculator() {
return DISTANCE_CALC;
}

@ConfigEntry.Category("content")
public boolean SPAWN_POWDER = true;

@Override
public boolean enableSpawnPowder() {
return SPAWN_POWDER;
}

@ConfigEntry.Category("content")
public boolean DECORATION = true;


@Override
public boolean enableDecoration() {
return DECORATION;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.possible_triangle.brazier.config;

public record SyncedServerConfig(
boolean injectJungleLoot,
boolean spawnCrazed,
double crazedSpawnChance,
int maxHeight,
int rangePerLevel,
int baseRange,
boolean protectAbove,
DistanceHandler.Type distanceCalculator,
boolean enableSpawnPowder,
boolean enableDecoration

) implements IServerConfig {

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static boolean isBorder(Vec3 pos, ResourceKey<Level> dimension) {
public static boolean inRange(BlockPos pos, ResourceKey<Level> dimension) {
synchronized (BRAZIERS) {
return BRAZIERS.containsKey(dimension) && BRAZIERS.get(dimension).entrySet().stream().anyMatch(e -> {
if (!Brazier.serverConfig().PROTECT_ABOVE && e.getKey().getY() < pos.getY()) return false;
if (!Brazier.serverConfig().protectAbove() && e.getKey().getY() < pos.getY()) return false;
double dist = DistanceHandler.getDistance(pos, e.getKey());
int maxDist = e.getValue() * e.getValue();
return dist <= maxDist;
Expand Down Expand Up @@ -81,7 +81,7 @@ public static boolean prevents(Entity entity, LevelAccessor world, MobSpawnType
BlockPos pos = entity.blockPosition();

// Check for spawn powder
if (Brazier.serverConfig().SPAWN_POWDER) {
if (Brazier.serverConfig().enableSpawnPowder()) {
Block block = world.getBlockState(pos).getBlock();
if (Content.SPAWN_POWDER.toOptional().filter(block::equals).isPresent()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.possible_triangle.brazier.Brazier;
import com.possible_triangle.brazier.Content;
import com.possible_triangle.brazier.config.ServerConfig;
import com.possible_triangle.brazier.entity.Crazed;
import net.minecraft.core.BlockPos;
import net.minecraft.util.RandomSource;
Expand All @@ -21,9 +20,9 @@ public class WoodlandMansionPiecesMixin {

@Inject(at = @At("HEAD"), cancellable = true, method = "handleDataMarker(Ljava/lang/String;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/ServerLevelAccessor;Lnet/minecraft/util/RandomSource;Lnet/minecraft/world/level/levelgen/structure/BoundingBox;)V")
public void handleDataMarker(String function, BlockPos pos, ServerLevelAccessor world, RandomSource rand, BoundingBox ssb, CallbackInfo callback) {
ServerConfig config = Brazier.serverConfig();
if (config.SPAWN_CRAZED) Content.CRAZED.ifPresent(type -> {
if (function.equals("Mage") && config.CRAZED_CHANCE > 0 && rand.nextDouble() <= config.CRAZED_CHANCE) {
var config = Brazier.serverConfig();
if (config.spawnCrazed()) Content.CRAZED.ifPresent(type -> {
if (function.equals("Mage") && config.crazedSpawnChance() > 0 && rand.nextDouble() <= config.crazedSpawnChance()) {
Crazed crazed = type.create(world.getLevel());
assert crazed != null;
crazed.setPersistenceRequired();
Expand Down

This file was deleted.

Loading

0 comments on commit 804001a

Please sign in to comment.