Skip to content

Commit aee3d20

Browse files
ONCE -> TIMED
closes #62
1 parent 1f37dc2 commit aee3d20

7 files changed

Lines changed: 70 additions & 37 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ authors=HavenKing, ModFest
1717
contributors=Chai, Sisby folk, Sammy.K, ShiroJR, Superkat32, maximumpower55, CallMeEcho, quaternary, comp500, LemmaEOF, acikek, TheEpicBlock, SkyNotTheLimit, Patbox, AmyMialee, Emi
1818
license=CC0-1.0
1919
# Mod Version
20-
baseVersion=1.9.1
20+
baseVersion=2.0.0-alpha.1
2121
# Branch Metadata
2222
branch=1.21
2323
tagBranch=1.21

src/main/java/dev/hephaestus/glowcase/block/ItemProviderBlock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public boolean canPickup(PlayerEntity player, BlockPos pos) {
5050

5151
@Override
5252
public boolean canTarget(PlayerEntity player, BlockPos pos) {
53-
return super.canTarget(player, pos) || canPickup(player, pos);
53+
return super.canTarget(player, pos) || canPickup(player, pos) || !player.isCreative();
5454
}
5555

5656
@Override

src/main/java/dev/hephaestus/glowcase/block/entity/ItemProviderBlockEntity.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66
import net.minecraft.item.ItemStack;
77
import net.minecraft.nbt.NbtCompound;
88
import net.minecraft.nbt.NbtElement;
9-
import net.minecraft.nbt.NbtList;
109
import net.minecraft.registry.RegistryWrapper;
1110
import net.minecraft.util.Hand;
1211
import net.minecraft.util.math.BlockPos;
1312

14-
import java.util.HashSet;
13+
import java.util.HashMap;
14+
import java.util.Map;
1515
import java.util.Set;
1616
import java.util.UUID;
1717

1818
public class ItemProviderBlockEntity extends GlowcaseBlockEntity implements InfiniteInventory, StackInteractable {
1919
protected ItemStack stack = ItemStack.EMPTY;
2020
protected GivesItem givesItem = GivesItem.ALWAYS;
21-
protected final Set<UUID> givenTo = new HashSet<>();
21+
public long cooldown = 0;
22+
protected final Map<UUID, Long> givenTimes = new HashMap<>();
2223

2324
public ItemProviderBlockEntity(BlockPos pos, BlockState state) {
2425
super(Glowcase.ITEM_PROVIDER_BLOCK_ENTITY.get(), pos, state);
@@ -32,7 +33,7 @@ public boolean matchesStack(ItemStack stack) {
3233
@Override
3334
public void setFromStack(ItemStack stack) {
3435
this.stack = stack.copy();
35-
this.givenTo.clear();
36+
this.givenTimes.clear();
3637
this.markDirty();
3738
}
3839

@@ -61,13 +62,10 @@ public void writeNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLook
6162
super.writeNbt(tag, registryLookup);
6263
if (!this.stack.isEmpty()) tag.put("item", this.stack.encode(registryLookup));
6364
tag.putString("gives_item", this.givesItem.name());
64-
NbtList given = new NbtList();
65-
for (UUID id : givenTo) {
66-
NbtCompound givenTag = new NbtCompound();
67-
givenTag.putUuid("id", id);
68-
given.add(givenTag);
69-
}
70-
tag.put("given_to", given);
65+
tag.putLong("cooldown", this.cooldown);
66+
NbtCompound timesNbt = new NbtCompound();
67+
givenTimes.forEach((id, tick) -> timesNbt.putLong(id.toString(), tick));
68+
tag.put("given_times", timesNbt);
7169
}
7270

7371
@Override
@@ -79,28 +77,30 @@ public void readNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLooku
7977
} else {
8078
this.givesItem = GivesItem.ALWAYS;
8179
}
80+
this.cooldown = tag.getLong("cooldown");
8281

83-
givenTo.clear();
84-
if (tag.contains("given_to")) {
85-
NbtList given = tag.getList("given_to", NbtElement.COMPOUND_TYPE);
86-
for (NbtElement elem : given) {
87-
NbtCompound comp = ((NbtCompound) elem);
88-
givenTo.add(comp.getUuid("id"));
89-
}
82+
givenTimes.clear();
83+
NbtCompound given = tag.getCompound("given_times");
84+
for (String key : given.getKeys()) {
85+
givenTimes.put(UUID.fromString(key), given.getLong(key));
9086
}
9187
}
9288

9389
public void cycleGiveType() {
9490
this.givesItem = GivesItem.values()[(this.givesItem.ordinal() + 1) % GivesItem.values().length];
95-
givenTo.clear();
91+
givenTimes.clear();
9692
markDirty();
9793
}
9894

95+
public long getCooldownTicks(PlayerEntity player) {
96+
return givenTimes.containsKey(player.getUuid()) ? givenTimes.get(player.getUuid()) + this.cooldown * 20 - world.getTime() : 0;
97+
}
98+
9999
public boolean canGiveTo(PlayerEntity player) {
100100
if (!hasItem()) return false;
101101
else return switch (this.givesItem) {
102102
case ALWAYS -> true;
103-
case ONCE -> player.isCreative() || !givenTo.contains(player.getUuid());
103+
case TIMED -> player.isCreative() || getCooldownTicks(player) <= 0;
104104
case ONE -> player.isCreative() || !player.getInventory().containsAny(Set.of(stack.getItem()));
105105
};
106106
}
@@ -117,12 +117,12 @@ public void giveTo(PlayerEntity player) {
117117
player.setStackInHand(Hand.MAIN_HAND, itemStack);
118118
}
119119
if (!player.isCreative()) {
120-
givenTo.add(player.getUuid());
120+
givenTimes.put(player.getUuid(), world.getTime());
121121
markDirty();
122122
}
123123
}
124124

125125
public enum GivesItem {
126-
ALWAYS, ONCE, ONE
126+
ALWAYS, TIMED, ONE
127127
}
128128
}
Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package dev.hephaestus.glowcase.client.gui.screen.ingame;
22

