Skip to content

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Powercyphe committed Mar 14, 2024
1 parent d70d7e4 commit 173b89a
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 110 deletions.
25 changes: 14 additions & 11 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ base {
}

repositories {
// Add repositories to retrieve artifacts from in here.
// You should only use this when depending on other mods because
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
// for more information about repositories.
maven {
name = "TerraformersMC"
url = "https://maven.terraformersmc.com/"
}
maven {
name = "Ladysnake Libs"
url = 'https://maven.ladysnake.org/releases'
}
}

loom {
Expand Down Expand Up @@ -44,18 +47,18 @@ sourceSets {
}
}
dependencies {
// To change the versions see the gradle.properties file
// Versions can be changed in the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

// Fabric API. This is technically optional, but you probably want it anyway.
// Fabric API
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

// Uncomment the following line to enable the deprecated Fabric API modules.
// These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time.

// modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}"
// Trinkets API
modImplementation "dev.emi:trinkets:${project.trinkets_version}"
modImplementation include("dev.onyxstudios.cardinal-components-api:cardinal-components-base:${project.cca_version}")
modImplementation include("dev.onyxstudios.cardinal-components-api:cardinal-components-entity:${project.cca_version}")
}

processResources {
Expand Down
6 changes: 4 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ yarn_mappings=1.19.2+build.28
loader_version=0.14.24

# Mod Properties
mod_version=1.1.0+1.19.2
mod_version=1.2.0+1.19.2
maven_group=powercyphe.coffins
archives_base_name=coffins

# Dependencies
fabric_version=0.76.1+1.19.2
fabric_version=0.76.1+1.19.2
trinkets_version=3.4.1
cca_version=5.0.2
6 changes: 5 additions & 1 deletion src/main/java/powercyphe/coffins/Mod.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.fabric.api.resource.ResourcePackActivationType;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.resource.ResourceType;
import net.minecraft.util.Identifier;
import net.minecraft.world.GameRules;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import powercyphe.coffins.block.ModBlocks;
import powercyphe.coffins.block.entity.ModBlockEntities;
import powercyphe.coffins.event.PlayerRespawnEvent;
import powercyphe.coffins.event.TrinketDropEvent;
import powercyphe.coffins.item.ModItems;
import powercyphe.coffins.screen.ModScreenHandlers;
import powercyphe.coffins.sound.ModSounds;
Expand All @@ -26,6 +26,8 @@ public class Mod implements ModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static final GameRules.Key<GameRules.BooleanRule> KEEP_RECOVERY_COMPASS = GameRuleRegistry.register("keepRecoveryCompass", GameRules.Category.PLAYER, GameRuleFactory.createBooleanRule(true));

public static final boolean isTrinketsLoaded = FabricLoader.getInstance().isModLoaded("trinkets");

@Override
public void onInitialize() {
ModItems.registerModItems();
Expand All @@ -35,6 +37,8 @@ public void onInitialize() {
ModSounds.registerModSounds();
ModLootTableModifier.modifyLootTables();

if (isTrinketsLoaded) TrinketDropEvent.registerCallback();

ServerPlayerEvents.AFTER_RESPAWN.register(new PlayerRespawnEvent());

FabricLoader.getInstance().getModContainer(Mod.MOD_ID).ifPresent(modContainer -> ResourceManagerHelper.registerBuiltinResourcePack(Mod.id("coffins-dark-menu"), modContainer, ResourcePackActivationType.NORMAL));
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/powercyphe/coffins/event/TrinketDropEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package powercyphe.coffins.event;


import dev.emi.trinkets.api.TrinketEnums;
import dev.emi.trinkets.api.event.TrinketDropCallback;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.world.GameRules;

public class TrinketDropEvent {

public static void registerCallback() {
TrinketDropCallback.EVENT.register((rule, stack, ref, entity) -> {

// If the trinkets were dropped by a player and keep inventory is off, destroy the trinket
if (!(entity instanceof PlayerEntity player)) return rule;
if (player.getWorld().getGameRules().getBoolean(GameRules.KEEP_INVENTORY)) return rule;

return TrinketEnums.DropRule.DESTROY;
});
}
}
191 changes: 99 additions & 92 deletions src/main/java/powercyphe/coffins/mixin/PlayerEntityMixin.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
package powercyphe.coffins.mixin;

import dev.emi.trinkets.api.TrinketsApi;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.ItemScatterer;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.GameRules;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import powercyphe.coffins.Mod;
import powercyphe.coffins.block.ModBlocks;
import powercyphe.coffins.block.entity.CoffinBlockEntity;
import powercyphe.coffins.event.PlayerEventHandler;
import powercyphe.coffins.util.IEntityDataSaver;
import powercyphe.coffins.util.ModTags;
import powercyphe.coffins.util.RecoveryCompassData;
Expand All @@ -40,111 +38,120 @@ public abstract class PlayerEntityMixin implements IEntityDataSaver {

@Shadow protected abstract void vanishCursedItems();

@Shadow @Final private PlayerInventory inventory;

@Inject(method = "dropInventory", at = @At("HEAD"))
private void dropInventory(CallbackInfo ci) {
PlayerEntity player = ((PlayerEntity) (Object) this);
if (!player.getWorld().isClient) {
RecoveryCompassData.setNbt((IEntityDataSaver) player, null);
RecoveryCompassData.setCount((IEntityDataSaver) player, 0);
if (!player.getWorld().getGameRules().getBoolean(GameRules.KEEP_INVENTORY)) {
this.vanishCursedItems();

ArrayList<ItemStack> inventory = new ArrayList<>();
for (int i = 0; i < this.getInventory().size(); i++) {
inventory.add(this.getInventory().getStack(i));
}
if (player.getWorld().isClient) return;
RecoveryCompassData.setNbt((IEntityDataSaver) player, null);
RecoveryCompassData.setCount((IEntityDataSaver) player, 0);
if (player.getWorld().getGameRules().getBoolean(GameRules.KEEP_INVENTORY)) return;
this.vanishCursedItems();

ArrayList<ItemStack> inventory = new ArrayList<>();
for (int i = 0; i < this.getInventory().size(); i++) {
inventory.add(this.getInventory().getStack(i));
}

int itemsAmount = 0;
DefaultedList<ItemStack> items = DefaultedList.ofSize(54, ItemStack.EMPTY);
boolean hasRecoveryCompass = false;
for (int i = 0; i < inventory.size(); i++) {
ItemStack stack = inventory.get(i);
if (!stack.isEmpty() && !stack.isOf(Items.AIR) && stack != Items.AIR.getDefaultStack() && itemsAmount < items.size()) {
boolean keepRecoveryCompass = false;
if (player.getWorld().getGameRules().getBoolean(Mod.KEEP_RECOVERY_COMPASS)) {
if (ItemStack.areItemsEqual(stack, Items.RECOVERY_COMPASS.getDefaultStack())) {
if (!hasRecoveryCompass) {
keepRecoveryCompass = true;
}
}
}
// If Trinkets is loaded & the player has a trinket component,
// we repeat through all the player's equipped trinkets and add them to the
// inventory (coffin).

if (Mod.isTrinketsLoaded && TrinketsApi.getTrinketComponent(player).isPresent()) {
TrinketsApi.getTrinketComponent(player).ifPresent(trinkets -> trinkets.forEach((ref, stack) -> {
if (!stack.isEmpty()) {
inventory.add(stack);
}
}));
}

if (!keepRecoveryCompass) {
this.getInventory().removeOne(stack);
itemsAmount = itemsAmount + 1;
items.set(i, stack);
} else {
this.getInventory().removeOne(stack);
RecoveryCompassData.setNbt((IEntityDataSaver) player, stack);
RecoveryCompassData.setCount((IEntityDataSaver) player, stack.getCount());
hasRecoveryCompass = true;
int itemsAmount = 0;
DefaultedList<ItemStack> items = DefaultedList.ofSize(54, ItemStack.EMPTY);
boolean hasRecoveryCompass = false;
for (int i = 0; i < inventory.size(); i++) {
ItemStack stack = inventory.get(i);
if (!stack.isEmpty() && !stack.isOf(Items.AIR) && stack != Items.AIR.getDefaultStack() && itemsAmount < items.size()) {
boolean keepRecoveryCompass = false;
if (player.getWorld().getGameRules().getBoolean(Mod.KEEP_RECOVERY_COMPASS)) {
if (ItemStack.areItemsEqual(stack, Items.RECOVERY_COMPASS.getDefaultStack())) {
if (!hasRecoveryCompass) {
keepRecoveryCompass = true;
}
}
}

if (!items.isEmpty() && itemsAmount > 0) {
TreeMap<BlockPos, Integer> distanceMap = new TreeMap<>();
World world = player.getWorld();
this.getInventory().removeOne(stack);
if (!keepRecoveryCompass) {
itemsAmount = itemsAmount + 1;
items.set(i, stack);
} else {
RecoveryCompassData.setNbt((IEntityDataSaver) player, stack);
RecoveryCompassData.setCount((IEntityDataSaver) player, stack.getCount());
hasRecoveryCompass = true;
}
}
}

BlockPos playerPos = player.getBlockPos();
boolean shift = true;
while (shift) {
BlockPos shiftPos = playerPos.down(1);
if (
player.getWorld().getBlockState(shiftPos).isIn(ModTags.Blocks.COFFIN_REPLACEABLE)
) {
playerPos = shiftPos;
} else {
shift = false;
}
if (!items.isEmpty() && itemsAmount > 0) {
TreeMap<BlockPos, Integer> distanceMap = new TreeMap<>();
World world = player.getWorld();

BlockPos playerPos = player.getBlockPos();
boolean shift = true;
while (shift) {
BlockPos shiftPos = playerPos.down(1);
if (player.getWorld().getBlockState(shiftPos).isIn(ModTags.Blocks.COFFIN_REPLACEABLE)) {
playerPos = shiftPos;
} else {
shift = false;
}

}
}

int xPlayer = playerPos.getX();
int yPlayer = playerPos.getY();
int zPlayer = playerPos.getZ();

int xPlayer = playerPos.getX();
int yPlayer = playerPos.getY();
int zPlayer = playerPos.getZ();

int xMin = playerPos.getX() - 16;
int xMax = playerPos.getX() + 16;
int zMin = playerPos.getZ() - 16;
int zMax = playerPos.getZ() + 16;

for (int x = xMin; x < xMax; x++) {
for (int z = zMin; z < zMax; z++) {
for (int y = -64; y < 255; y++) {
BlockPos blockPos = new BlockPos(x, y, z);
int distance = 0;
if (
player.getWorld().getBlockState(blockPos).isIn(ModTags.Blocks.COFFIN_REPLACEABLE) &&
player.getWorld().getWorldBorder().contains(x, z)
) {
distance = distance + Math.abs(x - xPlayer) + Math.abs(y - yPlayer) + Math.abs(z - zPlayer);
distanceMap.put(blockPos, distance);
}
}
int xMin = playerPos.getX() - 16;
int xMax = playerPos.getX() + 16;
int zMin = playerPos.getZ() - 16;
int zMax = playerPos.getZ() + 16;

for (int x = xMin; x < xMax; x++) {
for (int z = zMin; z < zMax; z++) {
for (int y = -64; y < 255; y++) {
BlockPos blockPos = new BlockPos(x, y, z);
int distance = 0;
if (
player.getWorld().getBlockState(blockPos).isIn(ModTags.Blocks.COFFIN_REPLACEABLE) &&
player.getWorld().getWorldBorder().contains(x, z)
) {
distance = distance + Math.abs(x - xPlayer) + Math.abs(y - yPlayer) + Math.abs(z - zPlayer);
distanceMap.put(blockPos, distance);
}
}
List<Map.Entry<BlockPos, Integer>> entryList = new ArrayList<>(distanceMap.entrySet());
entryList.sort(Map.Entry.comparingByValue());
int sizeCheck = entryList.size();
if (sizeCheck > 0) {
BlockPos blockPos = entryList.get(1).getKey();
BlockState blockState = ModBlocks.COFFIN.getDefaultState();
}
}
List<Map.Entry<BlockPos, Integer>> entryList = new ArrayList<>(distanceMap.entrySet());
entryList.sort(Map.Entry.comparingByValue());
int sizeCheck = entryList.size();
if (sizeCheck > 0) {
BlockPos blockPos = entryList.get(1).getKey();
BlockState blockState = ModBlocks.COFFIN.getDefaultState();

// player.sendMessage(Text.literal("Your Items were dropped at " + "[" + blockPos.getX() + " " + blockPos.getY() + " " + blockPos.getZ() + "]"));
world.setBlockState(blockPos, blockState);
BlockEntity blockEntity = world.getBlockEntity(blockPos);
if (blockEntity instanceof CoffinBlockEntity coffinBlockEntity) {
coffinBlockEntity.setItems(items);
coffinBlockEntity.setOwner(player.getEntityName());
coffinBlockEntity.setFragile(true);
world.updateListeners(blockPos, blockState, blockState, 3);
}
} else {
// player.sendMessage(Text.literal("Your Items were dropped at " + "[" + player.getBlockPos().getX() + " " + player.getBlockPos().getY() + " " + player.getBlockPos().getZ() + "]"));
ItemScatterer.spawn(world, player.getBlockPos(), items);
}
world.setBlockState(blockPos, blockState);
BlockEntity blockEntity = world.getBlockEntity(blockPos);
if (blockEntity instanceof CoffinBlockEntity coffinBlockEntity) {
coffinBlockEntity.setItems(items);
coffinBlockEntity.setOwner(player.getEntityName());
coffinBlockEntity.setFragile(true);
world.updateListeners(blockPos, blockState, blockState, 3);
}
} else {
// player.sendMessage(Text.literal("Your Items were dropped at " + "[" + player.getBlockPos().getX() + " " + player.getBlockPos().getY() + " " + player.getBlockPos().getZ() + "]"));
ItemScatterer.spawn(world, player.getBlockPos(), items);
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@
],
"depends": {
"fabricloader": ">=0.14.24",
"minecraft": "~1.19.2",
"minecraft": "1.19.2",
"java": ">=17",
"fabric-api": "*"
},
"suggests": {
"another-mod": "*"
}
}

0 comments on commit 173b89a

Please sign in to comment.