Skip to content

Commit

Permalink
Allow customizing the block tag that ornaments can be placed on
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 committed Dec 24, 2023
1 parent 2499d4e commit fb54749
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
14 changes: 4 additions & 10 deletions src/main/java/xyz/nucleoid/extras/lobby/NEItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -857,18 +857,12 @@ private static ActionResult onUseBlock(PlayerEntity player, World world, Hand ha
var lobbyState = PlayerLobbyState.get(player);

if (lobbyState.collectTaterFromBlock(world, pos, stack, player) == ActionResult.PASS && !(stack.getItem() instanceof TaterBoxItem)) {
var state = world.getBlockState(pos);
var serverWorld = (ServerWorld) world;
var blockEntity = TreeDecorationBlockEntity.findNearestTreeDecoration(serverWorld, pos);

if (state.isIn(BlockTags.LEAVES)) {
var serverWorld = (ServerWorld) world;
var blockEntity = TreeDecorationBlockEntity.findNearestTreeDecoration(serverWorld, pos);

if (blockEntity.isPresent() && blockEntity.get().placeOrnament((ServerPlayerEntity) player, serverWorld, hand, hitResult)) {
return ActionResult.SUCCESS;
}
if (blockEntity.isPresent() && blockEntity.get().placeOrnament((ServerPlayerEntity) player, serverWorld, hand, hitResult)) {
return ActionResult.SUCCESS;
}

return ActionResult.PASS;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import net.minecraft.block.Block;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.util.Identifier;
import net.minecraft.world.poi.PointOfInterestType;
import xyz.nucleoid.extras.NucleoidExtras;

public class NEPointOfInterestTypes {
public static final RegistryKey<PointOfInterestType> TREE_DECORATION = of("tree_decoration");
Expand All @@ -15,7 +15,7 @@ public static void register() {
}

private static RegistryKey<PointOfInterestType> of(String id) {
return RegistryKey.of(RegistryKeys.POINT_OF_INTEREST_TYPE, new Identifier(id));
return RegistryKey.of(RegistryKeys.POINT_OF_INTEREST_TYPE, NucleoidExtras.identifier(id));
}

private static PointOfInterestType register(RegistryKey<PointOfInterestType> key, int ticketCount, int searchDistance, Block... blocks) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,17 @@ public boolean placeOrnament(ServerPlayerEntity player, ServerWorld world, Hand
var item = TaterBoxItem.getPrimaryCollectedTater(player).asItem();
if (item == Items.AIR) return false;

var blockPos = hitResult.getBlockPos();

var state = world.getBlockState(blockPos);
if (!state.isIn(this.data.getSupportedBlocks())) return false;

var pos = hitResult.getPos();
var side = hitResult.getSide();

if (side == Direction.UP) {
return false;
} else if (side != Direction.DOWN) {
var blockPos = hitResult.getBlockPos();
var belowPos = blockPos.add(side.getOffsetX(), side.getOffsetY() - 1, side.getOffsetZ());

if (world.getBlockState(belowPos).isFullCube(world, belowPos)) {
Expand Down
22 changes: 17 additions & 5 deletions src/main/java/xyz/nucleoid/extras/lobby/tree/TreeDecoration.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,35 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

import net.minecraft.block.Block;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.tag.BlockTags;
import net.minecraft.registry.tag.TagKey;

public final class TreeDecoration {
public static final Codec<TreeDecoration> CODEC = RecordCodecBuilder.create(instance ->
instance.group(
Ornament.CODEC.listOf().fieldOf("ornaments").forGetter(t -> t.ornaments)
Ornament.CODEC.listOf().fieldOf("ornaments").forGetter(t -> t.ornaments),
TagKey.codec(RegistryKeys.BLOCK).fieldOf("supported_blocks").forGetter(TreeDecoration::getSupportedBlocks)
).apply(instance, TreeDecoration::new)
);

private final List<Ornament> ornaments;
private final TagKey<Block> supportedBlocks;

private TreeDecoration(List<Ornament> ornaments) {
private TreeDecoration(List<Ornament> ornaments, TagKey<Block> supportedBlocks) {
this.ornaments = ornaments;
this.supportedBlocks = supportedBlocks;
}

public Collection<Ornament> getOrnaments() {
return this.ornaments;
}

public TagKey<Block> getSupportedBlocks() {
return this.supportedBlocks;
}

public TreeDecoration withOrnament(Ornament ornament) {
var ornaments = new ArrayList<>(this.ornaments);

Expand All @@ -34,18 +46,18 @@ public TreeDecoration withOrnament(Ornament ornament) {

ornaments.add(ornament);

return new TreeDecoration(ornaments);
return new TreeDecoration(ornaments, this.supportedBlocks);
}

public TreeDecoration exceptOrnament(Ornament ornament) {
var ornaments = new ArrayList<>(this.ornaments);

ornaments.remove(ornament);

return new TreeDecoration(ornaments);
return new TreeDecoration(ornaments, this.supportedBlocks);
}

public static TreeDecoration createEmpty() {
return new TreeDecoration(List.of());
return new TreeDecoration(List.of(), BlockTags.LEAVES);
}
}

0 comments on commit fb54749

Please sign in to comment.