From 3a83a9f82cc553e5b0555a2278b8d2d98774f967 Mon Sep 17 00:00:00 2001 From: lunathelemon Date: Mon, 4 Sep 2023 13:16:45 +0100 Subject: [PATCH] Try to improve things lol --- .../lunathelemon/territorial/Territorial.java | 12 ++-- .../territorial/block/TerritorialBlocks.java | 5 +- .../entity/CorruptedBeaconBlockEntity.java | 2 +- .../entity/CorruptedPyramidBlockEntity.java | 59 +++++++++++++++++++ .../entity/PlinthOfPeekingBlockEntity.java | 12 +++- .../entity/TerritorialBlockEntities.java | 2 +- .../render/entity/PeekingEyeRenderer.java | 2 +- .../component/TerritorialComponents.java | 2 +- .../territorial/init/ItemGroupRegistry.java | 2 +- .../init/TerritorialDamageTypes.java | 2 - .../territorial/init/TerritorialTags.java | 15 +++++ .../territorial/item/LensItem.java | 5 +- .../mixin/common/BeaconBlockEntityMixin.java | 2 +- .../territorial/util/BeaconUtils.java | 49 ++++++++++++--- .../tags/blocks/corrupted_pyramid.json | 8 +++ 15 files changed, 152 insertions(+), 27 deletions(-) create mode 100644 src/main/java/io/github/lunathelemon/territorial/block/entity/CorruptedPyramidBlockEntity.java create mode 100644 src/main/java/io/github/lunathelemon/territorial/init/TerritorialTags.java create mode 100644 src/main/resources/data/territorial/tags/blocks/corrupted_pyramid.json diff --git a/src/main/java/io/github/lunathelemon/territorial/Territorial.java b/src/main/java/io/github/lunathelemon/territorial/Territorial.java index eb1ea59..1b028e9 100644 --- a/src/main/java/io/github/lunathelemon/territorial/Territorial.java +++ b/src/main/java/io/github/lunathelemon/territorial/Territorial.java @@ -1,5 +1,6 @@ package io.github.lunathelemon.territorial; +import io.github.lunathelemon.territorial.api.TerritorialAPI; import io.github.lunathelemon.territorial.api.event.CorruptionEvents; import io.github.lunathelemon.territorial.block.TerritorialBlocks; import io.github.lunathelemon.territorial.block.entity.CorruptedBeaconBlockEntity; @@ -13,17 +14,14 @@ import me.shedaniel.autoconfig.AutoConfig; import me.shedaniel.autoconfig.serializer.JanksonConfigSerializer; import net.fabricmc.api.ModInitializer; -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents; -import net.minecraft.world.dimension.NetherPortal; +import net.minecraft.util.Identifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.management.ManagementFactory; public class Territorial implements ModInitializer { - - public static final String MOD_ID = "territorial"; - public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); + public static final Logger LOGGER = LoggerFactory.getLogger(TerritorialAPI.MOD_ID); public static final boolean IS_DEBUG_MODE = isDebugMode(); @Override @@ -43,6 +41,10 @@ public void onInitialize() { C2SPacketRegistry.register(); } + public static Identifier getID(String path) { + return new Identifier(TerritorialAPI.MOD_ID, path); + } + private static boolean isDebugMode() { boolean debugMode = false; for (String arg : ManagementFactory.getRuntimeMXBean().getInputArguments()) { diff --git a/src/main/java/io/github/lunathelemon/territorial/block/TerritorialBlocks.java b/src/main/java/io/github/lunathelemon/territorial/block/TerritorialBlocks.java index 825a6f3..d5301f2 100644 --- a/src/main/java/io/github/lunathelemon/territorial/block/TerritorialBlocks.java +++ b/src/main/java/io/github/lunathelemon/territorial/block/TerritorialBlocks.java @@ -9,7 +9,6 @@ import net.minecraft.item.BlockItem; import net.minecraft.registry.Registries; import net.minecraft.registry.Registry; -import net.minecraft.util.Identifier; public final class TerritorialBlocks { @@ -38,11 +37,11 @@ private static void register(String id, Block block) { register(id, block, true, true); } private static void register(String id, Block block, boolean registerBlockItem, boolean addToGroupRegistry) { - Registry.register(Registries.BLOCK, new Identifier(Territorial.MOD_ID, id), block); + Registry.register(Registries.BLOCK, Territorial.getID(id), block); if (registerBlockItem) { var blockItem = new BlockItem(block, new FabricItemSettings()); - Registry.register(Registries.ITEM, new Identifier(Territorial.MOD_ID, id), blockItem); + Registry.register(Registries.ITEM, Territorial.getID(id), blockItem); if(addToGroupRegistry) ItemGroupRegistry.queueStackRegistration(blockItem.getDefaultStack()); diff --git a/src/main/java/io/github/lunathelemon/territorial/block/entity/CorruptedBeaconBlockEntity.java b/src/main/java/io/github/lunathelemon/territorial/block/entity/CorruptedBeaconBlockEntity.java index fcd2510..ac0c309 100644 --- a/src/main/java/io/github/lunathelemon/territorial/block/entity/CorruptedBeaconBlockEntity.java +++ b/src/main/java/io/github/lunathelemon/territorial/block/entity/CorruptedBeaconBlockEntity.java @@ -144,7 +144,7 @@ public static void tick(World world, BlockPos pos, BlockState state, CorruptedBe i = blockEntity.level; if (world.getTime() % 80L == 0L) { if (!blockEntity.beamSegments.isEmpty()) { - blockEntity.level = BeaconUtils.updateLevel(world, pos, List.of(TerritorialBlocks.OMNISCIENT_OBSIDIAN)); + blockEntity.level = BeaconUtils.updateCorruptedBeaconLevel(world, pos); } if (blockEntity.level > 0 && !blockEntity.beamSegments.isEmpty()) { applyPlayerEffects(world, pos, blockEntity.level, blockEntity.primary, blockEntity.secondary); diff --git a/src/main/java/io/github/lunathelemon/territorial/block/entity/CorruptedPyramidBlockEntity.java b/src/main/java/io/github/lunathelemon/territorial/block/entity/CorruptedPyramidBlockEntity.java new file mode 100644 index 0000000..d475374 --- /dev/null +++ b/src/main/java/io/github/lunathelemon/territorial/block/entity/CorruptedPyramidBlockEntity.java @@ -0,0 +1,59 @@ +package io.github.lunathelemon.territorial.block.entity; + +import io.github.lunathelemon.territorial.block.TerritorialBlocks; +import io.github.lunathelemon.territorial.init.TerritorialTags; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.BlockEntityType; +import net.minecraft.util.math.BlockPos; + +public abstract class CorruptedPyramidBlockEntity extends BlockEntity { + private static final int[] beaconSizes = { 9, 34, 83, 164 }; + protected Block mainPyramidBlock = TerritorialBlocks.OMNISCIENT_OBSIDIAN; + protected float mainBlockFormationThreshold = 0.5f; + + public CorruptedPyramidBlockEntity(BlockEntityType type, BlockPos pos, BlockState state) { + super(type, pos, state); + } + + protected int updateLevel() { + final int bottomY = world.getBottomY(); + + int level = 0, mainPyramidBlockCount = 0, y, xPos, zPos; + BlockState currentState; + + // Decrement the y value to check the blocks below + for(int yInc = 1; yInc <= 4; level = yInc++) { + y = pos.getY() - yInc; + // If we reach the bottom of the world exist early + if(y < bottomY) + break; + else { + xPos = pos.getX(); + zPos = pos.getZ(); + // Check each slice of blocks below in the same way a beacon would + for(int x = xPos - yInc; x <= xPos + yInc; ++x) { + for(int z = zPos - yInc; z <= zPos + yInc; ++z) { + currentState = world.getBlockState(new BlockPos(x, y, z)); + + // Return the current level if a new block cannot be found + if(!currentState.isIn(TerritorialTags.Blocks.CORRUPTED_PYRAMID)) { + return checkFormationThreshold(mainPyramidBlockCount, level); + } else if(currentState.getBlock().equals(mainPyramidBlock)) { + mainPyramidBlockCount++; + } + } + } + } + } + return checkFormationThreshold(mainPyramidBlockCount, level); + } + + protected int checkFormationThreshold(int mainPyramidBlockCount, int level) { + if(mainBlockFormationThreshold > 0 && level > 0) + return (float) mainPyramidBlockCount / beaconSizes[level-1] >= mainBlockFormationThreshold ? level : 0; + else + return level; + } +} diff --git a/src/main/java/io/github/lunathelemon/territorial/block/entity/PlinthOfPeekingBlockEntity.java b/src/main/java/io/github/lunathelemon/territorial/block/entity/PlinthOfPeekingBlockEntity.java index 1b983de..40ef4b0 100644 --- a/src/main/java/io/github/lunathelemon/territorial/block/entity/PlinthOfPeekingBlockEntity.java +++ b/src/main/java/io/github/lunathelemon/territorial/block/entity/PlinthOfPeekingBlockEntity.java @@ -7,7 +7,9 @@ import io.github.lunathelemon.territorial.block.TerritorialBlocks; import io.github.lunathelemon.territorial.component.TerritorialComponents; import io.github.lunathelemon.territorial.util.BeaconUtils; +import net.minecraft.block.Block; import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; import net.minecraft.block.entity.BlockEntity; import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; @@ -20,6 +22,7 @@ import net.minecraft.registry.Registries; import net.minecraft.state.property.Properties; import net.minecraft.util.Identifier; +import net.minecraft.util.Pair; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import org.jetbrains.annotations.Nullable; @@ -29,7 +32,10 @@ public class PlinthOfPeekingBlockEntity extends BlockEntity implements BoundBlockEntity { private static final int[] reachMultipliers = { 1, 3, 8, 16, 27 }; - private static final Set acceptedPodiumItems = Set.of(Items.ENDER_EYE); + // TODO - Move this to a tag at some point + private static final List acceptedBeaconBaseBlocks = List.of( + TerritorialBlocks.OMNISCIENT_OBSIDIAN, Blocks.OBSIDIAN, Blocks.CRYING_OBSIDIAN + ); private int level; private Item podiumItem; @@ -43,7 +49,9 @@ public PlinthOfPeekingBlockEntity(BlockPos pos, BlockState state) { public static void tick(World world, BlockPos pos, BlockState state, PlinthOfPeekingBlockEntity be) { if(world.getTime() % 80 == 0) { - int newLevel = BeaconUtils.updateLevel(world, pos, List.of(TerritorialBlocks.OMNISCIENT_OBSIDIAN)); + int newLevel = BeaconUtils.updateLevel( + world, pos, acceptedBeaconBaseBlocks, + new Pair<>(TerritorialBlocks.OMNISCIENT_OBSIDIAN, 0.5f)); // Level has changed if(newLevel != be.level) { diff --git a/src/main/java/io/github/lunathelemon/territorial/block/entity/TerritorialBlockEntities.java b/src/main/java/io/github/lunathelemon/territorial/block/entity/TerritorialBlockEntities.java index bb0e6e4..b577a7e 100644 --- a/src/main/java/io/github/lunathelemon/territorial/block/entity/TerritorialBlockEntities.java +++ b/src/main/java/io/github/lunathelemon/territorial/block/entity/TerritorialBlockEntities.java @@ -28,6 +28,6 @@ public static void registerAll() { } private static void register(String id, BlockEntityType beType) { - Registry.register(Registries.BLOCK_ENTITY_TYPE, new Identifier(Territorial.MOD_ID, id), beType); + Registry.register(Registries.BLOCK_ENTITY_TYPE, Territorial.getID(id), beType); } } diff --git a/src/main/java/io/github/lunathelemon/territorial/client/render/entity/PeekingEyeRenderer.java b/src/main/java/io/github/lunathelemon/territorial/client/render/entity/PeekingEyeRenderer.java index 2af1f97..c589664 100644 --- a/src/main/java/io/github/lunathelemon/territorial/client/render/entity/PeekingEyeRenderer.java +++ b/src/main/java/io/github/lunathelemon/territorial/client/render/entity/PeekingEyeRenderer.java @@ -50,7 +50,7 @@ private static void renderShaderOverlay(WorldRenderContext context) { if(peekingCapability.isPeeking()) { if(!RenderUtils.Shader.isLoaded()) { try { - RenderUtils.Shader.load(new Identifier(Territorial.MOD_ID, "shaders/post/" + "peeking_eye" + ".json")); + RenderUtils.Shader.load(Territorial.getID("shaders/post/" + "peeking_eye" + ".json")); } catch(IOException ignored) {} } RenderUtils.Shader.render(context.tickDelta()); diff --git a/src/main/java/io/github/lunathelemon/territorial/component/TerritorialComponents.java b/src/main/java/io/github/lunathelemon/territorial/component/TerritorialComponents.java index 6cb29a8..dd3af9c 100644 --- a/src/main/java/io/github/lunathelemon/territorial/component/TerritorialComponents.java +++ b/src/main/java/io/github/lunathelemon/territorial/component/TerritorialComponents.java @@ -12,7 +12,7 @@ public class TerritorialComponents implements EntityComponentInitializer { public static final ComponentKey PEEKING_EYE - = ComponentRegistry.getOrCreate(new Identifier(Territorial.MOD_ID, "peeking_eye"), IPeekingEyeComponent.class); + = ComponentRegistry.getOrCreate(Territorial.getID("peeking_eye"), IPeekingEyeComponent.class); @Override public void registerEntityComponentFactories(EntityComponentFactoryRegistry registry) { diff --git a/src/main/java/io/github/lunathelemon/territorial/init/ItemGroupRegistry.java b/src/main/java/io/github/lunathelemon/territorial/init/ItemGroupRegistry.java index 0c61642..912114f 100644 --- a/src/main/java/io/github/lunathelemon/territorial/init/ItemGroupRegistry.java +++ b/src/main/java/io/github/lunathelemon/territorial/init/ItemGroupRegistry.java @@ -13,7 +13,7 @@ public final class ItemGroupRegistry { - public static final ItemGroup BASE_GROUP = FabricItemGroup.builder(new Identifier(Territorial.MOD_ID, "base_group")) + public static final ItemGroup BASE_GROUP = FabricItemGroup.builder(Territorial.getID("base_group")) .icon(() -> new ItemStack(Blocks.OBSIDIAN)).build(); private static final Queue registrationStackQueue = new ArrayDeque<>(); diff --git a/src/main/java/io/github/lunathelemon/territorial/init/TerritorialDamageTypes.java b/src/main/java/io/github/lunathelemon/territorial/init/TerritorialDamageTypes.java index a276c2f..d63e85f 100644 --- a/src/main/java/io/github/lunathelemon/territorial/init/TerritorialDamageTypes.java +++ b/src/main/java/io/github/lunathelemon/territorial/init/TerritorialDamageTypes.java @@ -10,8 +10,6 @@ import net.minecraft.world.World; public final class TerritorialDamageTypes { - // TODO - 1.19.4 code - public static final RegistryKey OMNISCIENT_OBSIDIAN = RegistryKey.of(RegistryKeys.DAMAGE_TYPE, new Identifier(Territorial.MOD_ID, "omniscient_obsidian")); public static final RegistryKey LASER = RegistryKey.of(RegistryKeys.DAMAGE_TYPE, new Identifier(Territorial.MOD_ID, "laser")); diff --git a/src/main/java/io/github/lunathelemon/territorial/init/TerritorialTags.java b/src/main/java/io/github/lunathelemon/territorial/init/TerritorialTags.java new file mode 100644 index 0000000..cd4d6b0 --- /dev/null +++ b/src/main/java/io/github/lunathelemon/territorial/init/TerritorialTags.java @@ -0,0 +1,15 @@ +package io.github.lunathelemon.territorial.init; + +import io.github.lunathelemon.territorial.Territorial; +import net.minecraft.block.Block; +import net.minecraft.registry.RegistryKeys; +import net.minecraft.registry.tag.TagKey; + +public class TerritorialTags { + + public static class Blocks { + public static final TagKey CORRUPTED_PYRAMID = TagKey.of(RegistryKeys.BLOCK, Territorial.getID("corrupted_pyramid")); + } + + public static class Items {} +} diff --git a/src/main/java/io/github/lunathelemon/territorial/item/LensItem.java b/src/main/java/io/github/lunathelemon/territorial/item/LensItem.java index c2a4577..ae18ad7 100644 --- a/src/main/java/io/github/lunathelemon/territorial/item/LensItem.java +++ b/src/main/java/io/github/lunathelemon/territorial/item/LensItem.java @@ -35,10 +35,11 @@ public Text getName(ItemStack stack) { if (tag != null) { var dyeColour = Optional.of(DyeColor.byId(tag.getInt("colour"))).orElse(DyeColor.WHITE); + // TODO - Fix this for other languages String dyeNameCapitalized = Stream.of(dyeColour.getName().split("_")) - .map(part -> part.replace("_", " ")) + .map(part -> part.replace("_", "")) .map(part -> part.substring(0, 1).toUpperCase() + part.substring(1)) - .collect(Collectors.joining()); + .collect(Collectors.joining(" ")); return Text.of(dyeNameCapitalized + " " + translationText.getString()); } return translationText; diff --git a/src/main/java/io/github/lunathelemon/territorial/mixin/common/BeaconBlockEntityMixin.java b/src/main/java/io/github/lunathelemon/territorial/mixin/common/BeaconBlockEntityMixin.java index 2eb3b87..21a90a5 100644 --- a/src/main/java/io/github/lunathelemon/territorial/mixin/common/BeaconBlockEntityMixin.java +++ b/src/main/java/io/github/lunathelemon/territorial/mixin/common/BeaconBlockEntityMixin.java @@ -25,7 +25,7 @@ public abstract class BeaconBlockEntityMixin { private static void tick(World world, BlockPos pos, BlockState state, BeaconBlockEntity blockEntity, CallbackInfo ci, int x, int y, int z, BlockPos posLocal, BeaconBlockEntity.BeamSegment segment, int yTopPos, final int levelLocal) { if (world.getTime() % 80L == 0L && levelLocal == 0) { - int finalLevel = BeaconUtils.updateLevel(world, pos, List.of(TerritorialBlocks.OMNISCIENT_OBSIDIAN)); + int finalLevel = BeaconUtils.updateCorruptedBeaconLevel(world, pos); if(finalLevel > 0) { CorruptionEvents.BLOCK_CORRUPTED.invoker().onCorruptedBlock(world, pos, state, blockEntity); } diff --git a/src/main/java/io/github/lunathelemon/territorial/util/BeaconUtils.java b/src/main/java/io/github/lunathelemon/territorial/util/BeaconUtils.java index 095562e..dfe4442 100644 --- a/src/main/java/io/github/lunathelemon/territorial/util/BeaconUtils.java +++ b/src/main/java/io/github/lunathelemon/territorial/util/BeaconUtils.java @@ -1,17 +1,36 @@ package io.github.lunathelemon.territorial.util; +import io.github.lunathelemon.territorial.block.TerritorialBlocks; +import it.unimi.dsi.fastutil.objects.Object2IntArrayMap; import net.minecraft.block.Block; +import net.minecraft.block.Blocks; +import net.minecraft.util.Pair; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +import org.apache.commons.compress.compressors.lz77support.LZ77Compressor; +import org.jetbrains.annotations.Nullable; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; public class BeaconUtils { + private static final List corruptedBeaconBaseBlocks; + private static final Pair corruptedBeaconMainBlockProportion; - public static int updateLevel(final World world, final BlockPos pos, final List targetBlocks) { - int level = 0; + private static final int[] beaconSizes = { 9, 34, 83, 164 }; + + public static int updateCorruptedBeaconLevel(final World world, final BlockPos pos) { + return updateLevel(world, pos, corruptedBeaconBaseBlocks, corruptedBeaconMainBlockProportion); + } + + public static int updateLevel(final World world, final BlockPos pos, final List beaconBaseBlocks, final Pair mainBlockProportion) { final int bottomY = world.getBottomY(); - int y, xPos, zPos; + final Block mainBlock = mainBlockProportion.getLeft(); + + int level = 0, mainBlockCount = 0, y, xPos, zPos; + Block currentBlock; // Decrement the y value to check the blocks below for(int yInc = 1; yInc <= 4; level = yInc++) { @@ -25,14 +44,30 @@ public static int updateLevel(final World world, final BlockPos pos, final List< // Check each slice of blocks below in the same way a beacon would for(int x = xPos - yInc; x <= xPos + yInc; ++x) { for(int z = zPos - yInc; z <= zPos + yInc; ++z) { + currentBlock = world.getBlockState(new BlockPos(x, y, z)).getBlock(); // Return the current level if a new block cannot be found - if(!targetBlocks.contains(world.getBlockState(new BlockPos(x, y, z)).getBlock())) { - return level; - } + if(!beaconBaseBlocks.contains(currentBlock)) + return checkProportionality(mainBlockProportion, mainBlockCount, level); + else if(currentBlock.equals(mainBlock)) + mainBlockCount++; } } } } - return level; + return checkProportionality(mainBlockProportion, mainBlockCount, level); + } + + private static int checkProportionality(final Pair mainBlockProportion, int mainBlockCount, int level) { + float proportion = mainBlockProportion.getRight(); + + if(proportion > 0 && level > 0) + return (float) mainBlockCount / beaconSizes[level-1] >= proportion ? level : 0; + else + return level; + } + + static { + corruptedBeaconBaseBlocks = List.of(TerritorialBlocks.OMNISCIENT_OBSIDIAN, Blocks.OBSIDIAN, Blocks.CRYING_OBSIDIAN); + corruptedBeaconMainBlockProportion = new Pair<>(TerritorialBlocks.OMNISCIENT_OBSIDIAN, 0.5f); } } diff --git a/src/main/resources/data/territorial/tags/blocks/corrupted_pyramid.json b/src/main/resources/data/territorial/tags/blocks/corrupted_pyramid.json new file mode 100644 index 0000000..596d1ad --- /dev/null +++ b/src/main/resources/data/territorial/tags/blocks/corrupted_pyramid.json @@ -0,0 +1,8 @@ +{ + "replace": false, + "values": [ + "minecraft:obsidian", + "minecraft:crying_obsidian", + "territorial:omniscient_obsidian" + ] +} \ No newline at end of file