Skip to content

Commit

Permalink
Misc cleanup and thurible clean
Browse files Browse the repository at this point in the history
  • Loading branch information
dhyces committed Apr 2, 2024
1 parent f8680c2 commit 2128962
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ static Optional<VitalHolder> of(Object context) {
int getVitalThuribleLevel();

void setVitalThuribleLevel(int vital);

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

import java.util.Objects;

public class VitalThuribleBlock extends HorizontalFacingBlock implements BlockEntityProvider{
public class VitalThuribleBlock extends HorizontalFacingBlock implements BlockEntityProvider {
public static final BooleanProperty ACTIVE = BooleanProperty.of("active");
private static final VoxelShape SHAPES;

Expand All @@ -44,27 +44,27 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
if (hand == Hand.MAIN_HAND && !isActivateItem(itemStack) && isActivateItem(player.getStackInHand(Hand.OFF_HAND))) {
return ActionResult.PASS;
} else if (isActivateItem(itemStack) && !state.get(ACTIVE)) {
if(!world.isClient() && world.getBlockEntity(pos) != null){
((VitalThuribleBlockEntity) Objects.requireNonNull(world.getBlockEntity(pos))).onUse(player, hand);
if (!world.isClient() && world.getBlockEntity(pos) instanceof VitalThuribleBlockEntity vitalThuribleBlockEntity) {
vitalThuribleBlockEntity.onUse(player, hand);
}
return ActionResult.success(true);
}
return super.onUse(state, world, pos, player, hand, hit);
}

private static boolean isActivateItem(ItemStack stack) {
public static boolean isActivateItem(ItemStack stack) {
return stack.isOf(ModItems.WRONGMEAT);
}

public static void activate(World world, BlockPos pos, BlockState state) {
if(!state.get(ACTIVE)){
if (!state.get(ACTIVE)) {
world.setBlockState(pos, state.with(ACTIVE, true));
world.playSound(null, (double)pos.getX() + 0.5, (double)pos.getY() + 0.5, (double)pos.getZ() + 0.5, SoundEvents.ENTITY_GHAST_SHOOT, SoundCategory.BLOCKS, 1.0F, 1.0F);
}
}

public void genParticle(ParticleEffect particleEffect, World world, BlockPos pos, Random random){
for(int i = 0; i < random.nextInt(1) + 1; ++i) {
for (int i = 0; i < random.nextInt(1) + 1; ++i) {
double d = (double)pos.getX() + 0.25 + random.nextDouble() / 2;
double e = (double)pos.getY() + random.nextDouble() / 2;
double f = (double)pos.getZ() + 0.25 + random.nextDouble() / 2;
Expand All @@ -91,7 +91,7 @@ public BlockState getPlacementState(ItemPlacementContext ctx) {

@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(ACTIVE).add(FACING);
builder.add(ACTIVE, FACING);
}


Expand Down Expand Up @@ -121,5 +121,4 @@ public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) {
return (tickerWorld, pos, tickerState, blockEntity) -> VitalThuribleBlockEntity.tick(tickerWorld, pos, tickerState, (VitalThuribleBlockEntity) blockEntity);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventories;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.SingleStackInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
Expand All @@ -30,8 +29,7 @@

import java.util.UUID;


public class VitalThuribleBlockEntity extends BlockEntity implements Inventory {
public class VitalThuribleBlockEntity extends BlockEntity implements SingleStackInventory {
protected DefaultedList<ItemStack> inventory = DefaultedList.ofSize(1, ItemStack.EMPTY);
public UUID targetUUID = null;
private int timer = 0;
Expand All @@ -40,7 +38,6 @@ public VitalThuribleBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntityTypes.VITAL_THURIBLE_BLOCK_ENTITY, pos, state);
}


@Override
public NbtCompound toInitialChunkDataNbt() {
NbtCompound nbt = super.toInitialChunkDataNbt();
Expand All @@ -54,12 +51,10 @@ public Packet<ClientPlayPacketListener> toUpdatePacket() {
return BlockEntityUpdateS2CPacket.create(this, (BlockEntity b) -> this.toNbt());
}



public void sync() {
if (world != null && !world.isClient) {
world.updateListeners(pos, getCachedState(), getCachedState(), Block.NOTIFY_LISTENERS);
toUpdatePacket();
markDirty();
}
}

Expand All @@ -74,7 +69,7 @@ public void readNbt(NbtCompound nbt) {
uUID = nbt.getUuid("uuid");
} else {
String string = nbt.getString("uuid");
if(this.getWorld() != null){
if (this.getWorld() != null) {
uUID = ServerConfigHandler.getPlayerUuidByName(this.getWorld().getServer(), string);
}
}
Expand All @@ -97,7 +92,7 @@ protected void writeNbt(NbtCompound nbt) {

public static void tick(World world, BlockPos pos, BlockState state, VitalThuribleBlockEntity blockEntity) {
if (world != null) {
if(blockEntity.inventory.get(0).isOf(ModItems.WRONGMEAT) && blockEntity.inventory.get(0).getCount() == 5){
if (blockEntity.getStack().isOf(ModItems.WRONGMEAT) && blockEntity.getStack().getCount() == 5) {
blockEntity.timer++;
if (world.isClient) {
if (blockEntity.timer > 0) {
Expand All @@ -112,9 +107,9 @@ public static void tick(World world, BlockPos pos, BlockState state, VitalThurib
}
}
}
if(!world.isClient){
if(blockEntity.timer > 20 * 2){
if(blockEntity.targetUUID != null){
if (!world.isClient) {
if (blockEntity.timer > 20 * 2) {
if (blockEntity.targetUUID != null) {
PlayerEntity player = world.getPlayerByUuid(blockEntity.targetUUID);

VitalHolder.of(player).ifPresent(vital -> {
Expand All @@ -125,15 +120,15 @@ public static void tick(World world, BlockPos pos, BlockState state, VitalThurib
});
}

blockEntity.inventory.clear();
blockEntity.clear();
blockEntity.timer = 0;
blockEntity.targetUUID = null;
blockEntity.sync();
markDirty(world, pos, state);
world.setBlockState(pos, state.with(VitalThuribleBlock.ACTIVE, false));
world.playSound(null, (double)pos.getX() + 0.5, (double)pos.getY() + 0.5, (double)pos.getZ() + 0.5, SoundEvents.BLOCK_FIRE_EXTINGUISH, SoundCategory.BLOCKS, 1.0F, 1.0F);
}
if(blockEntity.timer > 0){
if (blockEntity.timer > 0) {
VitalThuribleBlock.activate(world, pos, state);
}
}
Expand All @@ -143,12 +138,12 @@ public static void tick(World world, BlockPos pos, BlockState state, VitalThurib

public void onUse(PlayerEntity player, Hand hand) {
ItemStack stack = player.getStackInHand(hand);
if(stack.isOf(ModItems.WRONGMEAT) && inventory.get(0).getCount() < 5){
if ((!hasOwner() || isOwner(player)) && VitalThuribleBlock.isActivateItem(stack) && getStack().getCount() < 5) {
targetUUID = player.getUuid();
if(getStack(0).isOf(Items.AIR)){
setStack(0, new ItemStack(ModItems.WRONGMEAT));
}else{
getStack(0).increment(1);
if (getStack().isEmpty()) {
setStack(stack.copy().split(1));
} else {
getStack().increment(1);
}
sync();
AylythUtil.decreaseStack(stack, player);
Expand All @@ -162,50 +157,36 @@ public NbtCompound toNbt(){
return rtn;
}

@Override
public int size() {
return 1;
public boolean hasOwner() {
return targetUUID != null;
}

@Override
public boolean isEmpty() {
for (int i = 0; i < size(); i++) {
if (getStack(i).isEmpty()) {
return false;
}
}
return true;
public boolean isOwner(PlayerEntity player) {
return player.getUuid().equals(targetUUID);
}

@Override
public ItemStack getStack(int slot) {
return inventory.get(slot);
return inventory.get(0);
}

@Override
public ItemStack removeStack(int slot, int amount) {
return Inventories.splitStack(inventory, slot, amount);
return Inventories.splitStack(inventory, 0, amount);
}

@Override
public ItemStack removeStack(int slot) {
return Inventories.removeStack(inventory, slot);
return Inventories.removeStack(inventory, 0);
}

@Override
public void setStack(int slot, ItemStack stack) {
inventory.set(slot, stack);
inventory.set(0, stack);
}

@Override
public boolean canPlayerUse(PlayerEntity player) {
return true;
}

@Override
public void clear() {
inventory.clear();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ private void aylyth_injectLootDrop(CallbackInfo ci){
}
}



@Inject(method = "canHaveStatusEffect", at = @At("HEAD"), cancellable = true)
public void aylyth_canHaveStatusEffect(StatusEffectInstance effect, CallbackInfoReturnable<Boolean> cir) {
if (this.isPlayer()) {
Expand All @@ -112,5 +110,4 @@ public void aylyth_canHaveStatusEffect(StatusEffectInstance effect, CallbackInfo

}
}

}

0 comments on commit 2128962

Please sign in to comment.