Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gigaherz.inventoryspam;
- import net.minecraft.item.*;
- import net.minecraft.entity.player.*;
- import net.minecraftforge.common.*;
- import com.google.common.collect.*;
- import net.minecraft.client.*;
- import net.minecraftforge.client.event.*;
- import gigaherz.inventoryspam.config.*;
- import net.minecraft.client.renderer.*;
- import net.minecraft.client.gui.*;
- import net.minecraftforge.fml.common.eventhandler.*;
- import java.util.function.*;
- import net.minecraft.util.text.*;
- import net.minecraftforge.fml.common.gameevent.*;
- import org.apache.commons.lang3.tuple.*;
- import java.util.*;
- import net.minecraft.inventory.*;
- public class ScrollingOverlay extends GuiScreen
- {
- private static final int TTL = 240;
- private static final int FADE = 40;
- private int hard_limit;
- private RenderItem renderItem;
- private int dim;
- private int dimLoadTicks;
- private ItemStack[] previous;
- private EntityPlayer playerEntity;
- private ItemStack previousInCursor;
- private final List<ChangeInfo> changeEntries;
- public static void init() {
- MinecraftForge.EVENT_BUS.register((Object)new ScrollingOverlay());
- }
- private ScrollingOverlay() {
- super();
- this.previousInCursor = ItemStack.EMPTY;
- this.changeEntries = (List<ChangeInfo>)Lists.newArrayList();
- this.renderItem = Minecraft.getMinecraft().getRenderItem();
- }
- @SubscribeEvent
- public void renderOverlay(final RenderGameOverlayEvent.Post event) {
- if (!Config.showItemAdditions && !Config.showItemRemovals) {
- return;
- }
- if (event.getType() != RenderGameOverlayEvent.ElementType.CHAT) {
- return;
- }
- final ScaledResolution resolution = event.getResolution();
- int width = resolution.getScaledWidth();
- int height = resolution.getScaledHeight();
- width /= (int)Config.drawScale;
- height /= (int)Config.drawScale;
- final FontRenderer font = Minecraft.getMinecraft().fontRenderer;
- final int iconSize = (int)(16.0 * Config.iconScale);
- final int rightMargin = Config.drawIcon ? (2 + iconSize) : 0;
- final int topMargin1 = 2 + (Config.drawIcon ? Math.max(0, (iconSize - font.FONT_HEIGHT) / 2) : 0);
- final int topMargin2 = 1 + Math.max(0, -(iconSize - font.FONT_HEIGHT) / 2);
- int lineHeight = font.FONT_HEIGHT;
- if (Config.drawIcon) {
- lineHeight = Math.max(2 + iconSize, lineHeight);
- }
- this.hard_limit = height / lineHeight;
- final List<Triple<ChangeInfo, String[], Integer>> computedStrings = (List<Triple<ChangeInfo, String[], Integer>>)Lists.newArrayList();
- int rectWidth;
- final int number;
- synchronized (this.changeEntries) {
- if (this.changeEntries.size() == 0) {
- return;
- }
- rectWidth = this.computeStrings(computedStrings, font);
- number = computedStrings.size();
- if (number == 0) {
- return;
- }
- }
- GlStateManager.pushMatrix();
- GlStateManager.scale(Config.drawScale, Config.drawScale, 1.0);
- rectWidth += rightMargin;
- final int rectHeight = lineHeight * number;
- int x = 0;
- int y = 0;
- int align = 0;
- switch (Config.drawPosition) {
- default: {
- x = width - 2 - rectWidth - Config.drawOffsetHorizontal;
- y = height - 2 - rectHeight - Config.drawOffsetVertical;
- align = 1;
- break;
- }
- case Bottom: {
- x = (width - rectWidth) / 2 - 2 + Config.drawOffsetHorizontal;
- y = height - 2 - rectHeight - Config.drawOffsetVertical;
- align = 0;
- break;
- }
- case BottomLeft: {
- x = 2 + Config.drawOffsetHorizontal;
- y = height - 2 - rectHeight - Config.drawOffsetVertical;
- align = -1;
- break;
- }
- case Left: {
- x = 2 + Config.drawOffsetHorizontal;
- y = (height - rectHeight) / 2 - 2 + Config.drawOffsetVertical;
- align = -1;
- break;
- }
- case TopLeft: {
- x = 2 + Config.drawOffsetHorizontal;
- y = 2 + Config.drawOffsetVertical;
- align = -1;
- break;
- }
- case Top: {
- x = (width - rectWidth) / 2 - 2 + Config.drawOffsetHorizontal;
- y = 2 + Config.drawOffsetVertical;
- align = 0;
- break;
- }
- case TopRight: {
- x = width - 2 - rectWidth - Config.drawOffsetHorizontal;
- y = 2 + Config.drawOffsetVertical;
- align = 1;
- break;
- }
- case Right: {
- x = width - 2 - rectWidth - Config.drawOffsetHorizontal;
- y = (height - rectHeight) / 2 - 2 + Config.drawOffsetVertical;
- align = 1;
- break;
- }
- case Center: {
- x = (width - rectWidth) / 2 - 2 + Config.drawOffsetHorizontal;
- y = (height - rectHeight) / 2 - 2 + Config.drawOffsetVertical;
- align = 0;
- break;
- }
- }
- drawRect(x - 2, y - 2, x + rectWidth + 4, y + rectHeight + 4, Integer.MIN_VALUE);
- for (int i = 0; i < computedStrings.size(); ++i) {
- final Triple<ChangeInfo, String[], Integer> e = computedStrings.get(i);
- final ChangeInfo change = e.getLeft();
- final String[] strings = e.getMiddle();
- final int fade = e.getRight();
- int w = 0;
- final int[] widths = new int[strings.length];
- for (int n = 0; n < strings.length; ++n) {
- final String str = strings[n];
- final int[] array = widths;
- final int n3 = n;
- final int stringWidth = font.getStringWidth(str);
- array[n3] = stringWidth;
- final int wn = stringWidth;
- w += wn;
- }
- final int forcedFade = (Config.fadeLimit > 0) ? (fade * 255 / (Config.fadeLimit + 2)) : 255;
- final int ttlFade = change.ttl * 255 / 40;
- final int alpha = Math.min(255, Math.min(forcedFade, ttlFade));
- final int color = alpha << 24 | ((change.mode == ChangeMode.Obtained) ? 8388479 : 16736095);
- int leftMargin = 0;
- switch (align) {
- case -1: {
- leftMargin = 2;
- break;
- }
- case 0: {
- leftMargin = (rectWidth - w - rightMargin) / 2;
- break;
- }
- case 1: {
- leftMargin = rectWidth - w - rightMargin;
- break;
- }
- }
- GlStateManager.enableBlend();
- int wAcc = 0;
- for (int n2 = 0; n2 < strings.length; ++n2) {
- font.drawStringWithShadow(strings[n2], (float)(x + leftMargin + wAcc), (float)(y + topMargin1), color);
- wAcc += widths[n2];
- }
- if (Config.drawIcon) {
- GlStateManager.pushMatrix();
- GlStateManager.translate((float)(x + 2 + w + leftMargin), (float)(y + topMargin2), 0.0f);
- GlStateManager.scale(Config.iconScale, Config.iconScale, 1.0);
- RenderHelper.enableGUIStandardItemLighting();
- this.renderItem.renderItemAndEffectIntoGUI(change.item.stack, 0, 0);
- this.renderItem.renderItemOverlayIntoGUI(font, change.item.stack, 0, 0, (String)null);
- RenderHelper.disableStandardItemLighting();
- GlStateManager.popMatrix();
- }
- y += lineHeight;
- }
- GlStateManager.popMatrix();
- }
- private int computeStrings(final List<Triple<ChangeInfo, String[], Integer>> computedStrings, final FontRenderer font) {
- int rectWidth = 0;
- final int itemsToShow = Math.min(Math.min(this.hard_limit, Config.softLimit + Config.fadeLimit), this.changeEntries.size());
- final int offset = Math.max(0, this.changeEntries.size() - itemsToShow);
- final int fadeOffset = this.changeEntries.size() - Config.softLimit - Config.fadeLimit;
- for (int i = offset; i < this.changeEntries.size(); ++i) {
- final ChangeInfo change = this.changeEntries.get(i);
- final String[] parts = this.getChangeStrings(change);
- final int w = Arrays.stream(parts).mapToInt(font::func_78256_a).sum();
- rectWidth = Math.max(rectWidth, w);
- computedStrings.add(Triple.of(change, parts, Math.min(Config.fadeLimit + 2, 1 + i - fadeOffset)));
- }
- return rectWidth;
- }
- private String[] getChangeStrings(final ChangeInfo change) {
- final String mode = (change.mode == ChangeMode.Obtained) ? "+" : "-";
- final String s1 = String.format("%s%d", mode, change.count);
- if (Config.drawName) {
- final String name = change.item.stack.getDisplayName();
- final String italics = change.item.stack.hasDisplayName() ? ("" + TextFormatting.ITALIC) : "";
- final String s2 = String.format("%s%s", italics, name);
- return new String[] { s1, " ", s2 };
- }
- return new String[] { s1 };
- }
- @SubscribeEvent
- public void clientTick(final TickEvent.ClientTickEvent event) {
- if (event.phase != TickEvent.Phase.END) {
- return;
- }
- if (!Config.showItemAdditions && !Config.showItemRemovals) {
- return;
- }
- final EntityPlayer player = (EntityPlayer)Minecraft.getMinecraft().player;
- if (player == null) {
- return;
- }
- if (player != this.playerEntity) {
- if (player.inventoryContainer != null) {
- player.inventoryContainer = (Container)new ContainerWrapper((ContainerPlayer)player.inventoryContainer, player, () -> {
- this.previous = null;
- this.dimLoadTicks = 0;
- return;
- });
- this.playerEntity = player;
- }
- this.previous = null;
- }
- if (player.dimension != this.dim) {
- this.previous = null;
- this.dimLoadTicks = 200;
- this.dim = player.dimension;
- }
- if (this.dimLoadTicks > 0) {
- this.previous = null;
- --this.dimLoadTicks;
- return;
- }
- synchronized (this.changeEntries) {
- this.changeEntries.forEach(e -> --e.ttl);
- while (this.changeEntries.size() > this.hard_limit) {
- this.changeEntries.remove(0);
- }
- this.changeEntries.removeIf(e -> e.ttl <= 0 || e.count == 0);
- }
- if (this.previous == null || this.previous.length != player.inventory.getSizeInventory()) {
- this.previous = new ItemStack[player.inventory.getSizeInventory()];
- for (int i = 0; i < player.inventory.getSizeInventory(); ++i) {
- this.previous[i] = safeCopy(player.inventory.getStackInSlot(i));
- }
- this.previousInCursor = player.inventory.getItemStack();
- return;
- }
- final List<Pair<ItemStack, ItemStack>> changes = (List<Pair<ItemStack, ItemStack>>)Lists.newArrayList();
- for (int j = 0; j < player.inventory.getSizeInventory(); ++j) {
- final ItemStack stack = player.inventory.getStackInSlot(j);
- final ItemStack old = this.previous[j];
- if (this.isChangeMeaningful(old, stack)) {
- changes.add(Pair.of(old, stack));
- }
- this.previous[j] = stack.copy();
- }
- final ItemStack stackInCursor = player.inventory.getItemStack();
- if (this.isChangeMeaningful(stackInCursor, this.previousInCursor)) {
- changes.add(Pair.of(this.previousInCursor, stackInCursor));
- }
- this.previousInCursor = stackInCursor.copy();
- if (changes.size() == 0) {
- return;
- }
- final List<ChangeInfo> changeList = (List<ChangeInfo>)Lists.newArrayList();
- final ItemStack left;
- final boolean leftEmpty;
- final ItemStack right;
- final boolean rightEmpty;
- int difference;
- final List<ChangeInfo> list;
- changes.forEach(change -> {
- left = change.getLeft();
- leftEmpty = (left.getCount() <= 0);
- right = (ItemStack)change.getRight();
- rightEmpty = (right.getCount() <= 0);
- if (areSameishItem(left, right)) {
- if (!this.isBlacklisted(left)) {
- difference = right.getCount() - left.getCount();
- if (difference > 0) {
- this.obtainedItem(list, left, difference);
- }
- else if (difference < 0) {
- this.lostItem(list, left, -difference);
- }
- }
- }
- else {
- if (!leftEmpty && !this.isBlacklisted(left)) {
- this.lostItem(list, left, left.getCount());
- }
- if (!rightEmpty && !this.isBlacklisted(right)) {
- this.obtainedItem(list, right, right.getCount());
- }
- }
- return;
- });
- changeList.removeIf(e -> e.count == 0);
- if (changeList.size() > 0) {
- synchronized (this.changeEntries) {
- for (final ChangeInfo info : changeList) {
- if (info.count == 0) {
- continue;
- }
- this.accumulate(this.changeEntries, info.item.stack, info.mode, info.count, false);
- }
- }
- }
- }
- private boolean isBlacklisted(final ItemStack left) {
- return Config.ignoreItems.contains(left.getItem().getRegistryName().toString());
- }
- private boolean isChangeMeaningful(final ItemStack a, final ItemStack b) {
- return a.getCount() != b.getCount() || (a != b && (!isStackEmpty(a) || !isStackEmpty(b)) && (a.getItem() != b.getItem() || !Config.ignoreSubitemChanges.contains(a.getItem().getRegistryName().toString())) && !ItemStack.areItemsEqualIgnoreDurability(a, b));
- }
- private static boolean areLooselyTheSame(final ItemStack a, final ItemStack b) {
- return a == b || (isStackEmpty(a) && isStackEmpty(b)) || ItemStack.areItemsEqualIgnoreDurability(a, b);
- }
- private static boolean areSameishItem(final ItemStack a, final ItemStack b) {
- return a == b || (isStackEmpty(a) && isStackEmpty(b)) || (ItemStack.areItemsEqual(a, b) && ItemStack.areItemStackTagsEqual(a, b));
- }
- private static boolean isStackEmpty(final ItemStack stack) {
- return stack.getCount() <= 0;
- }
- private static ItemStack safeCopy(final ItemStack stack) {
- return stack.copy();
- }
- private void obtainedItem(final List<ChangeInfo> changeList, final ItemStack item, final int added) {
- if (added <= 0 || !Config.showItemAdditions) {
- return;
- }
- this.accumulate(changeList, item, ChangeMode.Obtained, added, true);
- }
- private void lostItem(final List<ChangeInfo> changeList, final ItemStack item, final int removed) {
- if (removed <= 0 || !Config.showItemRemovals) {
- return;
- }
- this.accumulate(changeList, item, ChangeMode.Lost, removed, true);
- }
- private void accumulate(final List<ChangeInfo> changeList, final ItemStack stack, final ChangeMode mode, int count, final boolean isLocal) {
- if (stack.getCount() <= 0) {
- return;
- }
- final ComparableItem name = new ComparableItem(stack);
- ChangeInfo info = isLocal ? changeList.stream().filter(e -> e.item.equals(name)).findFirst().orElse(null) : changeList.stream().filter(e -> e.item.equals(name) && e.mode == mode).findFirst().orElse(null);
- if (info == null) {
- info = new ChangeInfo(name, mode, count, 240);
- changeList.add(info);
- return;
- }
- if (info.mode != mode) {
- count = -count;
- }
- final ChangeInfo changeInfo = info;
- changeInfo.count += count;
- info.ttl = 240;
- if (info.count < 0) {
- info.count = -info.count;
- info.mode = ((info.mode == ChangeMode.Lost) ? ChangeMode.Obtained : ChangeMode.Lost);
- }
- }
- private static /* synthetic */ boolean lambda$accumulate$6(final ComparableItem name, final ChangeMode mode, final ChangeInfo e) {
- return e.item.equals(name) && e.mode == mode;
- }
- private static /* synthetic */ boolean lambda$accumulate$5(final ComparableItem name, final ChangeInfo e) {
- return e.item.equals(name);
- }
- private static /* synthetic */ boolean lambda$clientTick$4(final ChangeInfo e) {
- return e.count == 0;
- }
- private /* synthetic */ void lambda$clientTick$3(final List changeList, final Pair change) {
- final ItemStack left = change.getLeft();
- final boolean leftEmpty = left.getCount() <= 0;
- final ItemStack right = (ItemStack)change.getRight();
- final boolean rightEmpty = right.getCount() <= 0;
- if (areSameishItem(left, right)) {
- if (!this.isBlacklisted(left)) {
- final int difference = right.getCount() - left.getCount();
- if (difference > 0) {
- this.obtainedItem(changeList, left, difference);
- }
- else if (difference < 0) {
- this.lostItem(changeList, left, -difference);
- }
- }
- }
- else {
- if (!leftEmpty && !this.isBlacklisted(left)) {
- this.lostItem(changeList, left, left.getCount());
- }
- if (!rightEmpty && !this.isBlacklisted(right)) {
- this.obtainedItem(changeList, right, right.getCount());
- }
- }
- }
- private static /* synthetic */ boolean lambda$clientTick$2(final ChangeInfo e) {
- return e.ttl <= 0 || e.count == 0;
- }
- private static /* synthetic */ void lambda$clientTick$1(final ChangeInfo e) {
- --e.ttl;
- }
- private /* synthetic */ void lambda$clientTick$0() {
- this.previous = null;
- this.dimLoadTicks = 0;
- }
- static /* synthetic */ boolean access$000(final ItemStack x0, final ItemStack x1) {
- return areSameishItem(x0, x1);
- }
- private static class ChangeInfo
- {
- final ComparableItem item;
- ChangeMode mode;
- int count;
- int ttl;
- ChangeInfo(final ComparableItem item, final ChangeMode mode, final int count, final int ttl) {
- super();
- this.item = item;
- this.mode = mode;
- this.count = count;
- this.ttl = ttl;
- }
- }
- private enum ChangeMode
- {
- Obtained,
- Lost;
- private static final /* synthetic */ ChangeMode[] $VALUES;
- public static ChangeMode[] values() {
- return ChangeMode.$VALUES.clone();
- }
- public static ChangeMode valueOf(final String name) {
- return Enum.valueOf(ChangeMode.class, name);
- }
- static {
- $VALUES = new ChangeMode[] { ChangeMode.Obtained, ChangeMode.Lost };
- }
- }
- private static class ComparableItem
- {
- ItemStack stack;
- ComparableItem(final ItemStack stack) {
- super();
- (this.stack = stack.copy()).setCount(1);
- }
- @Override
- public boolean equals(final Object obj) {
- if (!(obj instanceof ComparableItem)) {
- return false;
- }
- final ItemStack stack = ((ComparableItem)obj).stack;
- return areSameishItem(stack, this.stack);
- }
- @Override
- public int hashCode() {
- return this.stack.getItem().hashCode() * 31 ^ this.stack.getMetadata();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement