Skip to content

Commit

Permalink
Block Rotation
Browse files Browse the repository at this point in the history
Mixins for chunk loading
Updated steel armor textures
Signed-off-by: SuperScary
  • Loading branch information
SuperScary committed Oct 24, 2024
1 parent 386e350 commit 1566ceb
Show file tree
Hide file tree
Showing 21 changed files with 147 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.superscary.fluxmachines.blockentity.base;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
Expand All @@ -17,6 +18,7 @@
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.phys.BlockHitResult;
import net.neoforged.neoforge.items.ItemStackHandler;
import net.superscary.fluxmachines.api.data.BlockData;
Expand Down Expand Up @@ -137,7 +139,7 @@ public void drops () {
* @param wrench the {@link ItemStack} used. Already checked to contain {@link net.superscary.fluxmachines.util.tags.FMTag.Items#WRENCH}
* @return {@link InteractionResult}
*/
public InteractionResult disassembleWithWrench (Player player, Level level, BlockHitResult hitResult, ItemStack wrench) {
public InteractionResult disassemble (Player player, Level level, BlockHitResult hitResult, ItemStack wrench) {
var pos = hitResult.getBlockPos();
var state = level.getBlockState(pos);
var block = state.getBlock();
Expand All @@ -156,6 +158,20 @@ public InteractionResult disassembleWithWrench (Player player, Level level, Bloc
return InteractionResult.sidedSuccess(level.isClientSide());
}

// TODO: Y-rot not implemented. Facing down throws a IllegalStateException.
public InteractionResult rotateOnAxis (Level level, BlockHitResult hitResult, BlockState state, Direction direction) {
if (!level.isClientSide()) {
var currentDirection = state.getValue(BlockStateProperties.FACING);
if (direction != Direction.UP && direction != Direction.DOWN) {
level.setBlockAndUpdate(hitResult.getBlockPos(), state.setValue(BlockStateProperties.FACING, currentDirection.getClockWise()));
} else {
level.setBlockAndUpdate(hitResult.getBlockPos(), state.setValue(BlockStateProperties.FACING, currentDirection.getClockWise(Direction.Axis.X)));
}
}

return InteractionResult.sidedSuccess(level.isClientSide());
}

@Override
public ItemStackHandler getInventory () {
return inventory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public void saveClientData (CompoundTag tag, HolderLookup.Provider registries) {
@Override
public void loadClientData (CompoundTag tag, HolderLookup.Provider registries) {
super.loadClientData(tag, registries);
//IntTag intTag = new IntTag();
energyStorage.deserializeNBT(registries, IntTag.valueOf(tag.getInt(Keys.POWER)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.neoforged.neoforge.registries.RegisterEvent;
import net.neoforged.neoforge.server.ServerLifecycleHooks;
import net.superscary.fluxmachines.hook.HammerHook;
import net.superscary.fluxmachines.hook.RedstoneHook;
import net.superscary.fluxmachines.hook.WrenchHook;
import net.superscary.fluxmachines.impl.top.FMTopPlugin;
import net.superscary.fluxmachines.item.material.FMArmorMaterials;
Expand Down Expand Up @@ -60,6 +61,7 @@ public FluxMachinesBase (IEventBus modEventBus) {
NeoForge.EVENT_BUS.addListener(this::serverStopping);
NeoForge.EVENT_BUS.addListener(WrenchHook::onPlayerUseBlockEvent);
NeoForge.EVENT_BUS.addListener(HammerHook::onPlayerUseBlockEvent);
NeoForge.EVENT_BUS.addListener(RedstoneHook::onPlayerUseBlockEvent);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ protected void items () {
add(STEEL_HAMMER.asItem(), "Steel Hammer");
add(WRENCH.asItem(), "Wrench");

add(REDSTONE_AND_STEEL.asItem(), "Redstone and Steel");

add(HONEY_BUN.asItem(), "Honey Bun");
add(HARD_BOILED_EGG.asItem(), "Hard Boiled Egg");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ protected void registerModels () {
handheldItem(STEEL_HAMMER.asItem());
handheldItem(WRENCH.asItem());

basicItem(REDSTONE_AND_STEEL.asItem());
basicItem(HONEY_BUN.asItem());
basicItem(HARD_BOILED_EGG.asItem());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ protected void misc (RecipeOutput consumer) {
.define('G', Items.GOLD_INGOT)
.unlockedBy("has_steel", has(STEEL_INGOT))
.save(consumer, FluxMachines.getResource("tool/wrench"));

ShapedRecipeBuilder.shaped(RecipeCategory.TOOLS, REDSTONE_AND_STEEL, 1)
.pattern("S ")
.pattern(" R")
.define('S', STEEL_INGOT)
.define('R', Items.REDSTONE)
.unlockedBy("has_steel", has(STEEL_INGOT))
.unlockedBy("has_redstone", has(Items.REDSTONE))
.save(consumer, FluxMachines.getResource("tool/redstone_and_steel"));
}

protected void machine (RecipeOutput consumer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ protected void addTags (HolderLookup.@NotNull Provider provider) {
this.tag(FMTag.Items.HAMMER)
.add(STEEL_HAMMER.asItem());

this.tag(FMTag.Items.STEEL)
this.tag(Tags.Items.INGOTS)
.add(STEEL_INGOT.asItem());

this.tag(FMTag.Items.DUST)
.add(STEEL_DUST.asItem());

this.tag(FMTag.Items.NUGGET)
this.tag(Tags.Items.NUGGETS)
.add(STEEL_NUGGET.asItem());
}

Expand Down
39 changes: 39 additions & 0 deletions src/main/java/net/superscary/fluxmachines/hook/RedstoneHook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package net.superscary.fluxmachines.hook;

import net.minecraft.core.Direction;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.RedStoneWireBlock;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.phys.BlockHitResult;
import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
import net.superscary.fluxmachines.registries.FMItems;

// TODO: Redstone signal not working on right click
public class RedstoneHook {

public static void onPlayerUseBlockEvent (PlayerInteractEvent.RightClickBlock event) {
if (event.isCanceled()) return;
onPlayerUseBlock(event.getEntity(), event.getLevel(), event.getHand(), event.getHitVec());
}

public static InteractionResult onPlayerUseBlock (Player player, Level level, InteractionHand hand, BlockHitResult hitResult) {
if (player.isSpectator() || hand != InteractionHand.MAIN_HAND) return InteractionResult.PASS;
var itemStack = player.getItemInHand(hand);
var blockpos = hitResult.getBlockPos();

if (level.getBlockState(hitResult.getBlockPos()).getBlock() instanceof RedStoneWireBlock block && itemStack.is(FMItems.REDSTONE_AND_STEEL.asItem())) {
var state = level.getBlockState(hitResult.getBlockPos());
if (state.hasProperty(BlockStateProperties.POWER)) {
state.setValue(BlockStateProperties.POWER, 15);
level.updateNeighborsAt(blockpos, block);
return InteractionResult.SUCCESS;
}
}

return InteractionResult.PASS;
}

}
20 changes: 19 additions & 1 deletion src/main/java/net/superscary/fluxmachines/hook/WrenchHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ public static InteractionResult onPlayerUseBlock (Player player, Level level, In
if (player.isSpectator() || hand != InteractionHand.MAIN_HAND) return InteractionResult.PASS;
var itemStack = player.getItemInHand(hand);

// disassemble
if (alternateUseMode(player) && canDisassemble(itemStack)) {
var be = level.getBlockEntity(hitResult.getBlockPos());
if (be instanceof FMBaseBlockEntity baseBlockEntity) {
IS_DISASSEMBLING.set(true);
try {
var result = baseBlockEntity.disassembleWithWrench(player, level, hitResult, itemStack);
var result = baseBlockEntity.disassemble(player, level, hitResult, itemStack);
if (result.consumesAction()) {
SoundEvent sound = SoundEvents.ANVIL_HIT;
level.playSound(player, hitResult.getBlockPos(), sound, SoundSource.BLOCKS, 0.7f, 1.0f);
Expand All @@ -58,6 +59,23 @@ public static InteractionResult onPlayerUseBlock (Player player, Level level, In
}
}
}
// rotate
else if (!alternateUseMode(player) && canRotate(itemStack)) {
IS_DISASSEMBLING.set(true);
try {
var be = level.getBlockEntity(hitResult.getBlockPos());
var pos = hitResult.getBlockPos();
var state = level.getBlockState(pos);
var clickedFace = hitResult.getDirection();
if (be instanceof FMBaseBlockEntity baseBlockEntity) {
var result = baseBlockEntity.rotateOnAxis(level, hitResult, state, clickedFace);
ItemHelper.damageStack(itemStack);
return result;
}
} finally {
IS_DISASSEMBLING.remove();
}
}

return InteractionResult.PASS;
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/net/superscary/fluxmachines/item/tool/RSItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.superscary.fluxmachines.item.tool;

import net.superscary.fluxmachines.item.base.BaseItem;

public class RSItem extends BaseItem {

public RSItem (Properties properties) {
super(properties.durability(64));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.superscary.fluxmachines.mixins.chunkloading;

import net.minecraft.server.level.ChunkMap;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.ChunkPos;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(ChunkMap.class)
public class ChunkMapMixin {

@Shadow
@Final
ServerLevel level;

@Inject(at = @At("RETURN"), method = "anyPlayerCloseEnoughForSpawning", cancellable = true)
private void forceLoad (ChunkPos pos, CallbackInfoReturnable<Boolean> ci) {
if (!ci.getReturnValue()) {
ci.setReturnValue(true);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.superscary.fluxmachines.core.Tab;
import net.superscary.fluxmachines.item.base.BaseFood;
import net.superscary.fluxmachines.item.base.BaseItem;
import net.superscary.fluxmachines.item.tool.RSItem;
import net.superscary.fluxmachines.item.tool.SteelTool;
import net.superscary.fluxmachines.item.tool.Wrench;
import net.superscary.fluxmachines.util.item.ItemDefinition;
Expand All @@ -33,6 +34,8 @@ public class FMItems {
public static final ItemDefinition<BaseItem> STEEL_INGOT = item("steel_ingot", BaseItem::new);
public static final ItemDefinition<BaseItem> STEEL_NUGGET = item("steel_nugget", BaseItem::new);

public static final ItemDefinition<RSItem> REDSTONE_AND_STEEL = item("redstone_and_steel", RSItem::new);

public static final ItemDefinition<SteelArmorItem> STEEL_HELMET = item("steel_helmet", SteelArmorItem.SteelHelmet::new);
public static final ItemDefinition<SteelArmorItem> STEEL_CHESTPLATE = item("steel_chestplate", SteelArmorItem.SteelChestplate::new);
public static final ItemDefinition<SteelArmorItem> STEEL_LEGGINGS = item("steel_leggings", SteelArmorItem.SteelLeggings::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public static class Items {

public static final TagKey<Item> STEEL = createTag("steel");
public static final TagKey<Item> DUST = createTag("dust");
public static final TagKey<Item> NUGGET = createTag("nugget");

private static TagKey<Item> createTag (String key) {
return ItemTags.create(FluxMachines.getResource(key));
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions src/main/resources/fluxmachines.mixins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"required": true,
"package": "net.superscary.fluxmachines.mixins",
"compatibilityLevel": "JAVA_21",
"mixins": [
"chunkloading.ChunkMapMixin"
],
"client": [
],
"injectors": {
"defaultRequire": 1
}
}
4 changes: 2 additions & 2 deletions src/main/templates/META-INF/neoforge.mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ authors = "${mod_authors}" #optional
description = '''${mod_description}'''

# The [[mixins]] block allows you to declare your mixin config to FML so that it gets loaded.
#[[mixins]]
#config="${mod_id}.mixins.json"
[[mixins]]
config="${mod_id}.mixins.json"

# The [[accessTransformers]] block allows you to declare where your AT file is.
# If this block is omitted, a fallback attempt will be made to load an AT from META-INF/accesstransformer.cfg
Expand Down

0 comments on commit 1566ceb

Please sign in to comment.