Skip to content

Commit

Permalink
ViaVersion Support
Browse files Browse the repository at this point in the history
Remove GuiServerSpoof
  • Loading branch information
opZywl committed Nov 21, 2023
1 parent 8f75931 commit 737fdca
Show file tree
Hide file tree
Showing 34 changed files with 1,536 additions and 102 deletions.
25 changes: 25 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ plugins {
repositories {
mavenLocal()
mavenCentral()
maven { url = "https://maven.minecraftforge.net/" }
maven { url = "https://repo.spongepowered.org/repository/maven-public/" }
maven { url = "https://jitpack.io/" }
maven { url = "https://repo.viaversion.com" }
}

version = "5.4.0"
Expand All @@ -39,8 +41,21 @@ minecraft {
configurations {
include
implementation.extendsFrom(include)

external
compile.extendsFrom(external)

runtimeOnly.canBeResolved = true
}

def viaLibs = [
"com.viaversion:viaversion:${project.viaversion_version}",
"com.viaversion:viabackwards:${project.viabackwards_version}",
"com.viaversion:viarewind-universal:${project.viarewind_version}",
"org.yaml:snakeyaml:${project.snake_yml_version}",
"org.slf4j:slf4j-api:${project.slf4j_version}"
]

dependencies {
implementation 'org.projectlombok:lombok:1.18.26'
include("org.spongepowered:mixin:0.7.11-SNAPSHOT") {
Expand All @@ -53,6 +68,16 @@ dependencies {
exclude module: "slf4j-api"
}

for (final def via in viaLibs) {
include(via)
}

include('com.github.half-cambodian-hacker-man:Koffee:d8cee73') {
exclude module: 'asm-commons'
exclude module: 'asm-tree'
exclude module: 'asm'
}

annotationProcessor("org.spongepowered:mixin:0.7.11-SNAPSHOT")

include "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
Expand Down
9 changes: 9 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ org.gradle.jvmargs=-Xmx8g
kotlin_version = 1.6.10
detekt_version = 1.19.0
forgeVersion=1.8.9-11.15.1.2318-1.8.9

vialoader_version=2.2.11-SNAPSHOT
viaversion_version=4.9.0-23w45a-SNAPSHOT
viabackwards_version=4.9.0-23w45a-SNAPSHOT
viarewind_version=3.0.4-SNAPSHOT

forgegradle_version = f2c5bb338e
mixingradle_version = ae2a80e
jcef_version = 100.0.14.2

snake_yml_version=2.2
slf4j_version=2.0.9
Binary file added libs/ViaBackwards-4.4.1.jar
Binary file not shown.
Binary file added libs/ViaRewind-2.0.3-AnimationFixed.jar
Binary file not shown.
Binary file added libs/ViaSnakeYaml-1.30.jar
Binary file not shown.
Binary file added libs/ViaVersion-4.4.1.jar
Binary file not shown.
136 changes: 136 additions & 0 deletions src/main/java/cc/paimonmc/viamcp/ViaMCP.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package cc.paimonmc.viamcp;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.viaversion.viaversion.ViaManagerImpl;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.data.MappingDataLoader;
import io.netty.channel.EventLoop;
import io.netty.channel.local.LocalEventLoopGroup;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.ScaledResolution;
import org.apache.logging.log4j.LogManager;
import cc.paimonmc.viamcp.gui.AsyncVersionSlider;
import cc.paimonmc.viamcp.loader.MCPBackwardsLoader;
import cc.paimonmc.viamcp.loader.MCPRewindLoader;
import cc.paimonmc.viamcp.loader.MCPViaLoader;
import cc.paimonmc.viamcp.platform.MCPViaInjector;
import cc.paimonmc.viamcp.platform.MCPViaPlatform;
import cc.paimonmc.viamcp.utils.JLoggerToLog4j;
import java.io.File;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.logging.Logger;
import net.ccbluex.liquidbounce.utils.MinecraftInstance;

import static net.ccbluex.liquidbounce.utils.MinecraftInstance.mc;

public class ViaMCP {
public final static int PROTOCOL_VERSION = 47;
private static final ViaMCP instance = new ViaMCP();

public static ViaMCP getInstance() {
return instance;
}

private final Logger jLogger = new JLoggerToLog4j(LogManager.getLogger("ViaMCP"));
private final CompletableFuture<Void> INIT_FUTURE = new CompletableFuture<>();

private ExecutorService ASYNC_EXEC;
private EventLoop EVENT_LOOP;

private File file;
private int version;
private String lastServer;

public static boolean hidden;

public static void staticInit() {
getInstance().start();
}

/**
* Version Slider that works Asynchronously with the Version GUI
* Please initialize this before usage with initAsyncSlider() or initAsyncSlider(x, y, width (min. 110), height)
*/
public AsyncVersionSlider asyncSlider;

public void start() {
try {
final ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("ViaMCP-%d").build();
ASYNC_EXEC = Executors.newFixedThreadPool(8, factory);

EVENT_LOOP = new LocalEventLoopGroup(1, factory).next();
EVENT_LOOP.submit(INIT_FUTURE::join);

setVersion(PROTOCOL_VERSION);
this.file = new File("ViaMCP");
if (this.file.mkdir()) {
this.getjLogger().info("Creating ViaMCP Folder");
}

Via.init(ViaManagerImpl.builder().injector(new MCPViaInjector()).loader(new MCPViaLoader()).platform(new MCPViaPlatform(file)).build());

MappingDataLoader.enableMappingsCache();
((ViaManagerImpl) Via.getManager()).init();

new MCPBackwardsLoader(file);
new MCPRewindLoader(file);

INIT_FUTURE.complete(null);

//ViaMCP.getInstance().initAsyncSlider();
} catch (final Exception exception) {
exception.printStackTrace();
}
}

public Logger getjLogger() {
return jLogger;
}

public CompletableFuture<Void> getInitFuture() {
return INIT_FUTURE;
}

public ExecutorService getAsyncExecutor() {
return ASYNC_EXEC;
}

public EventLoop getEventLoop() {
return EVENT_LOOP;
}

public File getFile() {
return file;
}

public String getLastServer() {
return lastServer;
}

public int getVersion() {
return version;
}

public void setVersion(final int version) {
this.version = version;
}

public void setFile(final File file) {
this.file = file;
}

public void setLastServer(final String lastServer) {
this.lastServer = lastServer;
}

public static boolean isHidden() {
return hidden;
}

public static void setHidden(boolean hidden) {
ViaMCP.hidden = hidden;
}
}
90 changes: 90 additions & 0 deletions src/main/java/cc/paimonmc/viamcp/gui/AsyncVersionSlider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package cc.paimonmc.viamcp.gui;

import cc.paimonmc.viamcp.ViaMCP;
import cc.paimonmc.viamcp.protocols.ProtocolCollection;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.MathHelper;

import java.util.Arrays;
import java.util.Collections;

public class AsyncVersionSlider extends GuiButton {
private float dragValue = (float) (ProtocolCollection.values().length - Arrays.asList(ProtocolCollection.values()).indexOf(ProtocolCollection.getProtocolCollectionById(ViaMCP.PROTOCOL_VERSION))) / ProtocolCollection.values().length;

private final ProtocolCollection[] values;
private float sliderValue;
public boolean dragging;

public AsyncVersionSlider(final int buttonId, final int x, final int y, final int widthIn, final int heightIn) {
super(buttonId, x, y, Math.max(widthIn, 110), heightIn, "");
this.values = ProtocolCollection.values();
Collections.reverse(Arrays.asList(values));
this.sliderValue = dragValue;
this.displayString = values[(int) (this.sliderValue * (values.length - 1))].getVersion().getName();
}

public void drawButton(final Minecraft mc, final int mouseX, final int mouseY) {
super.drawButton(mc, mouseX, mouseY);
}

/**
* Returns 0 if the button is disabled, 1 if the mouse is NOT hovering over this button and 2 if it IS hovering over
* this button.
*/
public int getHoverState(final boolean mouseOver) {
return 0;
}

/**
* Fired when the mouse button is dragged. Equivalent of MouseListener.mouseDragged(MouseEvent e).
*/
public void mouseDragged(final Minecraft mc, final int mouseX, final int mouseY) {
if (this.visible) {
if (this.dragging) {
this.sliderValue = (float) (mouseX - (this.xPosition + 4)) / (float) (this.width - 8);
this.sliderValue = MathHelper.clamp_float(this.sliderValue, 0.0F, 1.0F);
this.dragValue = sliderValue;
this.displayString = values[(int) (this.sliderValue * (values.length - 1))].getVersion().getName();
ViaMCP.getInstance().setVersion(values[(int) (this.sliderValue * (values.length - 1))].getVersion().getVersion());
}

mc.getTextureManager().bindTexture(buttonTextures);
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
this.drawTexturedModalRect(this.xPosition + (int) (this.sliderValue * (float) (this.width - 8)), this.yPosition, 0, 66, 4, 20);
this.drawTexturedModalRect(this.xPosition + (int) (this.sliderValue * (float) (this.width - 8)) + 4, this.yPosition, 196, 66, 4, 20);
}
}

/**
* Returns true if the mouse has been pressed on this control. Equivalent of MouseListener.mousePressed(MouseEvent
* e).
*/
public boolean mousePressed(final Minecraft mc, final int mouseX, final int mouseY) {
if (super.mousePressed(mc, mouseX, mouseY)) {
this.sliderValue = (float) (mouseX - (this.xPosition + 4)) / (float) (this.width - 8);
this.sliderValue = MathHelper.clamp_float(this.sliderValue, 0.0F, 1.0F);
this.dragValue = sliderValue;
this.displayString = values[(int) (this.sliderValue * (values.length - 1))].getVersion().getName();
ViaMCP.getInstance().setVersion(values[(int) (this.sliderValue * (values.length - 1))].getVersion().getVersion());
this.dragging = true;
return true;
} else {
return false;
}
}

/**
* Fired when the mouse button is released. Equivalent of MouseListener.mouseReleased(MouseEvent e).
*/
public void mouseReleased(final int mouseX, final int mouseY) {
this.dragging = false;
}

public void setVersion(final int protocol) {
this.dragValue = (float) (ProtocolCollection.values().length - Arrays.asList(ProtocolCollection.values()).indexOf(ProtocolCollection.getProtocolCollectionById(protocol))) / ProtocolCollection.values().length;
this.sliderValue = this.dragValue;
this.displayString = values[(int) (this.sliderValue * (values.length - 1))].getVersion().getName();
}
}
105 changes: 105 additions & 0 deletions src/main/java/cc/paimonmc/viamcp/gui/GuiProtocolSelector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package cc.paimonmc.viamcp.gui;

import cc.paimonmc.viamcp.ViaMCP;
import cc.paimonmc.viamcp.protocols.ProtocolCollection;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiSlot;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.EnumChatFormatting;

import java.io.IOException;

public class GuiProtocolSelector extends GuiScreen {
private final GuiScreen parent;
public SlotList list;

public GuiProtocolSelector(final GuiScreen parent) {
this.parent = parent;
}

@Override
public void initGui() {
super.initGui();
buttonList.add(new GuiButton(1, width / 2 - 100, height - 25, 200, 20, "Back"));
buttonList.add(new GuiButton(2, width / 2 - 180, height - 25, 75, 20, "Credits"));
list = new SlotList(mc, width, height, 32, height - 32, 10);
}

@Override
public void actionPerformed(final GuiButton guiButton){
list.actionPerformed(guiButton);

if (guiButton.id == 1) {
mc.displayGuiScreen(parent);
}
}

@Override
public void handleMouseInput() throws IOException {
list.handleMouseInput();
super.handleMouseInput();
}

@Override
public void drawScreen(final int mouseX, final int mouseY, final float partialTicks) {
list.drawScreen(mouseX, mouseY, partialTicks);
GlStateManager.pushMatrix();
GlStateManager.scale(2.0, 2.0, 2.0);
final String title = EnumChatFormatting.BOLD + "ViaMCP Reborn";
drawString(this.fontRendererObj, title, (this.width - (this.fontRendererObj.getStringWidth(title) * 2)) / 4, 5, -1);
GlStateManager.popMatrix();

final String versionName = ProtocolCollection.getProtocolById(ViaMCP.getInstance().getVersion()).getName();
final String versionCodeName = ProtocolCollection.getProtocolInfoById(ViaMCP.getInstance().getVersion()).getName();
final String versionReleaseDate = ProtocolCollection.getProtocolInfoById(ViaMCP.getInstance().getVersion()).getReleaseDate();
final String versionTitle = "Version: " + versionName + " - " + versionCodeName;
final String versionReleased = "Released: " + versionReleaseDate;

final int fixedHeight = ((5 + this.fontRendererObj.FONT_HEIGHT) * 2) + 2;

drawString(this.fontRendererObj, EnumChatFormatting.GRAY + (EnumChatFormatting.BOLD + "Version Information"), (width - this.fontRendererObj.getStringWidth("Version Information")) / 2, fixedHeight, -1);
drawString(this.fontRendererObj, versionTitle, (width - this.fontRendererObj.getStringWidth(versionTitle)) / 2, fixedHeight + this.fontRendererObj.FONT_HEIGHT, -1);
drawString(this.fontRendererObj, versionReleased, (width - this.fontRendererObj.getStringWidth(versionReleased)) / 2, fixedHeight + this.fontRendererObj.FONT_HEIGHT * 2, -1);

super.drawScreen(mouseX, mouseY, partialTicks);
}

class SlotList extends GuiSlot {
public SlotList(final Minecraft mc, final int width, final int height, final int top, final int bottom, final int slotHeight) {
super(mc, width, height, top + 30, bottom, 18);
}

@Override
public int getSize() {
return ProtocolCollection.values().length;
}

@Override
public void elementClicked(final int i, final boolean b, final int i1, final int i2) {
final int protocolVersion = ProtocolCollection.values()[i].getVersion().getVersion();
ViaMCP.getInstance().setVersion(protocolVersion);
ViaMCP.getInstance().asyncSlider.setVersion(protocolVersion);
}

@Override
public boolean isSelected(final int i) {
return false;
}

@Override
public void drawBackground() {
drawDefaultBackground();
}

@Override
public void drawSlot(final int i, final int i1, final int i2, final int i3, final int i4, final int i5) {
drawCenteredString(mc.fontRendererObj, (ViaMCP.getInstance().getVersion() == ProtocolCollection.values()[i].getVersion().getVersion() ? EnumChatFormatting.GREEN.toString() + EnumChatFormatting.BOLD : EnumChatFormatting.GRAY.toString()) + ProtocolCollection.getProtocolById(ProtocolCollection.values()[i].getVersion().getVersion()).getName(), width / 2, i2 + 2, -1);
GlStateManager.pushMatrix();
GlStateManager.scale(0.5, 0.5, 0.5);
drawCenteredString(mc.fontRendererObj, "PVN: " + ProtocolCollection.getProtocolById(ProtocolCollection.values()[i].getVersion().getVersion()).getVersion(), width, (i2 + 2) * 2 + 20, -1);
GlStateManager.popMatrix();
}
}
}
Loading

0 comments on commit 737fdca

Please sign in to comment.