3+
import com.google.common.primitives.Longs;
34
import dev.hephaestus.glowcase.block.entity.ItemProviderBlockEntity;
45
import dev.hephaestus.glowcase.packet.C2SEditItemProviderBlock;
56
import net.minecraft.client.gui.widget.ButtonWidget;
7+
import net.minecraft.client.gui.widget.TextFieldWidget;
8+
import net.minecraft.client.gui.widget.TextWidget;
69
import net.minecraft.text.Text;
710

811
public class ItemProviderBlockEditScreen extends GlowcaseScreen {
912

1013
private final ItemProviderBlockEntity providerBlock;
1114
private ButtonWidget givesItemButton;
15+
private TextFieldWidget cooldownWidget;
16+
private TextWidget secondsLabel;
1217
public ItemProviderBlockEditScreen(ItemProviderBlockEntity providerBlock) {
1318
this.providerBlock = providerBlock;
1419
}
@@ -18,22 +23,38 @@ public void init() {
1823
super.init();
1924

2025
if (this.client != null) {
21-
int padding = width / 100;
22-
int individualPadding = padding / 2;
23-
int centerW = width / 2;
24-
int centerH = height / 2;
25-
2626
this.givesItemButton = ButtonWidget.builder(Text.stringifiedTranslatable("gui.glowcase.gives_item", this.providerBlock.getGivesItem()), (action) -> {
2727
this.providerBlock.cycleGiveType();
2828
this.givesItemButton.setMessage(Text.stringifiedTranslatable("gui.glowcase.gives_item", this.providerBlock.getGivesItem()));
29-
editItemDisplayBlock();
30-
}).dimensions(centerW - 75, centerH - 40 - individualPadding, 150, 20).build();
29+
this.cooldownWidget.setVisible(this.providerBlock.getGivesItem() == ItemProviderBlockEntity.GivesItem.TIMED);
30+
this.secondsLabel.visible = this.providerBlock.getGivesItem() == ItemProviderBlockEntity.GivesItem.TIMED;
31+
if (this.providerBlock.getGivesItem() == ItemProviderBlockEntity.GivesItem.TIMED && (this.cooldownWidget.getText().isBlank() || this.cooldownWidget.getText().equals("0"))) this.cooldownWidget.setText(String.valueOf(60));
32+
}).dimensions(width / 2 - 75, height / 2 - 25, 150, 20).build();
33+
34+
this.cooldownWidget = new TextFieldWidget(this.textRenderer, width / 2 - 30, height / 2 + 5, 60, 20, Text.empty());
35+
this.cooldownWidget.setText(this.providerBlock.cooldown == 0 ? "" : String.valueOf(this.providerBlock.cooldown));
36+
this.cooldownWidget.setPlaceholder(Text.translatable("gui.glowcase.cooldown"));
37+
this.cooldownWidget.setTextPredicate(s -> s.matches("\\d*"));
38+
this.cooldownWidget.setVisible(this.providerBlock.getGivesItem() == ItemProviderBlockEntity.GivesItem.TIMED);
39+
40+
this.secondsLabel = new TextWidget(width / 2 + 30, height / 2 + 5, 10, 20, Text.of("s"), this.textRenderer);
41+
this.secondsLabel.visible = this.providerBlock.getGivesItem() == ItemProviderBlockEntity.GivesItem.TIMED;
3142

3243
this.addDrawableChild(this.givesItemButton);
44+
this.addDrawableChild(this.cooldownWidget);
45+
this.addDrawableChild(this.secondsLabel);
3346
}
3447
}
3548

