Skip to content

Commit

Permalink
Minor cleanup to comparatorBetterItemFrames
Browse files Browse the repository at this point in the history
Started by just wanting to remove the id field...
Also wondering, can `getEntitiesByClass` actually give nulls?
  • Loading branch information
altrisi committed Oct 13, 2023
1 parent c1e25d0 commit 7524e1b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 29 deletions.
14 changes: 4 additions & 10 deletions src/main/java/carpetextra/CarpetExtraSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,10 @@
public class CarpetExtraSettings
{
public enum ComparatorOptions {
VANILLA(0),
BEHIND(1),
LENIENT(2),
EXTENDED(3);

public final int id;

ComparatorOptions(int id) {
this.id = id;
}
VANILLA,
BEHIND,
LENIENT,
EXTENDED;
}

public static final String EXTRA = "extras";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package carpetextra.mixins;

import carpetextra.CarpetExtraSettings;
import carpetextra.CarpetExtraSettings.ComparatorOptions;
import net.minecraft.block.AbstractRedstoneGateBlock;
import net.minecraft.block.BlockState;
import net.minecraft.block.ComparatorBlock;
Expand All @@ -14,7 +15,6 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

Expand All @@ -25,25 +25,25 @@ protected ComparatorBlock_comparatorBetterItemFramesMixin(Settings settings) {
}

private ItemFrameEntity getAttachedItemFrameHorizontal(World world, Direction facing, BlockPos pos) {
List<ItemFrameEntity> list = new ArrayList<>();
list.addAll(world.getEntitiesByClass(ItemFrameEntity.class, new Box(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1), (itemFrameEntity) -> {
return itemFrameEntity != null && (CarpetExtraSettings.comparatorBetterItemFrames.id >= 2 || itemFrameEntity.getHorizontalFacing() == facing);
}));
if (list.size() == 0 && CarpetExtraSettings.comparatorBetterItemFrames.id >= 3) {
list.addAll(world.getEntitiesByClass(ItemFrameEntity.class, new Box(pos.getX() - 0.3, pos.getY()-0.3, pos.getZ()-0.3, pos.getX() + 1.3, pos.getY() + 1.3, pos.getZ()+1.3), Objects::nonNull));
List<ItemFrameEntity> list;
list = world.getEntitiesByClass(ItemFrameEntity.class, new Box(pos), (itemFrame) -> {
return itemFrame != null && (CarpetExtraSettings.comparatorBetterItemFrames.ordinal() >= 2 || itemFrame.getHorizontalFacing() == facing);
});
if (list.isEmpty() && CarpetExtraSettings.comparatorBetterItemFrames == ComparatorOptions.EXTENDED) {
list = world.getEntitiesByClass(ItemFrameEntity.class, new Box(pos).expand(0.3), Objects::nonNull);
}
return list.size() >= 1 ? list.get(0) : null;
return !list.isEmpty() ? list.get(0) : null;
}

private ItemFrameEntity getUnAttachedItemFrameHorizontal(World world, Direction facing, BlockPos pos) {
//If item frame is sitting in front of the comparator, horizontally or on another block
List<ItemFrameEntity> list = world.getEntitiesByClass(ItemFrameEntity.class, new Box(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1), Objects::nonNull);
return list.size() == 1 ? list.get(0) : null;
// If item frame is sitting in front of the comparator, horizontally or on another block
List<ItemFrameEntity> list = world.getEntitiesByClass(ItemFrameEntity.class, new Box(pos), Objects::nonNull);
return !list.isEmpty() ? list.get(0) : null;
}

@Inject(method = "getPower", at = @At(value = "INVOKE"), cancellable = true)
protected void isSolidBlockOrAir(World world, BlockPos pos, BlockState state, CallbackInfoReturnable<Integer> cir) {
if (CarpetExtraSettings.comparatorBetterItemFrames.id >= 1) {
if (CarpetExtraSettings.comparatorBetterItemFrames != ComparatorOptions.VANILLA) {
int i = super.getPower(world, pos, state);
Direction direction = state.get(FACING);
BlockPos blockPos = pos.offset(direction);
Expand All @@ -59,9 +59,9 @@ protected void isSolidBlockOrAir(World world, BlockPos pos, BlockState state, Ca
i = j;
}
} else {
ItemFrameEntity itemFrameEntity = getUnAttachedItemFrameHorizontal(world,direction,blockPos);
if (itemFrameEntity != null) {
i = itemFrameEntity.getComparatorPower();
ItemFrameEntity itemFrame = getUnAttachedItemFrameHorizontal(world,direction,blockPos);
if (itemFrame != null) {
i = itemFrame.getComparatorPower();
}
}
cir.setReturnValue(i);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package carpetextra.mixins;

import carpetextra.CarpetExtraSettings;
import carpetextra.CarpetExtraSettings.ComparatorOptions;
import net.minecraft.block.Blocks;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.decoration.AbstractDecorationEntity;
Expand All @@ -14,20 +15,20 @@

@Mixin(ItemFrameEntity.class)
public abstract class ItemFrameEntity_comparatorBetterItemFramesMixin extends AbstractDecorationEntity {
public ItemFrameEntity_comparatorBetterItemFramesMixin(EntityType<? extends ItemFrameEntity> entityType_1, World world_1) {
super(entityType_1, world_1);
public ItemFrameEntity_comparatorBetterItemFramesMixin(EntityType<? extends ItemFrameEntity> type, World world) {
super(type, world);
}

@Inject(method = "setHeldItemStack(Lnet/minecraft/item/ItemStack;Z)V", at = @At(value = "INVOKE", target="Lnet/minecraft/world/World;updateComparators(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/Block;)V"), cancellable = true)
private void onStackChangeUpdateComparatorDownwards(ItemStack value, boolean update, CallbackInfo ci) {
if (CarpetExtraSettings.comparatorBetterItemFrames.id == 3) {
if (CarpetExtraSettings.comparatorBetterItemFrames == ComparatorOptions.EXTENDED) {
this.getWorld().updateComparators(this.attachmentPos.offset(this.getHorizontalFacing().getOpposite()), Blocks.AIR);
}
}

@Inject(method = "setRotation(IZ)V", at = @At(value = "INVOKE", target="Lnet/minecraft/world/World;updateComparators(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/Block;)V"), cancellable = true)
private void onRotationUpdateComparatorDownwards(int value, boolean bl, CallbackInfo ci) {
if (CarpetExtraSettings.comparatorBetterItemFrames.id == 3) {
if (CarpetExtraSettings.comparatorBetterItemFrames == ComparatorOptions.EXTENDED) {
this.getWorld().updateComparators(this.attachmentPos.offset(this.getHorizontalFacing().getOpposite()), Blocks.AIR);
}
}
Expand Down

0 comments on commit 7524e1b

Please sign in to comment.