Skip to content

Commit

Permalink
Now with Vault Buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
MrFrydae committed Mar 1, 2021
1 parent 29cb4cb commit a62079a
Show file tree
Hide file tree
Showing 16 changed files with 320 additions and 6 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ repositories {
}
}

minecraft {
accessWidener = file("src/main/resources/emcutils.accesswidener")
}

dependencies {
// To change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ loader_version=0.11.2
fabric_version=0.31.0+1.16

# Mod Properties
mod_version = 1.4.1
mod_version = 1.5.0
maven_group = dev.frydae
archives_base_name = empire-minecraft-utilities

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package dev.frydae.emcutils;

import dev.frydae.emcutils.features.VaultButtons;
import dev.frydae.emcutils.features.vaultButtons.VaultScreen;
import dev.frydae.emcutils.utils.Config;
import dev.frydae.emcutils.utils.Util;
import lombok.Getter;
import net.fabricmc.api.ModInitializer;
import net.minecraft.client.gui.screen.ingame.HandledScreens;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class EmpireMinecraftUtilities implements ModInitializer {
@Getter private static EmpireMinecraftUtilities instance;
@Getter private Logger logger;
@Getter private final String MOD_ID = "EMC_UTILS";
@Getter private final String KEYBIND_CATEGORY = "emc_utils.keybinds.category";

@Override
public void onInitialize() {
instance = this;
logger = LogManager.getLogger("EMC Utils");

HandledScreens.register(VaultButtons.GENERIC_9X7, VaultScreen::new);

Util.getOnJoinCommandQueue();

Config.getInstance().load();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.frydae.emcutils.accessors;

public interface GenericContainerScreenHandlerAccessor {
void setRows(int rows);
}
10 changes: 10 additions & 0 deletions src/main/java/dev/frydae/emcutils/features/UsableItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.item.ItemStack;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

@Environment(EnvType.CLIENT)
Expand All @@ -22,6 +23,15 @@ public void onJoinEmpireMinecraft() {
ItemTooltipCallback.EVENT.register(((itemStack, tooltipContext, list) -> {
if (Util.IS_ON_EMC) {
if (isUsableItemWithCooldown(itemStack)) {

for (Text text : list) {
if (text.getString().startsWith("Usable in: ")) {
return;
} else if (text.getString().equalsIgnoreCase("Can be used now")) {
return;
}
}

list.add(new LiteralText(""));

long untilUsable = getSecondsUntilUsable(itemStack);
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/dev/frydae/emcutils/features/VaultButtons.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.frydae.emcutils.features;

import dev.frydae.emcutils.features.vaultButtons.VaultScreenHandler;
import dev.frydae.emcutils.utils.Util;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ingame.HandledScreens;
import net.minecraft.network.packet.s2c.play.OpenScreenS2CPacket;
import net.minecraft.screen.ScreenHandlerType;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

public class VaultButtons {
public static final ScreenHandlerType<VaultScreenHandler> GENERIC_9X7 = ScreenHandlerType.register("generic_9x7", VaultScreenHandler::createGeneric9x7);

public static void handleScreenOpen(OpenScreenS2CPacket packet, CallbackInfo ci) {
if (Util.IS_ON_EMC) {
if (!packet.getName().getString().startsWith("Page: ")) {
return;
}

if (packet.getScreenHandlerType() != ScreenHandlerType.GENERIC_9X6) {
return;
}

HandledScreens.open(GENERIC_9X7, MinecraftClient.getInstance(), packet.getSyncId(), packet.getName());
ci.cancel();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package dev.frydae.emcutils.features.vaultButtons;

import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.mojang.blaze3d.systems.RenderSystem;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.gui.screen.ingame.ScreenHandlerProvider;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtHelper;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.text.TextColor;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.UUID;

@Environment(EnvType.CLIENT)
public class VaultScreen extends HandledScreen<VaultScreenHandler> implements ScreenHandlerProvider<VaultScreenHandler> {
private static final Identifier TEXTURE = new Identifier("emcutils", "textures/gui/container/generic_63.png");
private final int rows;
private final int vaultPage;
private boolean shouldCallClose = true;

private final int[] slotOffsets = {8, 26, 44, 62, 80, 98, 116, 134, 152};

public VaultScreen(VaultScreenHandler handler, PlayerInventory inventory, Text title) {
super(handler, inventory, title);
this.passEvents = false;
this.rows = 6;
this.backgroundHeight = 114 + 7 * 18;
this.playerInventoryTitleY = this.backgroundHeight - 94;

String page = title.getString().split(" ")[1];
this.vaultPage = NumberUtils.isParsable(page) ? Integer.parseInt(page) : 1;
}

/**
* @param amount the amount of pages to go back
* @return an {@link ItemStack player head} with a left arrow
*/
private ItemStack getPreviousHead(int amount) {
ItemStack stack = Items.PLAYER_HEAD.getDefaultStack();

stack.setCustomName(new LiteralText("Go back " + amount + " page" + (amount > 1 ? "s" : "")).setStyle(Style.EMPTY.withColor(TextColor.fromFormatting(Formatting.GREEN)).withItalic(false)));

GameProfile profile = new GameProfile(UUID.fromString("1f961930-4e97-47b7-a5a1-2cc5150f3764"), "");
profile.getProperties().put("textures", new Property("Value", "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNWYxMzNlOTE5MTlkYjBhY2VmZGMyNzJkNjdmZDg3YjRiZTg4ZGM0NGE5NTg5NTg4MjQ0NzRlMjFlMDZkNTNlNiJ9fX0="));

stack.getTag().put("SkullOwner", NbtHelper.fromGameProfile(new CompoundTag(), profile));

return stack;
}

private ItemStack getNextHead(int amount) {
ItemStack stack = Items.PLAYER_HEAD.getDefaultStack();

stack.setCustomName(new LiteralText("Go forward " + amount + " page" + (amount > 1 ? "s" : "")).setStyle(Style.EMPTY.withColor(TextColor.fromFormatting(Formatting.GREEN)).withItalic(false)));

GameProfile profile = new GameProfile(UUID.fromString("1f961930-4e97-47b7-a5a1-2cc5150f3764"), "");
profile.getProperties().put("textures", new Property("Value", "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZTNmYzUyMjY0ZDhhZDllNjU0ZjQxNWJlZjAxYTIzOTQ3ZWRiY2NjY2Y2NDkzNzMyODliZWE0ZDE0OTU0MWY3MCJ9fX0="));

stack.getTag().put("SkullOwner", NbtHelper.fromGameProfile(new CompoundTag(), profile));

return stack;
}

@Override
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
this.renderBackground(matrices);
super.render(matrices, mouseX, mouseY, delta);

for (int i = 4; i > 0; i--) {
if (vaultPage > i) {
drawButton(matrices, getPreviousHead(i), mouseX, mouseY, slotOffsets[4 - i], i + "");
}
}

ItemStack chest = Items.CHEST.getDefaultStack();
chest.setCustomName(new LiteralText("View your vaults").setStyle(Style.EMPTY.withColor(TextColor.fromFormatting(Formatting.GREEN)).withItalic(false)));
drawButton(matrices, chest, mouseX, mouseY, slotOffsets[4], "");

for (int i = 1; i <= 4; i++) {
drawButton(matrices, getNextHead(i), mouseX, mouseY, slotOffsets[4 + i], i + "");
}

this.drawMouseoverTooltip(matrices, mouseX, mouseY);
}

private void drawButton(MatrixStack matrices, ItemStack button, int mouseX, int mouseY, int buttonX, String amountText) {
int midWidth = (this.width - this.backgroundWidth) / 2;
int midHeight = (this.height - this.backgroundHeight) / 2;

this.drawItem(button, midWidth + buttonX, midHeight + 125, amountText);

if (mouseX >= midWidth + buttonX && mouseX <= midWidth + buttonX + 15) {
if (mouseY >= midHeight + 126 && mouseY <= midHeight + 141) {
matrices.translate(0, 0, 225);
this.fillGradient(matrices, midWidth + buttonX, midHeight + 125, midWidth + buttonX + 16, midHeight + 125 + 16, 0x80ffffff, 0x80ffffff);
this.renderTooltip(matrices, button, mouseX, mouseY);
matrices.translate(0, 0, -225);
}
}
}

protected void drawBackground(MatrixStack matrices, float delta, int mouseX, int mouseY) {
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
this.client.getTextureManager().bindTexture(TEXTURE);
int i = (this.width - this.backgroundWidth) / 2;
int j = (this.height - this.backgroundHeight) / 2;
this.drawTexture(matrices, i, j, 0, 0, this.backgroundWidth, (rows + 1) * 18 + 17);
this.drawTexture(matrices, i, j + this.rows * 18 + 17, 0, 126, this.backgroundWidth, 128);
}

@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
for (int i = 4; i > 0; i--) {
handleClick(slotOffsets[4 - i], mouseX, mouseY, "/vault " + (vaultPage - i));
}

handleClick(slotOffsets[4], mouseX, mouseY, "/vaults");

for (int i = 1; i <= 4; i++) {
handleClick(slotOffsets[4 + i], mouseX, mouseY, "/vault " + (vaultPage + i));
}

return super.mouseClicked(mouseX, mouseY, button);
}

private void handleClick(int buttonX, double mouseX, double mouseY, String command) {
int midWidth = (this.width - this.backgroundWidth) / 2;
int midHeight = (this.height - this.backgroundHeight) / 2;

if (mouseX >= midWidth + buttonX && mouseX < midWidth + buttonX + 16) {
if (mouseY >= midHeight + 126 && mouseY <= midHeight + 141) {
this.shouldCallClose = false;
MinecraftClient.getInstance().player.playSound(SoundEvents.BLOCK_NOTE_BLOCK_SNARE, 4F, 1F);
MinecraftClient.getInstance().player.sendChatMessage(command);
}
}
}

@Override
public void onClose() {
if (shouldCallClose) {
super.onClose();
} else {
shouldCallClose = true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package dev.frydae.emcutils.features.vaultButtons;

import dev.frydae.emcutils.features.VaultButtons;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.screen.slot.Slot;

public class VaultScreenHandler extends ScreenHandler {
private final Inventory inventory;
private final int rows;

public static VaultScreenHandler createGeneric9x7(int syncId, PlayerInventory playerInventory) {
return new VaultScreenHandler(VaultButtons.GENERIC_9X7, syncId, playerInventory, 7);
}

private VaultScreenHandler(ScreenHandlerType<?> type, int syncId, PlayerInventory playerInventory, int rows) {
this(type, syncId, playerInventory, new SimpleInventory(9 * rows), rows);
}

public VaultScreenHandler(ScreenHandlerType<?> type, int syncId, PlayerInventory playerInventory, Inventory inventory, int rows) {
super(type, syncId);
checkSize(inventory, rows * 9);
this.inventory = inventory;
this.rows = rows;
inventory.onOpen(playerInventory.player);
int i = (6 - 3) * 18;

for(int row = 0; row < 6; ++row) {
for(int column = 0; column < 9; ++column) {
this.addSlot(new Slot(inventory, column + row * 9, 8 + column * 18, 18 + row * 18));
}
}

for(int row = 0; row < 3; ++row) {
for(int column = 0; column < 9; ++column) {
this.addSlot(new Slot(playerInventory, column + row * 9 + 9, 8 + column * 18, 103 + row * 18 + i));
}
}

for(int row = 0; row < 9; ++row) {
this.addSlot(new Slot(playerInventory, row, 8 + row * 18, 161 + i));
}

}

public boolean canUse(PlayerEntity player) {
return this.inventory.canPlayerUse(player);
}

public ItemStack transferSlot(PlayerEntity player, int index) {
ItemStack itemStack = ItemStack.EMPTY;
Slot slot = (Slot)this.slots.get(index);
if (slot != null && slot.hasStack()) {
ItemStack itemStack2 = slot.getStack();
itemStack = itemStack2.copy();
if (index < this.rows * 9) {
if (!this.insertItem(itemStack2, this.rows * 9, this.slots.size(), true)) {
return ItemStack.EMPTY;
}
} else if (!this.insertItem(itemStack2, 0, this.rows * 9, false)) {
return ItemStack.EMPTY;
}

if (itemStack2.isEmpty()) {
slot.setStack(ItemStack.EMPTY);
} else {
slot.markDirty();
}
}

return itemStack;
}

public void close(PlayerEntity player) {
super.close(player);
this.inventory.onClose(player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected ChatScreenMixin(Text title) {
super(title);
}

@Inject(at = @At("RETURN"), method = "render")
@Inject(at = @At("HEAD"), method = "render")
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta, CallbackInfo info) {
ChatChannels.handleChatScreenRender(this, matrices, info);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import dev.frydae.emcutils.callbacks.ChatCallback;
import dev.frydae.emcutils.features.ChatChannels;
import dev.frydae.emcutils.features.VaultButtons;
import dev.frydae.emcutils.loader.EmpireMinecraftInitializer;
import dev.frydae.emcutils.utils.Util;
import net.fabricmc.loader.entrypoint.minecraft.hooks.EntrypointUtils;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.network.packet.s2c.play.GameJoinS2CPacket;
import net.minecraft.network.packet.s2c.play.GameMessageS2CPacket;
import net.minecraft.network.packet.s2c.play.OpenScreenS2CPacket;
import net.minecraft.util.ActionResult;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand Down Expand Up @@ -42,4 +44,9 @@ public void onGameJoin(GameJoinS2CPacket packet, CallbackInfo info) {
Util.executeJoinCommands();
}
}

@Inject(at = @At("INVOKE"), method = "onOpenScreen", cancellable = true)
public void onOpenScreen(OpenScreenS2CPacket packet, CallbackInfo ci) {
VaultButtons.handleScreenOpen(packet, ci);
}
}
2 changes: 1 addition & 1 deletion src/main/java/dev/frydae/emcutils/utils/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static void setCurrentServer(String name) {
}

public static List<PlayerListEntry> getPlayerListEntries() {
return Lists.newArrayList(((PlayerListHudAccessor) MinecraftClient.getInstance().inGameHud.getPlayerListWidget()).getEntryOrdering().sortedCopy(MinecraftClient.getInstance().getNetworkHandler().getPlayerList()));
return Lists.newArrayList(((PlayerListHudAccessor) MinecraftClient.getInstance().inGameHud.getPlayerListHud()).getEntryOrdering().sortedCopy(MinecraftClient.getInstance().getNetworkHandler().getPlayerList()));
}

public static Queue<String> getOnJoinCommandQueue() {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit a62079a

Please sign in to comment.