Skip to content

Commit

Permalink
remove the speed thing it was too hard and my brain aches im gonna go…
Browse files Browse the repository at this point in the history
… sleep now gn
  • Loading branch information
AnAwesomGuy committed Jul 10, 2024
1 parent 3dc4e55 commit 55141e3
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import net.minecraft.block.BlockWithEntity;
import net.minecraft.block.ShapeContext;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityTicker;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.DyeItem;
import net.minecraft.item.Item;
Expand All @@ -33,7 +31,6 @@
import org.jetbrains.annotations.Nullable;

public class CottonCandyMachineBlock extends BlockWithEntity {
public static final int START_SPIN_TIME = 16;
public static final int TIME_FOR_ONE_LAYER = 24;

protected static final VoxelShape SHAPE = VoxelShapes.union(
Expand Down Expand Up @@ -67,18 +64,6 @@ public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new CottonCandyMachineBlockEntity(pos, state);
}

@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state,
BlockEntityType<T> type) {
return world.isClient ? null :
validateTicker(type, CottonCandyMachineBlockEntity.TYPE,
(world1, pos, state1, blockEntity) -> {
if (blockEntity.playerUsing == null)
blockEntity.decrementSpeed();
});
}

@Override
protected BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.ENTITYBLOCK_ANIMATED;
Expand Down Expand Up @@ -109,11 +94,8 @@ protected ItemActionResult onUseWithItem(ItemStack stack, BlockState state, Worl
if (world.getBlockEntity(pos) instanceof CottonCandyMachineBlockEntity machine) {
Item item = stack.getItem();
if (item instanceof CottonCandyMachineUsable) {
if (machine.playerUsing == null || machine.playerUsing == player.getUuid()) {
if (item instanceof CottonCandyItem)
stack.set(CarnivalFoods.MARKER, Unit.INSTANCE);
machine.playerUsing = player.getUuid();
}
if (item instanceof CottonCandyItem)
stack.set(CarnivalFoods.MARKER, Unit.INSTANCE);
} else {
int amount = 0;
int max = machine.getMaxCount(stack);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.anawesomguy.carnivalfoods.block.entity;

import net.anawesomguy.carnivalfoods.CarnivalFoods;
import net.anawesomguy.carnivalfoods.block.CottonCandyMachineBlock;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
Expand All @@ -14,13 +13,10 @@
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtHelper;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.registry.RegistryWrapper.WrapperLookup;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ColorHelper.Argb;
Expand All @@ -30,21 +26,12 @@
import org.jetbrains.annotations.Nullable;

import java.util.Set;
import java.util.UUID;

public class CottonCandyMachineBlockEntity extends BlockEntity implements BasicInventory {
public static final BlockEntityType<CottonCandyMachineBlockEntity> TYPE =
new BlockEntityType<>(CottonCandyMachineBlockEntity::new, Set.of(CarnivalFoods.COTTON_CANDY_MACHINE), null);

protected final DefaultedList<ItemStack> items; // 0-15 is dyes, 16 is sugar
public UUID playerUsing;

/**
* A value between 0 and 32, indicating how fast the blade will spin and how far the progress of starting the spin is.
*
* @see CottonCandyMachineBlock#START_SPIN_TIME
*/
protected byte speed;

public CottonCandyMachineBlockEntity(BlockPos pos, BlockState state) {
super(TYPE, pos, state);
Expand Down Expand Up @@ -78,30 +65,12 @@ protected void readNbt(NbtCompound nbt, WrapperLookup lookup) {
super.readNbt(nbt, lookup);
items.clear();
Inventories.readNbt(nbt, items, lookup);
speed = nbt.getByte("speed");
if (speed < (byte)0)
speed = (byte)0;
else if (speed > (byte)32)
speed = (byte)32;
NbtElement uuid = nbt.get("playerUsing");
if (uuid != null)
playerUsing = NbtHelper.toUuid(uuid);
else
playerUsing = null;
}

@Override
protected void writeNbt(NbtCompound nbt, WrapperLookup lookup) {
writeNbtWithoutPlayer(nbt, lookup);
if (playerUsing != null)
nbt.putUuid("playerUsing", playerUsing);
}

protected void writeNbtWithoutPlayer(NbtCompound nbt, WrapperLookup lookup) {
super.writeNbt(nbt, lookup);
Inventories.writeNbt(nbt, items, lookup);
if (speed > (byte)0)
nbt.putByte("speed", speed);
}

@Override
Expand All @@ -124,27 +93,7 @@ public Packet<ClientPlayPacketListener> toUpdatePacket() {

@Override
public NbtCompound toInitialChunkDataNbt(WrapperLookup lookup) {
NbtCompound nbt = new NbtCompound();
writeNbtWithoutPlayer(nbt, lookup);
return nbt;
}

public byte getSpeed() {
return speed;
}

/**
* @return {@code true} if speed was incremented.
*/
public boolean incrementSpeed() {
if (speed >= (byte)32)
return false;
speed += (byte)2;
if (speed > (byte)32)
speed = (byte)32;
if (world instanceof ServerWorld serverWorld)
serverWorld.getChunkManager().markForUpdate(pos);
return true;
return createNbt(lookup);
}

public int getCraftedColor() {
Expand Down Expand Up @@ -177,14 +126,6 @@ public int getCraftedColor() {
return Argb.getArgb(red, green, blue);
}

public void decrementSpeed() {
if (speed > 0) {
speed--;
if (world instanceof ServerWorld serverWorld)
serverWorld.getChunkManager().markForUpdate(pos);
}
}

@Internal
public static CottonCandyMachineBlockEntity createDummy() {
return new CottonCandyMachineBlockEntity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,47 +90,6 @@ public void onInitializeClient() {
context.drawText(textRenderer, text, centerX - textWidth - colorWidth, y, 16777215, true);
context.drawText(textRenderer, colorText, centerX + textWidth - colorWidth, y, color, true);
}

/*
// TODO: it doesnt work and i have no clue why and cant be bothered to fix it (draw dyes on screen)
List<ItemStack> dyes = new ArrayList<>();
for (int i = 0; i < 16; i++) {
ItemStack dyeStack = machine.getStack(i);
if (!dyeStack.isEmpty())
dyes.add(dyeStack);
}
if (dyes.isEmpty())
return;
int size = dyes.size();
int row1, row2 = 0, row3 = 0;
if (size <= 5)
row1 = size;
else if (size <= 10)
row2 = (size % (row1 = 5)) + 5;
else if (size < 16)
row3 = (size % (row1 = 5)) + (row2 = 10);
else { // size can only be 16 now
row1 = 6;
row2 = 11;
row3 = 16;
} // row1 is NEVER under 1
int startingX = width / 2;
int y = (height + 25) / 2;
int i = 0, x = startingX - row1 * 10;
// row1
for (; i <= row1; i++, x += 20)
drawStack(context, client.textRenderer, machine.getStack(i), x, y);
// row2
y += 20;
for (x = startingX - (row2 - row1) * 10; i <= row2; i++, x += 20)
drawStack(context, client.textRenderer, machine.getStack(i), x, y);
// row3
y += 20;
for (x = startingX - (row3 - row2) * 10; i <= row3; i++, x += 20)
drawStack(context, client.textRenderer, machine.getStack(i), x, y);
*/
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public static TexturedModelData createBodyLayer() {
@Override
public void render(CottonCandyMachineBlockEntity entity, float tickDelta, MatrixStack matrices,
VertexConsumerProvider vertexConsumers, int light, int overlay) {
if (entity.getSpeed() > (byte)0)
spinning.yaw = (spinning.yaw + tickDelta * ((float)entity.getSpeed() / 32F)) % PI;
spinning.yaw = (spinning.yaw + tickDelta * 0.9F) % PI;
renderStationary(matrices, vertexConsumers, light, overlay);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.anawesomguy.carnivalfoods.item;

import net.anawesomguy.carnivalfoods.CarnivalFoods;
import net.anawesomguy.carnivalfoods.block.CottonCandyMachineBlock;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
Expand All @@ -11,34 +12,31 @@
import net.minecraft.util.UseAction;
import net.minecraft.world.World;

import static net.anawesomguy.carnivalfoods.block.CottonCandyMachineBlock.START_SPIN_TIME;
import static net.anawesomguy.carnivalfoods.block.CottonCandyMachineBlock.TIME_FOR_ONE_LAYER;

public class CottonCandyItem extends CottonCandyMachineUsable {
public CottonCandyItem(Settings settings) {
super(settings);
}

@Override
public UseAction getUseAction(ItemStack stack) {
return stack.get(CarnivalFoods.MARKER) == null ? UseAction.NONE : super.getUseAction(stack);
return stack.contains(CarnivalFoods.MARKER) ? super.getUseAction(stack) : UseAction.NONE;
}

@Override
public int getMaxUseTime(ItemStack stack, LivingEntity user) {
return stack.get(CarnivalFoods.MARKER) == null ?
super.getMaxUseTime(stack, user) :
START_SPIN_TIME + TIME_FOR_ONE_LAYER * stack.getDamage();
return stack.contains(CarnivalFoods.MARKER) ?
CottonCandyMachineBlock.TIME_FOR_ONE_LAYER * stack.getDamage() :
super.getMaxUseTime(stack, user);
}

@Override
public ActionResult useOnBlock(ItemUsageContext context) {
return context.getStack().get(CarnivalFoods.MARKER) == null ? ActionResult.PASS : super.useOnBlock(context);
return context.getStack().contains(CarnivalFoods.MARKER) ? super.useOnBlock(context) : ActionResult.PASS;
}

@Override
public void usageTick(World world, LivingEntity user, ItemStack stack, int remainingUseTicks) {
if (stack.get(CarnivalFoods.MARKER) != null)
if (stack.contains(CarnivalFoods.MARKER))
super.usageTick(world, user, stack, remainingUseTicks);
}

Expand All @@ -51,7 +49,7 @@ public void onStoppedUsing(ItemStack stack, World world, LivingEntity user, int
@Override
public ItemStack finishUsing(ItemStack stack, World world, LivingEntity user) {
stack.remove(CarnivalFoods.MARKER);
super.finishUsing(stack, world, user); // there is a mixin to LivingEntity#eatFood so this works properly
stack = super.finishUsing(stack, world, user); // there is a mixin to LivingEntity#eatFood so this works properly
if (world instanceof ServerWorld serverWorld) {
stack.damage(1, serverWorld, user instanceof ServerPlayerEntity player ? player : null, item -> {});
if (stack.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.UseAction;
Expand All @@ -36,51 +34,46 @@ public UseAction getUseAction(ItemStack stack) {

@Override
public void usageTick(World world, LivingEntity user, ItemStack stack, int remainingUseTicks) {
if (world instanceof ServerWorld) {
ItemStack sugars;
if (user instanceof PlayerEntity &&
raycast((PlayerEntity)user) instanceof BlockHitResult blockHitResult &&
blockHitResult.getType() == Type.BLOCK &&
world.getBlockEntity(blockHitResult.getBlockPos()) instanceof CottonCandyMachineBlockEntity machine &&
!(sugars = machine.getStack(16)).isEmpty()) {
if (!machine.incrementSpeed()) {
machine.playerUsing = null;
ItemStack newStack;
if (stack.isOf(CarnivalFoods.COTTON_CANDY))
(newStack = stack).setDamage(stack.getDamage() - 1);
else
newStack = CarnivalFoods.COTTON_CANDY.getDefaultStack();
int color = machine.getCraftedColor();
if (color > -1)
newStack.set(DataComponentTypes.DYED_COLOR, new DyedColorComponent(color, false));
sugars.decrement(1);
stack.decrement(1);
if (stack.getCount() < 1)
user.setStackInHand(user.getActiveHand(), newStack);
}
} else
user.stopUsingItem();
ItemStack sugars;
if (user instanceof PlayerEntity player &&
remainingUseTicks % CottonCandyMachineBlock.TIME_FOR_ONE_LAYER == 0 &&
raycast(player) instanceof BlockHitResult blockHitResult &&
blockHitResult.getType() == Type.BLOCK &&
world.getBlockEntity(blockHitResult.getBlockPos()) instanceof CottonCandyMachineBlockEntity machine &&
!(sugars = machine.getStack(16)).isEmpty()) {
ItemStack newStack;
if (stack.isOf(CarnivalFoods.COTTON_CANDY))
(newStack = stack).setDamage(stack.getDamage() - 1);
else {
newStack = CarnivalFoods.COTTON_CANDY.getDefaultStack();
stack.decrement(1);
if (stack.getCount() < 1)
player.setStackInHand(player.getActiveHand(), newStack);
else
player.getInventory().offerOrDrop(stack);
}
int color = machine.getCraftedColor();
if (color != -1)
newStack.set(DataComponentTypes.DYED_COLOR, new DyedColorComponent(color, false));
sugars.decrement(1);
} else {
stack.remove(CarnivalFoods.MARKER);
user.stopUsingItem();
}
}

@Override
public void onStoppedUsing(ItemStack stack, World world, LivingEntity user, int remainingUseTicks) {
super.onStoppedUsing(stack, world, user, remainingUseTicks);
resetPlayerUsing(user, world);
}

@Override
public ItemStack finishUsing(ItemStack stack, World world, LivingEntity user) {
resetPlayerUsing(user, world);
return super.finishUsing(stack, world, user);
stack = super.finishUsing(stack, world, user);
usageTick(world, user, stack, 0);
return stack;
}

@Override
public ActionResult useOnBlock(ItemUsageContext context) {
PlayerEntity player = context.getPlayer();
if (player != null &&
context.getWorld().getBlockEntity(context.getBlockPos()) instanceof CottonCandyMachineBlockEntity machine &&
(machine.playerUsing == null || machine.playerUsing == player.getUuid()))
context.getWorld().getBlockEntity(context.getBlockPos()) instanceof CottonCandyMachineBlockEntity machine)
if (machine.getStack(16).isEmpty())
player.sendMessage(Text.translatable("message.carnival-foods.cotton_candy_machine_fail"), true);
else {
Expand All @@ -95,12 +88,4 @@ protected HitResult raycast(PlayerEntity player) {
return ProjectileUtil.getCollision(player, entity -> !entity.isSpectator() && entity.canHit(),
player.getBlockInteractionRange());
}

private void resetPlayerUsing(LivingEntity user, World world) {
if (user instanceof ServerPlayerEntity &&
raycast((PlayerEntity)user) instanceof BlockHitResult blockHitResult &&
blockHitResult.getType() == Type.BLOCK &&
world.getBlockEntity(blockHitResult.getBlockPos()) instanceof CottonCandyMachineBlockEntity machine)
machine.playerUsing = null;
}
}
Loading

0 comments on commit 55141e3

Please sign in to comment.