36-
private void editItemDisplayBlock() {
49+
@Override
50+
public void close() {
51+
if (this.providerBlock.getGivesItem() != ItemProviderBlockEntity.GivesItem.TIMED) {
52+
this.providerBlock.cooldown = 0;
53+
} else if (Longs.tryParse(cooldownWidget.getText()) instanceof Long l) {
54+
this.providerBlock.cooldown = Math.clamp(l, 0, 172800000 /* 48 Hours */);
55+
}
56+
3757
C2SEditItemProviderBlock.of(providerBlock).send();
58+
super.close();
3859
}
3960
}

src/main/java/dev/hephaestus/glowcase/client/render/block/entity/ItemProviderBlockEntityRenderer.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
import net.minecraft.item.BlockItem;
1919
import net.minecraft.item.ItemStack;
2020
import net.minecraft.text.Text;
21+
import net.minecraft.util.Formatting;
2122
import net.minecraft.util.Identifier;
2223
import net.minecraft.util.hit.BlockHitResult;
2324
import net.minecraft.util.hit.HitResult;
2425
import net.minecraft.util.math.Direction;
2526
import net.minecraft.util.math.RotationAxis;
2627
import net.minecraft.util.math.Vec2f;
28+
import org.apache.commons.lang3.time.DurationFormatUtils;
2729

