Skip to content

Commit

Permalink
Try to improve things lol
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-evelyn committed Sep 4, 2023
1 parent 24534e4 commit 3a83a9f
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<Item> acceptedPodiumItems = Set.of(Items.ENDER_EYE);
// TODO - Move this to a tag at some point
private static final List<Block> acceptedBeaconBaseBlocks = List.of(
TerritorialBlocks.OMNISCIENT_OBSIDIAN, Blocks.OBSIDIAN, Blocks.CRYING_OBSIDIAN
);

private int level;
private Item podiumItem;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public static void registerAll() {
}

private static <T extends BlockEntity> void register(String id, BlockEntityType<T> beType) {
Registry.register(Registries.BLOCK_ENTITY_TYPE, new Identifier(Territorial.MOD_ID, id), beType);
Registry.register(Registries.BLOCK_ENTITY_TYPE, Territorial.getID(id), beType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class TerritorialComponents implements EntityComponentInitializer {

public static final ComponentKey<IPeekingEyeComponent> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ItemStack> registrationStackQueue = new ArrayDeque<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import net.minecraft.world.World;

public final class TerritorialDamageTypes {
// TODO - 1.19.4 code

public static final RegistryKey<DamageType> OMNISCIENT_OBSIDIAN = RegistryKey.of(RegistryKeys.DAMAGE_TYPE, new Identifier(Territorial.MOD_ID, "omniscient_obsidian"));
public static final RegistryKey<DamageType> LASER = RegistryKey.of(RegistryKeys.DAMAGE_TYPE, new Identifier(Territorial.MOD_ID, "laser"));

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Block> CORRUPTED_PYRAMID = TagKey.of(RegistryKeys.BLOCK, Territorial.getID("corrupted_pyramid"));
}

public static class Items {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Block> corruptedBeaconBaseBlocks;
private static final Pair<Block, Float> corruptedBeaconMainBlockProportion;

public static int updateLevel(final World world, final BlockPos pos, final List<Block> 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<Block> beaconBaseBlocks, final Pair<Block, Float> 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++) {
Expand All @@ -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<Block, Float> 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"replace": false,
"values": [
"minecraft:obsidian",
"minecraft:crying_obsidian",
"territorial:omniscient_obsidian"
]
}

0 comments on commit 3a83a9f

Please sign in to comment.