Skip to content

Commit

Permalink
Version 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mt1006 committed May 25, 2024
1 parent 3b2fce1 commit e9908b1
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 31 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
===Version 1.1===
-Blocks openable with key are now defined using "#irondoorkey:openable" tag, therefore they can now be added by other mods or datapacks (thanks to Anonyku05 for suggestion).
-Minor improvements.
14 changes: 12 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@ base
archivesName = "${project.archives_base_name}-FABRIC-${project.minecraft_version}"
}

repositories
{
maven { url = 'https://maven.parchmentmc.org/' }
}

dependencies
{
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings loom.officialMojangMappings()
mappings loom.layered()
{
officialMojangMappings()
parchment("org.parchmentmc.data:parchment-${project.minecraft_version}:${project.mappings_version}@zip")
}

modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
}
Expand All @@ -38,5 +48,5 @@ java

jar
{
from("LICENSE") { rename { "${it}_${project.archivesBaseName}"} }
from "LICENSE"
}
13 changes: 7 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ org.gradle.jvmargs=-Xmx1G
org.gradle.parallel=true

# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.20.5
yarn_mappings=1.20.5+build.1
loader_version=0.15.10
# https://fabricmc.net/develop
# https://parchmentmc.org/docs/getting-started
minecraft_version=1.20.6
loader_version=0.15.11
mappings_version=2024.05.01

# Mod Properties
mod_version=1.0
mod_version=1.1
maven_group=com.mt1006.irondoorkey
archives_base_name=IronDoorKey

# Dependencies
fabric_version=0.97.6+1.20.5
fabric_version=0.99.0+1.20.6
6 changes: 1 addition & 5 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ pluginManagement
{
repositories
{
maven
{
name = 'Fabric'
url = 'https://maven.fabricmc.net/'
}
mavenCentral()
gradlePluginPortal()
maven { url = 'https://maven.fabricmc.net/' }
}
}
52 changes: 39 additions & 13 deletions src/main/java/com/mt1006/irondoorkey/IronDoorKeyItem.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.mt1006.irondoorkey;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.DoorBlock;
import net.minecraft.world.level.block.FenceGateBlock;
import net.minecraft.world.level.block.TrapDoorBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockSetType;
Expand All @@ -30,25 +31,30 @@ public IronDoorKeyItem()
Level level = ctx.getLevel();
BlockPos blockPos = ctx.getClickedPos();
BlockState blockState = level.getBlockState(blockPos);

if (!blockState.is(IronDoorKeyMod.OPENABLE)) { return InteractionResult.PASS; }
Block blockType = blockState.getBlock();

if (blockType instanceof DoorBlock)
{
if (blockType == Blocks.IRON_DOOR)
{
DoorBlock doorBlock = (DoorBlock)blockType;
doorBlock.setOpen(ctx.getPlayer(), level, blockState, blockPos, !doorBlock.isOpen(blockState));
return InteractionResult.sidedSuccess(level.isClientSide);
}
DoorBlock doorBlock = (DoorBlock)blockType;
doorBlock.setOpen(ctx.getPlayer(), level, blockState, blockPos, !doorBlock.isOpen(blockState));
return InteractionResult.sidedSuccess(level.isClientSide);
}
else if (blockType instanceof TrapDoorBlock)
{
if (blockType == Blocks.IRON_TRAPDOOR)
{
openTrapDoor(ctx.getPlayer(), level, blockPos, blockState);
return InteractionResult.sidedSuccess(level.isClientSide);
}
openTrapDoor(ctx.getPlayer(), level, blockPos, blockState);
return InteractionResult.sidedSuccess(level.isClientSide);
}
else if (blockType instanceof FenceGateBlock)
{
// redundant in vanilla, added for better mod support, e.g. with SecurityCraft
openFenceGate(ctx.getPlayer(), level, blockPos, blockState);
return InteractionResult.sidedSuccess(level.isClientSide);
}