2830
public record ItemProviderBlockEntityRenderer(BlockEntityRendererFactory.Context context) implements BlockEntityRenderer<ItemProviderBlockEntity> {
2931
public static Identifier ITEM_TEXTURE = Glowcase.id("textures/item/item_provider_block.png");
@@ -101,9 +103,16 @@ public void render(ItemProviderBlockEntity entity, float tickDelta, MatrixStack
101103

102104
if (!stack.isEmpty()) {
103105
matrices.push();
104-
Text countText = Text.literal("%dx".formatted(entity.getStack().getCount()));
105-
matrices.translate(-context.getTextRenderer().getWidth(countText) + 16, 32, 0);
106-
context.getTextRenderer().draw(countText, 0, 0, 0xFFFFFF, false, matrices.peek().getPositionMatrix(), vertexConsumers, TextRenderer.TextLayerType.NORMAL, 0, LightmapTextureManager.MAX_LIGHT_COORDINATE);
106+
if (entity.canGiveTo(MinecraftClient.getInstance().player)) {
107+
Text countText = Text.literal("%dx".formatted(entity.getStack().getCount()));
108+
matrices.translate(-context.getTextRenderer().getWidth(countText) + 16, 32, 0);
109+
context.getTextRenderer().draw(countText, 0, 0, 0xFFFFFF, false, matrices.peek().getPositionMatrix(), vertexConsumers, TextRenderer.TextLayerType.NORMAL, 0, LightmapTextureManager.MAX_LIGHT_COORDINATE);
110+
} else {
111+
long cooldownMS = entity.getCooldownTicks(MinecraftClient.getInstance().player) * 50;
112+
Text countText = Text.literal("[%s]".formatted(entity.getGivesItem() == ItemProviderBlockEntity.GivesItem.TIMED ? DurationFormatUtils.formatDuration(cooldownMS, cooldownMS > 3600000 ? "HH:mm:ss" : "mm:ss") : "MAX")).formatted(Formatting.YELLOW);
113+
matrices.translate(-context.getTextRenderer().getWidth(countText) + 16, 24, 0);
114+
context.getTextRenderer().draw(countText, 0, 0, 0xFFFFFF, false, matrices.peek().getPositionMatrix(), vertexConsumers, TextRenderer.TextLayerType.NORMAL, 0, LightmapTextureManager.MAX_LIGHT_COORDINATE);
115+
}
107116
matrices.pop();
108117
}
109118
matrices.pop();

src/main/java/dev/hephaestus/glowcase/packet/C2SEditItemProviderBlock.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@
1010
import net.minecraft.server.world.ServerWorld;
1111
import net.minecraft.util.math.BlockPos;
1212

13-
public record C2SEditItemProviderBlock(BlockPos pos, ItemProviderBlockEntity.GivesItem givesItem) implements C2SEditBlockEntity {
13+
public record C2SEditItemProviderBlock(BlockPos pos, ItemProviderBlockEntity.GivesItem givesItem, long cooldown) implements C2SEditBlockEntity {
1414
public static final CustomPayload.Id<C2SEditItemProviderBlock> ID = new CustomPayload.Id<>(Glowcase.id("channel.item_provider"));
1515
public static final PacketCodec<RegistryByteBuf, C2SEditItemProviderBlock> PACKET_CODEC = PacketCodec.tuple(
1616
BlockPos.PACKET_CODEC, C2SEditItemProviderBlock::pos,
1717
PacketCodecs.BYTE.xmap(index -> ItemProviderBlockEntity.GivesItem.values()[index], givesItem -> (byte) givesItem.ordinal()), C2SEditItemProviderBlock::givesItem,
18+
PacketCodecs.VAR_LONG, C2SEditItemProviderBlock::cooldown,
1819
C2SEditItemProviderBlock::new
1920
);
2021

2122
public static C2SEditItemProviderBlock of(ItemProviderBlockEntity be) {
22-
return new C2SEditItemProviderBlock(be.getPos(), be.getGivesItem());
23+
return new C2SEditItemProviderBlock(be.getPos(), be.getGivesItem(), be.cooldown);
2324
}
2425

2526
@Override
@@ -31,5 +32,6 @@ public CustomPayload.Id<? extends CustomPayload> getId() {
3132
public void receive(ServerWorld world, BlockEntity blockEntity) {
3233
if (!(blockEntity instanceof ItemProviderBlockEntity be)) return;
3334
be.setGivesItem(this.givesItem());
35+
be.cooldown = this.cooldown;
3436
}
3537
}

src/main/resources/assets/glowcase/lang/en_us.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"gui.glowcase.count": "Count",
6464
"gui.glowcase.pulse": "Pulse Ticks",
6565
"gui.glowcase.output_direction": "Pulse/Push Side: %s",
66+
"gui.glowcase.cooldown": "Cooldown",
6667
"gui.glowcase.alt": "Alt-Text",
6768
"gui.glowcase.previous": "Previous",
6869
"gui.glowcase.next": "Next",

0 commit comments

Comments
 (0)