Skip to content

Commit

Permalink
Add Some Env.Json Visitors
Browse files Browse the repository at this point in the history
Change fabric.mod.json
  • Loading branch information
FirstMegaGame4 committed Dec 20, 2023
1 parent dc0713b commit bd2dfce
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package fr.firstmegagame4.env.driven.assets;

import fr.firstmegagame4.env.driven.assets.impl.env.json.BlockEnvJsonVisitor;
import fr.firstmegagame4.env.driven.assets.impl.env.json.ClientEnvJsonVisitor;
import fr.firstmegagame4.env.driven.assets.impl.env.json.EntityEnvJsonVisitor;
import fr.firstmegagame4.env.json.api.EnvJsonVisitor;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class EDAEnvJsonVisitors {

public static EnvJsonVisitor blockVisitor(World world, BlockPos pos) {
return new BlockEnvJsonVisitor(world, pos);
}

public static EnvJsonVisitor clientVisitor(MinecraftClient client) {
return new ClientEnvJsonVisitor(client);
}

public static EnvJsonVisitor entityVisitor(Entity entity) {
return new EntityEnvJsonVisitor(entity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package fr.firstmegagame4.env.driven.assets.impl.env.json;

import fr.firstmegagame4.env.json.api.EnvJsonVisitor;
import fr.firstmegagame4.env.json.api.rule.SkyEnvJsonRule;
import fr.firstmegagame4.env.json.api.rule.VoidEnvJsonRule;
import fr.firstmegagame4.env.json.api.rule.WaterEnvJsonRule;
import it.unimi.dsi.fastutil.ints.Int2BooleanFunction;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.tag.FluidTags;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public class BlockEnvJsonVisitor implements EnvJsonVisitor {

private final World world;
private final BlockPos pos;

public BlockEnvJsonVisitor(World world, BlockPos pos) {
this.world = world;
this.pos = pos;
}

@Override
public boolean applyDimensionKey(RegistryKey<World> dimensionKey) {
return this.world.getRegistryKey() == dimensionKey;
}

@Override
public boolean applyDimensionTag(TagKey<World> dimensionTag) {
return false;
}

@Override
public boolean applyBiomeKey(RegistryKey<Biome> biomeKey) {
return this.world.getBiome(this.pos).matchesKey(biomeKey);
}

@Override
public boolean applyBiomeTag(TagKey<Biome> biomeTag) {
return this.world.getBiome(this.pos).isIn(biomeTag);
}

@Override
public boolean applyXCoord(Int2BooleanFunction operation) {
return operation.get(this.pos.getX());
}

@Override
public boolean applyYCoord(Int2BooleanFunction operation) {
return operation.get(this.pos.getY());
}

@Override
public boolean applyZCoord(Int2BooleanFunction operation) {
return operation.get(this.pos.getZ());
}

@Override
public boolean applySubmerged(boolean submerged) {
return submerged && this.world.getBlockState(this.pos.up()).getFluidState().isIn(FluidTags.WATER);
}

@Override
public boolean applySky(SkyEnvJsonRule.Localization localization) {
return switch (localization) {
case BELOW -> this.pos.getY() <= this.world.getTopY();
case ABOVE -> this.pos.getY() > this.world.getTopY();
};
}

@Override
public boolean applyWater(WaterEnvJsonRule.Localization localization) {
return switch (localization) {
case BELOW -> this.pos.getY() <= this.world.getSeaLevel();
case ABOVE -> this.pos.getY() > this.world.getSeaLevel();
};
}

@Override
public boolean applyVoid(VoidEnvJsonRule.Localization localization) {
return switch (localization) {
case BELOW -> this.pos.getY() <= this.world.getBottomY();
case ABOVE -> this.pos.getY() > this.world.getBottomY();
};
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.firstmegagame4.env.driven.assets.mixin.client;
package fr.firstmegagame4.env.driven.assets.impl.env.json;

import fr.firstmegagame4.env.json.api.EnvJsonVisitor;
import fr.firstmegagame4.env.json.api.rule.SkyEnvJsonRule;
Expand All @@ -8,13 +8,22 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.tag.FluidTags;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import org.jetbrains.annotations.ApiStatus;

import java.util.Optional;

public class ClientPlayerVisitor implements EnvJsonVisitor {
@ApiStatus.Internal
public class ClientEnvJsonVisitor implements EnvJsonVisitor {

private final MinecraftClient client;

public ClientEnvJsonVisitor(MinecraftClient client) {
this.client = client;
}

@Override
public boolean applyDimensionKey(RegistryKey<World> dimensionKey) {
Expand All @@ -28,7 +37,7 @@ public boolean applyDimensionTag(TagKey<World> dimensionTag) {

@Override
public boolean applyBiomeKey(RegistryKey<Biome> biomeKey) {
return this.player().isPresent() && this.player().get().clientWorld.getBiome(this.player().get().getBlockPos()) == biomeKey;
return this.player().isPresent() && this.player().get().clientWorld.getBiome(this.player().get().getBlockPos()).matchesKey(biomeKey);
}

@Override
Expand All @@ -53,34 +62,34 @@ public boolean applyZCoord(Int2BooleanFunction operation) {

@Override
public boolean applySubmerged(boolean submerged) {
return false;
return submerged && this.player().isPresent() && this.player().get().clientWorld.getBlockState(this.player().get().getBlockPos().up()).getFluidState().isIn(FluidTags.WATER);
}

@Override
public boolean applySky(SkyEnvJsonRule.Localization localization) {
return false;
return this.player().isPresent() && switch (localization) {
case BELOW -> this.player().get().getBlockY() <= this.player().get().clientWorld.getTopY();
case ABOVE -> this.player().get().getBlockY() > this.player().get().clientWorld.getTopY();
};
}

@Override
public boolean applyWater(WaterEnvJsonRule.Localization localization) {
return false;
return this.player().isPresent() && switch (localization) {
case BELOW -> this.player().get().getBlockY() <= this.player().get().clientWorld.getTopY();
case ABOVE -> this.player().get().getBlockY() > this.player().get().clientWorld.getTopY();
};
}

@Override
public boolean applyVoid(VoidEnvJsonRule.Localization localization) {
return false;
}

private Optional<MinecraftClient> client() {
return Optional.ofNullable(MinecraftClient.getInstance());
return this.player().isPresent() && switch (localization) {
case BELOW -> this.player().get().getBlockY() <= this.player().get().clientWorld.getTopY();
case ABOVE -> this.player().get().getBlockY() > this.player().get().clientWorld.getTopY();
};
}

private Optional<ClientPlayerEntity> player() {
if (this.client().isPresent()) {
return Optional.ofNullable(this.client().get().player);
}
else {
return Optional.empty();
}
return Optional.ofNullable(this.client.player);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package fr.firstmegagame4.env.driven.assets.impl.env.json;

import fr.firstmegagame4.env.json.api.EnvJsonVisitor;
import fr.firstmegagame4.env.json.api.rule.SkyEnvJsonRule;
import fr.firstmegagame4.env.json.api.rule.VoidEnvJsonRule;
import fr.firstmegagame4.env.json.api.rule.WaterEnvJsonRule;
import it.unimi.dsi.fastutil.ints.Int2BooleanFunction;
import net.minecraft.entity.Entity;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public class EntityEnvJsonVisitor implements EnvJsonVisitor {

private final Entity entity;

public EntityEnvJsonVisitor(Entity entity) {
this.entity = entity;
}

@Override
public boolean applyDimensionKey(RegistryKey<World> dimensionKey) {
return this.entity.getWorld().getRegistryKey() == dimensionKey;
}

@Override
public boolean applyDimensionTag(TagKey<World> dimensionTag) {
return false;
}

@Override
public boolean applyBiomeKey(RegistryKey<Biome> biomeKey) {
return this.entity.getWorld().getBiome(this.entity.getBlockPos()).matchesKey(biomeKey);
}

@Override
public boolean applyBiomeTag(TagKey<Biome> biomeTag) {
return this.entity.getWorld().getBiome(this.entity.getBlockPos()).isIn(biomeTag);
}

@Override
public boolean applyXCoord(Int2BooleanFunction operation) {
return operation.get(this.entity.getBlockX());
}

@Override
public boolean applyYCoord(Int2BooleanFunction operation) {
return operation.get(this.entity.getBlockY());
}

@Override
public boolean applyZCoord(Int2BooleanFunction operation) {
return operation.get(this.entity.getBlockZ());
}

@Override
public boolean applySubmerged(boolean submerged) {
return submerged;
}

@Override
public boolean applySky(SkyEnvJsonRule.Localization localization) {
return switch (localization) {
case BELOW -> this.entity.getBlockY() <= this.entity.getWorld().getTopY();
case ABOVE -> this.entity.getBlockY() > this.entity.getWorld().getTopY();
};
}

@Override
public boolean applyWater(WaterEnvJsonRule.Localization localization) {
return switch (localization) {
case BELOW -> this.entity.getBlockY() <= this.entity.getWorld().getSeaLevel();
case ABOVE -> this.entity.getBlockY() > this.entity.getWorld().getSeaLevel();
};
}

@Override
public boolean applyVoid(VoidEnvJsonRule.Localization localization) {
return switch (localization) {
case BELOW -> this.entity.getBlockY() <= this.entity.getWorld().getBottomY();
case ABOVE -> this.entity.getBlockY() > this.entity.getWorld().getBottomY();
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EnvironmentDrivenAssetsCommon implements ModInitializer {
public class EDACommons implements ModInitializer {
// This logger is used to write text to the console and the log file.
// It is considered best practice to use your mod id as the logger's name.
// That way, it's clear which mod wrote info, warnings, and errors.
Expand Down
10 changes: 5 additions & 5 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
"FirstMegaGame4"
],
"contact": {
"homepage": "https://fabricmc.net/",
"sources": "https://github.com/FabricMC/fabric-example-mod"
"homepage": "https://modrinth.com/mod/env-driven-assets",
"sources": "https://github.com/FirstMegaGame4/env-driven-assets"
},
"license": "CC0-1.0",
"license": "LGPL-3.0",
"icon": "assets/env_driven_assets/icon.png",
"environment": "*",
"environment": "client",
"entrypoints": {
"main": [
"fr.firstmegagame4.env.driven.assets.EnvironmentDrivenAssetsCommon"
"fr.firstmegagame4.env.driven.assets.EDACommons"
],
"client": [
"fr.firstmegagame4.env.driven.assets.EnvironmentDrivenAssets"
Expand Down

0 comments on commit bd2dfce

Please sign in to comment.