Skip to content

Commit

Permalink
MORE color handling
Browse files Browse the repository at this point in the history
  • Loading branch information
robotgryphon committed Apr 8, 2024
1 parent 895c880 commit 37d6416
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ItemStack apply(ItemStack stack, LootContext ctx) {
if(state.is(MachineConstants.MACHINE_BLOCK)) {
var data = ctx.getParam(LootContextParams.BLOCK_ENTITY);
if (data instanceof BoundCompactMachineBlockEntity machine && stack.getItem() instanceof IBoundCompactMachineItem bound) {
stack.setData(Machines.MACHINE_COLOR, machine.getColor());
stack.setData(Machines.MACHINE_COLOR, machine.getData(Machines.MACHINE_COLOR));
bound.setRoom(stack, machine.connectedRoom());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public BoundCompactMachineBlock(Properties props) {
public ItemStack getCloneItemStack(BlockState state, HitResult target, LevelReader level, BlockPos pos, Player player) {
try {
if (level.getBlockEntity(pos) instanceof BoundCompactMachineBlockEntity be) {
return MachineCreator.boundToRoom(be.connectedRoom(), be.getColor());
return MachineCreator.boundToRoom(be.connectedRoom(), be.getData(Machines.MACHINE_COLOR));
}

return MachineCreator.unbound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public CompoundTag getUpdateTag() {
public void handleUpdateTag(CompoundTag tag) {
super.handleUpdateTag(tag);

if(tag.contains(NBT_ROOM_CODE))
this.roomCode = tag.getString(NBT_ROOM_CODE);

if (tag.contains("players")) {
CompoundTag players = tag.getCompound("players");
// playerData = CompactMachinePlayerData.fromNBT(players);
Expand Down Expand Up @@ -149,9 +152,4 @@ public void setCustomName(Component customName) {
this.customName = customName;
this.setChanged();
}

@Override
public int getColor() {
return this.getData(Machines.MACHINE_COLOR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,33 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.GlobalPos;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.DyeItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class CompactMachineBlock extends Block {
public CompactMachineBlock(Properties pProperties) {
super(pProperties);
}

@Override
public void setPlacedBy(Level pLevel, BlockPos pPos, BlockState pState, @Nullable LivingEntity pPlacer, ItemStack pStack) {
super.setPlacedBy(pLevel, pPos, pState, pPlacer, pStack);

pStack.getExistingData(Machines.MACHINE_COLOR).ifPresent(color -> {
final var be = pLevel.getBlockEntity(pPos);
if(be != null)
be.setData(Machines.MACHINE_COLOR, color);
});
}

@NotNull
protected static InteractionResult tryDyingMachine(Level level, @NotNull BlockPos pos, Player player, DyeItem dye, ItemStack mainItem) {
var color = dye.getDyeColor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public ItemStack getCloneItemStack(BlockState state, HitResult target, LevelRead
level.getBlockEntity(pos, Machines.UNBOUND_MACHINE_ENTITY.get()).ifPresent(unboundEntity -> {
RoomTemplate template = unboundEntity.template().orElse(RoomTemplate.INVALID_TEMPLATE);
if (!template.equals(RoomTemplate.INVALID_TEMPLATE)) {
int color = unboundEntity.getData(Machines.MACHINE_COLOR);

try {
// Generate a new machine room
final var newRoom = RoomApi.newRoom(server, template, sp.getUUID());
Expand All @@ -86,6 +88,7 @@ public ItemStack getCloneItemStack(BlockState state, HitResult target, LevelRead
// Set up binding and enter
level.getBlockEntity(pos, Machines.MACHINE_ENTITY.get()).ifPresent(ent -> {
ent.setConnectedRoom(newRoom.code());
ent.setData(Machines.MACHINE_COLOR, color);

try {
RoomHelper.teleportPlayerIntoRoom(server, sp, newRoom, RoomEntryPoint.playerEnteringMachine(player));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ public void setTemplate(ResourceLocation template) {
this.setChanged();
}

@Override
public int getColor() {
return this.getData(Machines.MACHINE_COLOR);
}

@Nullable
public ResourceLocation templateId() {
return templateId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

import dev.compactmods.machines.api.Constants;
import dev.compactmods.machines.neoforge.machine.Machines;
import dev.compactmods.machines.neoforge.machine.block.UnboundCompactMachineEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
Expand All @@ -16,13 +12,11 @@ public class MachineClientEvents {

@SubscribeEvent
public static void onItemColors(final RegisterColorHandlersEvent.Item colors) {
colors.register(MachineColors.ITEM, Machines.BOUND_MACHINE_BLOCK_ITEM.get());
colors.register(MachineColors.ITEM, Machines.UNBOUND_MACHINE_BLOCK_ITEM.get());
colors.register(MachineColors.ITEM, Machines.BOUND_MACHINE_BLOCK_ITEM.get(), Machines.UNBOUND_MACHINE_BLOCK_ITEM.get());
}

@SubscribeEvent
public static void onBlockColors(final RegisterColorHandlersEvent.Block colors) {
colors.register(MachineColors.BOUND_BLOCK, Machines.MACHINE_BLOCK.get());
colors.register(MachineColors.UNBOUND_BLOCK, Machines.UNBOUND_MACHINE_BLOCK.get());
colors.register(MachineColors.BLOCK, Machines.MACHINE_BLOCK.get(), Machines.UNBOUND_MACHINE_BLOCK.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

import dev.compactmods.machines.api.machine.MachineConstants;
import dev.compactmods.machines.neoforge.machine.Machines;
import dev.compactmods.machines.neoforge.machine.block.UnboundCompactMachineEntity;
import net.minecraft.client.color.block.BlockColor;
import net.minecraft.client.color.item.ItemColor;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.block.state.BlockState;

public class MachineColors {

Expand All @@ -18,7 +14,7 @@ public class MachineColors {
return pTintIndex == 0 ? stack.getData(Machines.MACHINE_COLOR) : DEFAULT;
};

public static final BlockColor BOUND_BLOCK = (state, level, pos, tintIndex) -> {
public static final BlockColor BLOCK = (state, level, pos, tintIndex) -> {
if (!state.is(MachineConstants.MACHINE_BLOCK) || level == null || pos == null)
return DEFAULT;

Expand All @@ -28,9 +24,4 @@ public class MachineColors {

return DEFAULT;
};

public static final BlockColor UNBOUND_BLOCK = (BlockState state, BlockAndTintGetter level, BlockPos pos, int tintIndex) -> switch (tintIndex) {
case 0 -> level.getBlockEntity(pos) instanceof UnboundCompactMachineEntity unbound ? unbound.getColor() : 0xFFFFFFFF;
default -> 0xFFFFFFFF;
};
}

0 comments on commit 37d6416

Please sign in to comment.