Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
Add versioned controllers for 1.20.2
Browse files Browse the repository at this point in the history
  • Loading branch information
rettichlp committed Nov 28, 2023
1 parent c169178 commit 66f896b
Show file tree
Hide file tree
Showing 9 changed files with 754 additions and 1 deletion.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ labyMod {
"1.19.2",
"1.19.3",
"1.19.4",
"1.20.1"
"1.20.1",
"1.20.2"
) { version, provider ->
configureRun(provider, version)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.rettichlp.unicacityaddon.v1_20_2;

import com.rettichlp.unicacityaddon.UnicacityAddon;
import com.rettichlp.unicacityaddon.base.enums.faction.Faction;
import com.rettichlp.unicacityaddon.base.text.ColorCode;
import com.rettichlp.unicacityaddon.base.text.Message;
import com.rettichlp.unicacityaddon.controller.DeadBodyController;
import net.labymod.api.models.Implements;
import net.labymod.api.util.math.vector.FloatVector3;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextColor;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.Items;
import net.minecraft.world.phys.AABB;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

/**
* @author RettichLP
*/
@Singleton
@Implements(DeadBodyController.class)
public class VersionedDeadBodyController extends DeadBodyController {

/**
* When a corpse is rendered, the display name is automatically changed. The name can reflect the current wanted
* level. However, if this state changes while the corpse has already been rendered, the color will not change. In
* order to ensure this change anyway, the name must be available in every render process. In order not to waste
* performance by calculating the name in every render process, the name that was calculated when the corpse was
* first rendered is used. Likewise, the status of whether the corpse can be revived.
*/
private final Map<UUID, Map.Entry<String, Boolean>> corpseMap = new HashMap<>();

@Inject
public VersionedDeadBodyController() {
}

@Override
public void updateDisplayName(UnicacityAddon unicacityAddon) {
FloatVector3 location = unicacityAddon.player().getLocation();

assert location != null;
AABB aabb = new AABB(
location.getX() - 50,
location.getY() - 50,
location.getZ() - 50,
location.getX() + 50,
location.getY() + 50,
location.getZ() + 50
);

assert Minecraft.getInstance().level != null;
Minecraft.getInstance().level.getEntitiesOfClass(ItemEntity.class, aabb, itemEntity -> itemEntity != null && itemEntity.hasCustomName() && itemEntity.getItem().getItem().equals(Items.SKELETON_SKULL)).forEach(itemEntity -> {
Component customName = itemEntity.getCustomName();

assert customName != null;
List<Component> siblings = customName.getSiblings();

// get player name and revivable status
String playerName;
boolean nonRevivable;
if (!siblings.isEmpty()) { // sibling size only by not formatted corpses greater than 0
Component originalCorpseName = siblings.get(0);
playerName = originalCorpseName.getContents().toString().substring(1);
nonRevivable = Objects.equals(originalCorpseName.getStyle().getColor(), TextColor.fromLegacyFormat(ChatFormatting.DARK_GRAY));
this.corpseMap.put(itemEntity.getUUID(), new AbstractMap.SimpleEntry<>(playerName, nonRevivable));
} else {
Map.Entry<String, Boolean> corpse = this.corpseMap.getOrDefault(itemEntity.getUUID(), new AbstractMap.SimpleEntry<>("Unbekannt", false));
playerName = corpse.getKey();
nonRevivable = corpse.getValue();
}

// use player name and revivable status
String prefix = unicacityAddon.nameTagService().getPrefix(playerName, true);
String factionInfo = unicacityAddon.configuration().nametag().info().get() ? unicacityAddon.api().getPlayerFactionMap().getOrDefault(playerName, Faction.NULL).getNameTagSuffix() : "";

String ndn = Message.getBuilder()
.of("✟").color(nonRevivable ? ColorCode.DARK_GRAY : ColorCode.GRAY).advance()
.of((nonRevivable ? ColorCode.DARK_GRAY.getCode() : prefix) + playerName).advance().space()
.of(factionInfo).advance()
.create();

itemEntity.setCustomName(Component.nullToEmpty(ndn));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
package com.rettichlp.unicacityaddon.v1_20_2;

import com.rettichlp.unicacityaddon.UnicacityAddon;
import com.rettichlp.unicacityaddon.base.enums.faction.DrugPurity;
import com.rettichlp.unicacityaddon.base.enums.faction.DrugType;
import com.rettichlp.unicacityaddon.controller.GuiController;
import com.rettichlp.unicacityaddon.listener.ScreenRenderListener;
import it.unimi.dsi.fastutil.ints.Int2ObjectMaps;
import net.labymod.api.models.Implements;
import net.labymod.api.nbt.NBTTagType;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.ContainerScreen;
import net.minecraft.client.gui.screens.inventory.HopperScreen;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.network.protocol.game.ServerboundContainerClickPacket;
import net.minecraft.world.inventory.ChestMenu;
import net.minecraft.world.inventory.ClickType;
import net.minecraft.world.inventory.HopperMenu;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;

import javax.inject.Singleton;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author RettichLP
*/
@Singleton
@Implements(GuiController.class)
public class VersionedGuiController extends GuiController {

private int containerId = 0;

@Override
public int getSlotNumberByDisplayName(String displayName) {
int slotNumber = -1;

Screen screen = Minecraft.getInstance().screen;
if (screen instanceof ContainerScreen containerScreen) {
ChestMenu chestMenu = containerScreen.getMenu();

NonNullList<ItemStack> itemStacks = chestMenu.getItems();
Optional<ItemStack> hoveredItemStackOptional = itemStacks.stream()
.filter(itemStack -> itemStack.getDisplayName().getString().contains(displayName))
.findFirst();

if (hoveredItemStackOptional.isPresent()) {
slotNumber = itemStacks.indexOf(hoveredItemStackOptional.get());
}
}

return slotNumber;
}

@Override
public @Nullable String getContainerLegacyName() {
return null;
}

@Override
public int getContainerId() {
return 0;
}

@Override
public void inventoryClick(int slotNumber) {
Screen screen = Minecraft.getInstance().screen;

this.containerId = 0;
if (screen instanceof ContainerScreen containerScreen) {
this.containerId = containerScreen.getMenu().containerId;
} else if (screen instanceof HopperScreen hopperScreen) {
this.containerId = hopperScreen.getMenu().containerId;
}

LocalPlayer localPlayer = Minecraft.getInstance().player;
assert localPlayer != null;

ServerboundContainerClickPacket serverboundContainerClickPacket = new ServerboundContainerClickPacket(this.containerId, 0, slotNumber, 1, ClickType.PICKUP, localPlayer.containerMenu.getCarried(), Int2ObjectMaps.emptyMap());
localPlayer.connection.send(serverboundContainerClickPacket);
}

@Override
public void updateDrugInventoryMap(UnicacityAddon unicacityAddon) {
Screen screen = Minecraft.getInstance().screen;
if (screen instanceof ContainerScreen containerScreen) {
ChestMenu chestMenu = containerScreen.getMenu();

this.containerId = chestMenu.containerId;
if (unicacityAddon.utilService().command().getLastWindowId() == this.containerId)
return;

unicacityAddon.utilService().command().setLastWindowId(this.containerId);

if (unicacityAddon.utilService().command().isCocaineCheck()) {
unicacityAddon.utilService().command().setCocaineCheck(false);
// select cocaine to check drug purity
this.inventoryClick(0);
} else if (unicacityAddon.utilService().command().isMarihuanaCheck()) {
unicacityAddon.utilService().command().setMarihuanaCheck(false);
// select marihuana to check drug purity
this.inventoryClick(1);
} else if (unicacityAddon.utilService().command().isMethCheck()) {
unicacityAddon.utilService().command().setMethCheck(false);
// select meth to check drug purity
this.inventoryClick(2);
} else {
chestMenu.getItems().stream()
.filter(itemStack -> !itemStack.isEmpty() && !itemStack.getDisplayName().getString().contains("Pulver") && !itemStack.getDisplayName().getString().contains("Kräuter") && !itemStack.getDisplayName().getString().contains("Kristalle"))
.forEach(itemStack -> {
assert itemStack.getTag() != null;
CompoundTag compoundTag = itemStack.getTag().getCompound("display");
String lore = compoundTag.getList("Lore", NBTTagType.STRING.getId()).getString(0);

Matcher loreMatcher = Pattern.compile("» (?<amount>\\d+)(g| Pillen| Flaschen| Päckchen| Stück| Kisten)").matcher(lore);
if (loreMatcher.find()) {
int amount = Integer.parseInt(loreMatcher.group("amount"));
DrugType drugType = DrugType.getDrugType(itemStack.getDisplayName().getString());

if (drugType != null) {
Map<DrugType, Map<DrugPurity, Integer>> drugInventoryMap = unicacityAddon.fileService().data().getDrugInventoryMap();
Map<DrugPurity, Integer> drugPurityMap = drugInventoryMap.getOrDefault(drugType, new HashMap<>());
drugPurityMap.put(DrugPurity.BEST, amount);
drugInventoryMap.put(drugType, drugPurityMap);
unicacityAddon.fileService().data().setDrugInventoryMap(drugInventoryMap);
}
}
});

unicacityAddon.utilService().command().setActiveDrugInventoryLoading(false);
assert Minecraft.getInstance().player != null;
Minecraft.getInstance().player.closeContainer();
}
} else if (screen instanceof HopperScreen hopperScreen && unicacityAddon.utilService().command().isActiveDrugInventoryLoading()) {
HopperMenu hopperMenu = hopperScreen.getMenu();

this.containerId = hopperMenu.containerId;
if (unicacityAddon.utilService().command().getLastWindowId() == this.containerId)
return;

unicacityAddon.utilService().command().setLastWindowId(this.containerId);

hopperMenu.getItems().stream()
.filter(itemStack -> !itemStack.isEmpty())
.forEach(itemStack -> {
assert itemStack.getTag() != null;
CompoundTag compoundTag = itemStack.getTag().getCompound("display");
ListTag lore = compoundTag.getList("Lore", NBTTagType.STRING.getId());
String drugPurityNbt = lore.getString(1);
String amountNbt = lore.getString(2);

Matcher loreMatcher = Pattern.compile("» (?<amount>\\d+)g").matcher(amountNbt);
if (loreMatcher.find()) {
int amount = Integer.parseInt(loreMatcher.group("amount"));
DrugType drugType = DrugType.getDrugType(itemStack.getDisplayName().getString());
DrugPurity drugPurity = DrugPurity.getDrugPurity(drugPurityNbt);

if (drugType != null) {
Map<DrugType, Map<DrugPurity, Integer>> drugInventoryMap = unicacityAddon.fileService().data().getDrugInventoryMap();
Map<DrugPurity, Integer> drugPurityMap = drugInventoryMap.getOrDefault(drugType, new HashMap<>());
drugPurityMap.put(drugPurity, amount);
drugInventoryMap.put(drugType, drugPurityMap);
unicacityAddon.fileService().data().setDrugInventoryMap(drugInventoryMap);
}
}
});

// go back to inventory container
this.inventoryClick(4);
}
}

@Override
public void setSelectedHotbarSlot(int slotNumber) {
assert Minecraft.getInstance().player != null;
Minecraft.getInstance().player.getInventory().selected = slotNumber;
}

@Override
public void updateSetting(boolean expectedValue) {
Screen screen = Minecraft.getInstance().screen;
if (screen instanceof ContainerScreen containerScreen && !ScreenRenderListener.settingPath.isEmpty()) {
ChestMenu chestMenu = containerScreen.getMenu();

if (chestMenu.containerId != this.containerId) {
this.containerId = chestMenu.containerId;

if (ScreenRenderListener.settingPath.size() > 1) {
this.inventoryClick(ScreenRenderListener.settingPath.remove(0));
} else {
int slotNumber = ScreenRenderListener.settingPath.remove(0);
ItemStack itemStack = chestMenu.getItems().get(slotNumber);
assert itemStack.getTag() != null;
CompoundTag compoundTag = itemStack.getTag().getCompound("display");
ListTag lore = compoundTag.getList("Lore", NBTTagType.STRING.getId());
if (lore.getString(1).equals("§cAktiviere den Hitsound") && expectedValue) {
this.inventoryClick(slotNumber);
}
assert Minecraft.getInstance().player != null;
Minecraft.getInstance().player.closeContainer();
}
}
}
}

@Override
public boolean containsItemContainingString(String name) {
NonNullList<ItemStack> itemStackNonNullList = null;

Screen screen = Minecraft.getInstance().screen;
if (screen instanceof ContainerScreen containerScreen) {
itemStackNonNullList = containerScreen.getMenu().getItems();
} else if (screen instanceof HopperScreen hopperScreen) {
itemStackNonNullList = hopperScreen.getMenu().getItems();
}

return itemStackNonNullList != null && itemStackNonNullList.stream().anyMatch(itemStack -> itemStack.getDisplayName().getContents().toString().contains(name));
}
}
Loading

0 comments on commit 66f896b

Please sign in to comment.