IronDoorKeyMod.LOGGER.warn("Failed to open the block - " +
"it has \"openable\" tag, but isn't instance of DoorBlock or TrapDoorBlock");
return InteractionResult.PASS;
}

Expand All @@ -64,7 +70,27 @@ private static void openTrapDoor(@Nullable Player player, Level level, BlockPos

boolean isOpen = newBlockState.getValue(TrapDoorBlock.OPEN);
level.playSound(player, blockPos, isOpen ? BlockSetType.IRON.trapdoorOpen() : BlockSetType.IRON.trapdoorClose(),
SoundSource.BLOCKS, 1.0F, level.getRandom().nextFloat() * 0.1F + 0.9F);
SoundSource.BLOCKS, 1.0f, level.getRandom().nextFloat() * 0.1f + 0.9f);
level.gameEvent(player, isOpen ? GameEvent.BLOCK_OPEN : GameEvent.BLOCK_CLOSE, blockPos);
}

private static void openFenceGate(@Nullable Player player, Level level, BlockPos blockPos, BlockState blockState)
{
boolean wasOpen = blockState.getValue(FenceGateBlock.OPEN);
if (wasOpen)
{
BlockState newBlockState = blockState.setValue(FenceGateBlock.OPEN, false);
level.setBlock(blockPos, newBlockState, 10);
}
else
{
Direction direction = player != null ? player.getDirection() : blockState.getValue(FenceGateBlock.FACING);
BlockState newBlockState = blockState.setValue(FenceGateBlock.OPEN, true).setValue(FenceGateBlock.FACING, direction);
level.setBlock(blockPos, newBlockState, 10);
}

boolean isOpen = !wasOpen;
level.levelEvent(player, isOpen ? 1008 : 1014, blockPos, 0);
level.gameEvent(player, isOpen ? GameEvent.BLOCK_OPEN : GameEvent.BLOCK_CLOSE, blockPos);
}
}
10 changes: 7 additions & 3 deletions src/main/java/com/mt1006/irondoorkey/IronDoorKeyMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import org.slf4j.Logger;

public class IronDoorKeyMod implements ModInitializer
{
public static final String MOD_ID = "irondoorkey";
public static final String VERSION = "1.0";
public static final String FOR_VERSION = "1.20.5";
public static final String VERSION = "1.1";
public static final String FOR_VERSION = "1.20.6";
public static final String FOR_LOADER = "Fabric";
public static final Logger LOGGER = LogUtils.getLogger();

public static final Item ITEM_IRON_DOOR_KEY = new IronDoorKeyItem();
public static final TagKey<Block> OPENABLE = TagKey.create(Registries.BLOCK, new ResourceLocation(MOD_ID, "openable"));

@Override public void onInitialize()
{
LOGGER.info("{} - Author: mt1006 (mt1006x)", getFullName());
LOGGER.info("{} - Author: mt1006", getFullName());
Registry.register(BuiltInRegistries.ITEM, new ResourceLocation(MOD_ID, "iron_door_key"), ITEM_IRON_DOOR_KEY);

ItemGroupEvents.modifyEntriesEvent(CreativeModeTabs.TOOLS_AND_UTILITIES)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions src/main/resources/data/irondoorkey/tags/blocks/openable.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"replace": false,
"values": [
"minecraft:iron_door",
"minecraft:iron_trapdoor"
]
}
5 changes: 3 additions & 2 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

"name": "Iron Door Key",
"description": "Adds iron door key",
"authors": ["mt1006 (mt1006x)"],
"authors": ["mt1006"],
"contact":
{
"homepage": "https://modrinth.com/mod/iron-door-key",
"sources": "https://github.com/mt1006/mc-irondoorkey-mod",
"issues": "https://github.com/mt1006/mc-irondoorkey-mod/issues"
},

"license": "GNU Lesser General Public License v3.0",
"license": "LGPL-3.0-only",
"icon": "logo.png",

"environment": "*",
Expand Down

0 comments on commit e9908b1

Please sign in to comment.