Skip to content

Commit

Permalink
Add creative tater boxes (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 authored Sep 8, 2023
1 parent 139c2a3 commit 8dcd27b
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 29 deletions.
8 changes: 7 additions & 1 deletion src/main/java/xyz/nucleoid/extras/lobby/NEItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import xyz.nucleoid.extras.NucleoidExtrasConfig;
import xyz.nucleoid.extras.lobby.block.tater.TinyPotatoBlock;
import xyz.nucleoid.extras.lobby.item.*;
import xyz.nucleoid.extras.lobby.item.tater.CreativeTaterBoxItem;
import xyz.nucleoid.extras.lobby.item.tater.TaterBoxItem;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -85,7 +87,7 @@ public class NEItems {
entries.add(NEItems.YELLOW_CONCRETE_POWDER);
entries.add(NEItems.GAME_PORTAL_OPENER);
entries.add(NEItems.TATER_BOX);
TaterBoxItem.addToItemGroup(entries);
entries.add(NEItems.CREATIVE_TATER_BOX);
TATERS.forEach(entries::add);
})
.build();
Expand Down Expand Up @@ -433,6 +435,8 @@ public class NEItems {
public static final Item CORRUPTATER = createHead(NEBlocks.CORRUPTATER);

public static final Item TATER_BOX = new TaterBoxItem(new Item.Settings().maxDamage(0));
public static final Item CREATIVE_TATER_BOX = new CreativeTaterBoxItem(new Item.Settings().maxDamage(0));

public static final Item QUICK_ARMOR_STAND = new QuickArmorStandItem(new Item.Settings());
public static final Item GAME_PORTAL_OPENER = new GamePortalOpenerItem(new Item.Settings().maxCount(1));
public static final Item LAUNCH_FEATHER = new LaunchFeatherItem(new Item.Settings().maxCount(1));
Expand Down Expand Up @@ -793,6 +797,8 @@ public static void register() {
registerTater("corruptater", CORRUPTATER);

register("tater_box", TATER_BOX);
register("creative_tater_box", CREATIVE_TATER_BOX);

register("quick_armor_stand", QUICK_ARMOR_STAND);
register("game_portal_opener", GAME_PORTAL_OPENER);
register("launch_feather", LAUNCH_FEATHER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import xyz.nucleoid.extras.lobby.block.tater.TinyPotatoBlock;
import xyz.nucleoid.extras.lobby.item.TaterBoxItem;
import xyz.nucleoid.extras.lobby.item.tater.TaterBoxItem;
import xyz.nucleoid.extras.mixin.lobby.ArmorStandEntityAccessor;

import java.util.HashSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import xyz.nucleoid.extras.lobby.NEBlocks;
import xyz.nucleoid.extras.lobby.contributor.Contributor;
import xyz.nucleoid.extras.lobby.contributor.ContributorData;
import xyz.nucleoid.extras.lobby.item.TaterBoxItem;
import xyz.nucleoid.extras.lobby.item.tater.TaterBoxItem;
import xyz.nucleoid.extras.mixin.lobby.LivingEntityAccessor;
import xyz.nucleoid.extras.util.PagedGui;

Expand Down
54 changes: 51 additions & 3 deletions src/main/java/xyz/nucleoid/extras/lobby/gui/TaterBoxGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import net.minecraft.item.Items;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import xyz.nucleoid.extras.lobby.PlayerLobbyState;
import xyz.nucleoid.extras.lobby.block.tater.TinyPotatoBlock;
import xyz.nucleoid.extras.util.PagedGui;

import java.util.List;
Expand All @@ -19,14 +22,24 @@ public class TaterBoxGui extends PagedGui.FromList {
protected static final Text HIDE_UNFOUND_TEXT = Text.translatable("text.nucleoid_extras.tater_box.hide_unfound").formatted(Formatting.WHITE);
protected static final Item UNFOUND_BUTTON_ICON = Items.POISONOUS_POTATO;

protected static final Text COLLECT_ALL_TEXT = Text.translatable("text.nucleoid_extras.creative_tater_box.collect_all").formatted(Formatting.WHITE);
protected static final Item COLLECT_ALL_ICON = Items.EMERALD;

protected static final Text RESET_TEXT = Text.translatable("text.nucleoid_extras.creative_tater_box.reset").formatted(Formatting.WHITE);
protected static final Item RESET_ICON = Items.CAMPFIRE;

private final boolean creative;

protected boolean hideUnfound = true;

public TaterBoxGui(ScreenHandlerType<?> type, ServerPlayerEntity player, boolean includePlayerInventorySlots, List<GuiElementInterface> guiElementInterfaces) {
public TaterBoxGui(ScreenHandlerType<?> type, ServerPlayerEntity player, boolean includePlayerInventorySlots, List<GuiElementInterface> guiElementInterfaces, boolean creative) {
super(type, player, includePlayerInventorySlots, guiElementInterfaces, null);

this.creative = creative;
}

public static TaterBoxGui of(ServerPlayerEntity player, List<GuiElementInterface> elements) {
return new TaterBoxGui(ScreenHandlerType.GENERIC_9X6, player, false, elements);
public static TaterBoxGui of(ServerPlayerEntity player, List<GuiElementInterface> elements, boolean creative) {
return new TaterBoxGui(ScreenHandlerType.GENERIC_9X6, player, false, elements, creative);
}

public boolean shouldHideUnfound() {
Expand All @@ -47,6 +60,11 @@ public void toggleHideUnfound() {

@Override
protected DisplayElement getNavElement(int id) {
if (creative) {
if (id == 0) return TaterBoxGui.collectAllButton(this);
if (id == 8) return TaterBoxGui.resetButton(this);
}

if(id == 4) {
return TaterBoxGui.hideUnfoundButton(this);
} else return super.getNavElement(id);
Expand Down Expand Up @@ -79,6 +97,36 @@ public static DisplayElement hideUnfoundButton(TaterBoxGui gui) {
return DisplayElement.of(builder);
}

public static DisplayElement collectAllButton(TaterBoxGui gui) {
GuiElementBuilder builder = new GuiElementBuilder(COLLECT_ALL_ICON)
.setName(COLLECT_ALL_TEXT)
.hideFlags()
.setCallback(() -> {
var state = PlayerLobbyState.get(gui.getPlayer());
state.collectedTaters.addAll(TinyPotatoBlock.TATERS);

playSound(gui.player, SoundEvents.ENTITY_PLAYER_LEVELUP);
gui.close();
});

return DisplayElement.of(builder);
}

public static DisplayElement resetButton(TaterBoxGui gui) {
GuiElementBuilder builder = new GuiElementBuilder(RESET_ICON)
.setName(RESET_TEXT)
.hideFlags()
.setCallback(() -> {
var state = PlayerLobbyState.get(gui.getPlayer());
state.collectedTaters.clear();

playSound(gui.player, SoundEvents.ITEM_FIRECHARGE_USE);
gui.close();
});

return DisplayElement.of(builder);
}

public static class TaterGuiElement extends GuiElement {
protected final boolean found;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package xyz.nucleoid.extras.lobby.item.tater;

public class CreativeTaterBoxItem extends TaterBoxItem {
private static final int COLOR = 0xFF00FF;

public CreativeTaterBoxItem(Settings settings) {
super(settings);
}

@Override
protected int getEmptyColor() {
return COLOR;
}

@Override
protected boolean isCreative() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.nucleoid.extras.lobby.item;
package xyz.nucleoid.extras.lobby.item.tater;

import eu.pb4.polymer.core.api.item.PolymerItem;
import eu.pb4.polymer.core.api.utils.PolymerUtils;
Expand All @@ -11,8 +11,6 @@
import net.minecraft.item.*;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtString;
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.tag.TagKey;
Expand All @@ -25,7 +23,6 @@
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.extras.NucleoidExtras;
import xyz.nucleoid.extras.lobby.NEItems;
import xyz.nucleoid.extras.lobby.PlayerLobbyState;
import xyz.nucleoid.extras.lobby.block.tater.CubicPotatoBlock;
import xyz.nucleoid.extras.lobby.block.tater.TinyPotatoBlock;
Expand Down Expand Up @@ -113,7 +110,7 @@ private void openTaterBox(World world, PlayerEntity user, ItemStack stack, Hand
taters.add(createGuiElement(stack, user, hand, tater, tater.getName(), Registries.BLOCK.getId(tater), found));
}

var ui = TaterBoxGui.of((ServerPlayerEntity) user, taters);
var ui = TaterBoxGui.of((ServerPlayerEntity) user, taters, this.isCreative());
ui.setHideUnfound(true);
ui.setTitle(this.getTitle((ServerPlayerEntity) user));
ui.open();
Expand All @@ -138,6 +135,17 @@ private TaterBoxGui.TaterGuiElement createGuiElement(ItemStack stack, PlayerEnti
return guiElementBuilder.build();
}

/**
* {@return the color used for the empty leather helmet appearance}
*/
protected int getEmptyColor() {
return COLOR;
}

protected boolean isCreative() {
return false;
}

@Override
public Item getPolymerItem(ItemStack itemStack, @Nullable ServerPlayerEntity player) {
if (TaterBoxItem.getSelectedTater(itemStack) != null) {
Expand All @@ -156,7 +164,7 @@ public ItemStack getPolymerItemStack(ItemStack itemStack, TooltipContext context
NbtCompound skullOwner = PolymerUtils.createSkullOwner(potatoBlock.getItemTexture());
out.getOrCreateNbt().put(SkullItem.SKULL_OWNER_KEY, skullOwner);
} else {
out.getOrCreateSubNbt(DyeableItem.DISPLAY_KEY).putInt(DyeableItem.COLOR_KEY, COLOR);
out.getOrCreateSubNbt(DyeableItem.DISPLAY_KEY).putInt(DyeableItem.COLOR_KEY, this.getEmptyColor());
out.getOrCreateNbt().putBoolean("Unbreakable", true);
}

Expand Down Expand Up @@ -226,19 +234,4 @@ public static void setSelectedTater(ItemStack stack, @Nullable Identifier select
tag.putString(SELECTED_TATER_KEY, selectedTaterId.toString());
}
}

public static void addToItemGroup(ItemGroup.Entries entries) {
ItemStack fullStack = new ItemStack(NEItems.TATER_BOX);

NbtCompound nbt = fullStack.getOrCreateNbt();
NbtList taters = nbt.getList(LEGACY_TATERS_KEY, NbtElement.STRING_TYPE);

TinyPotatoBlock.TATERS.forEach((tater) -> taters.add(NbtString.of(Registries.BLOCK.getId(tater).toString())));

nbt.put(LEGACY_TATERS_KEY, taters);

fullStack.setCustomName(Text.literal("Unlocked Tater Box"));

entries.add(fullStack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import xyz.nucleoid.extras.lobby.NECriteria;
import xyz.nucleoid.extras.lobby.PlayerLobbyState;
import xyz.nucleoid.extras.lobby.block.tater.CubicPotatoBlock;
import xyz.nucleoid.extras.lobby.item.TaterBoxItem;
import xyz.nucleoid.extras.lobby.item.tater.TaterBoxItem;

@Mixin(ServerPlayerEntity.class)
public abstract class ServerPlayerEntityMixin extends PlayerEntity {
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/xyz/nucleoid/extras/util/PagedGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.screen.slot.Slot;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
Expand Down Expand Up @@ -177,8 +178,12 @@ public static DisplayElement empty() {
}
}

public static void playSound(ServerPlayerEntity player, SoundEvent sound) {
player.playSound(sound, SoundCategory.MASTER, 1, 1);
}

public static void playClickSound(ServerPlayerEntity player) {
player.playSound(SoundEvents.UI_BUTTON_CLICK.value(), SoundCategory.MASTER, 1, 1);
playSound(player, SoundEvents.UI_BUTTON_CLICK.value());
}

public static class FromList extends PagedGui {
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/data/nucleoid_extras/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@
"item.nucleoid_extras.game_portal_opener": "Game Portal Opener",
"item.nucleoid_extras.launch_feather": "Launch Feather",
"item.nucleoid_extras.tater_box": "Tater Box",
"item.nucleoid_extras.creative_tater_box": "Creative Tater Box",
"item.nucleoid_extras.quick_armor_stand": "Quick Armor Stand (Lobby only!)",

"entity.nucleoid_extras.quick_armor_stand": "Quick Armor Stand",
Expand All @@ -373,6 +374,9 @@
"text.nucleoid_extras.tater_box.show_unfound": "Show Unfound",
"text.nucleoid_extras.tater_box.updated": "Your Tater Box has been updated to new format! It will be safer than ever!",

"text.nucleoid_extras.creative_tater_box.collect_all": "Collect All",
"text.nucleoid_extras.creative_tater_box.reset": "Reset",

"text.nucleoid_extras.contributor_statue.title": "Select Contributor",

"text.nucleoid_extras.lobby_items": "Do not use this block outside of lobby!",
Expand Down

0 comments on commit 8dcd27b

Please sign in to comment.