-
Notifications
You must be signed in to change notification settings - Fork 2
/
JackInTheBoxItem.java
88 lines (75 loc) · 3.42 KB
/
JackInTheBoxItem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.example.examplemod.item;
import com.example.examplemod.client.renderer.item.JackInTheBoxRenderer;
import com.example.examplemod.registry.SoundRegistry;
import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;
import software.bernie.geckolib.animatable.GeoItem;
import software.bernie.geckolib.animatable.SingletonGeoAnimatable;
import software.bernie.geckolib.animatable.client.GeoRenderProvider;
import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.animation.AnimatableManager;
import software.bernie.geckolib.animation.AnimationController;
import software.bernie.geckolib.animation.PlayState;
import software.bernie.geckolib.animation.RawAnimation;
import software.bernie.geckolib.util.ClientUtil;
import software.bernie.geckolib.util.GeckoLibUtil;
import java.util.function.Consumer;
/**
* Example {@link GeoItem} implementation in the form of a Jack-in-the-Box.<br>
*/
public final class JackInTheBoxItem extends Item implements GeoItem {
private static final RawAnimation POPUP_ANIM = RawAnimation.begin().thenPlay("use.popup");
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
public JackInTheBoxItem(Properties properties) {
super(properties);
// Register our item as server-side handled.
// This enables both animation data syncing and server-side animation triggering
SingletonGeoAnimatable.registerSyncedAnimatable(this);
}
// Create our armor model/renderer and return it
@Override
public void createGeoRenderer(Consumer<GeoRenderProvider> consumer) {
consumer.accept(new GeoRenderProvider() {
private JackInTheBoxRenderer renderer;
@Override
@Nullable
public BlockEntityWithoutLevelRenderer getGeoItemRenderer() {
if (this.renderer == null)
this.renderer = new JackInTheBoxRenderer();
// Defer creation of our renderer then cache it so that it doesn't get instantiated too early
return this.renderer;
}
});
}
// Let's add our animation controller
@Override
public void registerControllers(AnimatableManager.ControllerRegistrar controllers) {
controllers.add(new AnimationController<>(this, "popup_controller", 20, state -> PlayState.STOP)
.triggerableAnim("box_open", POPUP_ANIM)
// We've marked the "box_open" animation as being triggerable from the server
.setSoundKeyframeHandler(state -> {
// Use helper method to avoid client-code in common class
Player player = ClientUtil.getClientPlayer();
if (player != null)
player.playSound(SoundRegistry.JACK_MUSIC.get(), 1, 1);
}));
}
// Let's handle our use method so that we activate the animation when right-clicking while holding the box
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (level instanceof ServerLevel serverLevel)
triggerAnim(player, GeoItem.getOrAssignId(player.getItemInHand(hand), serverLevel), "popup_controller", "box_open");
return super.use(level, player, hand);
}
@Override
public AnimatableInstanceCache getAnimatableInstanceCache() {
return this.cache;
}
}