Skip to content

Commit

Permalink
complete trade station owner side
Browse files Browse the repository at this point in the history
  • Loading branch information
IAFEnvoy committed Dec 23, 2024
1 parent 9652fdf commit b50151e
Show file tree
Hide file tree
Showing 17 changed files with 349 additions and 85 deletions.
92 changes: 50 additions & 42 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,76 +1,84 @@
plugins {
id 'fabric-loom' version '1.7-SNAPSHOT'
id 'maven-publish'
id 'fabric-loom' version '1.7-SNAPSHOT'
id 'maven-publish'
}

version = project.mod_version
group = project.maven_group

base {
archivesName = project.archives_base_name
archivesName = project.archives_base_name
}

repositories {
// Add repositories to retrieve artifacts from in here.
// You should only use this when depending on other mods because
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
// for more information about repositories.
// Add repositories to retrieve artifacts from in here.
// You should only use this when depending on other mods because
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
// for more information about repositories.
repositories {
maven {
name = "Ladysnake Mods"
url = 'https://maven.ladysnake.org/releases'
}
}
}

dependencies {
// To change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
// To change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

modImplementation(include("dev.onyxstudios.cardinal-components-api:cardinal-components-base:${project.cca_version}"))
modImplementation(include("dev.onyxstudios.cardinal-components-api:cardinal-components-block:${project.cca_version}"))
}

processResources {
inputs.property "version", project.version
inputs.property "version", project.version

filesMatching("fabric.mod.json") {
expand "version": project.version
}
filesMatching("fabric.mod.json") {
expand "version": project.version
}
}

tasks.withType(JavaCompile).configureEach {
it.options.release = 17
it.options.release = 17
}

java {
// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
// if it is present.
// If you remove this line, sources will not be generated.
withSourcesJar()
// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
// if it is present.
// If you remove this line, sources will not be generated.
withSourcesJar()

sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

jar {
from("LICENSE") {
rename { "${it}_${project.base.archivesName.get()}"}
}
from("LICENSE") {
rename { "${it}_${project.base.archivesName.get()}" }
}
}

// configure the maven publication
publishing {
publications {
create("mavenJava", MavenPublication) {
artifactId = project.archives_base_name
from components.java
}
}
publications {
create("mavenJava", MavenPublication) {
artifactId = project.archives_base_name
from components.java
}
}

// See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing.
repositories {
// Add repositories to publish to here.
// Notice: This block does NOT have the same function as the block in the top level.
// The repositories here will be used for publishing your artifact, not for
// retrieving dependencies.
}
// See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing.
repositories {
// Add repositories to publish to here.
// Notice: This block does NOT have the same function as the block in the top level.
// The repositories here will be used for publishing your artifact, not for
// retrieving dependencies.
}
}
6 changes: 2 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# Done to increase the memory available to gradle.
org.gradle.jvmargs=-Xmx1G
org.gradle.parallel=true

# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.20.1
yarn_mappings=1.20.1+build.10
loader_version=0.16.9

# Mod Properties
mod_version=1.0.0
maven_group=com.iafenvoy.nee
archives_base_name=not_enough_economy

# Dependencies
fabric_version=0.92.2+1.20.1
fabric_version=0.92.2+1.20.1
cca_version=5.2.1
11 changes: 11 additions & 0 deletions src/main/java/com/iafenvoy/nee/NotEnoughEconomyClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.iafenvoy.nee;

import com.iafenvoy.nee.registry.NeeRenderers;
import net.fabricmc.api.ClientModInitializer;

public final class NotEnoughEconomyClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
NeeRenderers.registerBlockEntityRenderers();
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/iafenvoy/nee/component/ModComponentEntry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.iafenvoy.nee.component;

import com.iafenvoy.nee.item.block.entity.TradeStationBlockEntity;
import dev.onyxstudios.cca.api.v3.block.BlockComponentFactoryRegistry;
import dev.onyxstudios.cca.api.v3.block.BlockComponentInitializer;

public class ModComponentEntry implements BlockComponentInitializer {
@Override
public void registerBlockComponentFactories(BlockComponentFactoryRegistry registry) {
registry.registerFor(TradeStationBlockEntity.class, TradeStationComponent.COMPONENT, TradeStationComponent::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.iafenvoy.nee.component;

import com.iafenvoy.nee.NotEnoughEconomy;
import com.iafenvoy.nee.item.block.entity.TradeStationBlockEntity;
import dev.onyxstudios.cca.api.v3.component.ComponentKey;
import dev.onyxstudios.cca.api.v3.component.ComponentRegistry;
import dev.onyxstudios.cca.api.v3.component.ComponentV3;
import dev.onyxstudios.cca.api.v3.component.sync.AutoSyncedComponent;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.Identifier;

public class TradeStationComponent implements ComponentV3, AutoSyncedComponent {
public static final ComponentKey<TradeStationComponent> COMPONENT = ComponentRegistry.getOrCreate(Identifier.of(NotEnoughEconomy.MOD_ID, "trade_station"), TradeStationComponent.class);
private final TradeStationBlockEntity blockEntity;

public TradeStationComponent(TradeStationBlockEntity blockEntity) {
this.blockEntity = blockEntity;
}

@Override
public void readFromNbt(NbtCompound tag) {
this.blockEntity.readFromNbt(tag);
}

@Override
public void writeToNbt(NbtCompound tag) {
this.blockEntity.writeToNbt(tag);
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/iafenvoy/nee/item/block/TradeStationBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import net.minecraft.block.BlockWithEntity;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
Expand All @@ -33,6 +35,14 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
return super.onUse(state, world, pos, player, hand, hit);
}

@Override
public void onPlaced(World world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack itemStack) {
super.onPlaced(world, pos, state, placer, itemStack);
if (world.getBlockEntity(pos) instanceof TradeStationBlockEntity blockEntity && placer instanceof PlayerEntity player)
blockEntity.setOwner(player.getUuid());

}

@Override
public BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.MODEL;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.iafenvoy.nee.item.block.entity;

import com.iafenvoy.nee.component.TradeStationComponent;
import com.iafenvoy.nee.registry.NeeBlockEntities;
import com.iafenvoy.nee.screen.handler.TradeStationOwnerScreenHandler;
import com.iafenvoy.nee.screen.inventory.ImplementedInventory;
import com.mojang.authlib.GameProfile;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
Expand All @@ -11,6 +15,9 @@
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerContext;
Expand All @@ -26,9 +33,15 @@

public class TradeStationBlockEntity extends BlockEntity implements NamedScreenHandlerFactory {
private static final String OWNER_KEY = "owner";
private static final String OWNER_NAME_KEY = "owner_name";
@Nullable
private UUID owner;
private final DefaultedList<ItemStack> coins = DefaultedList.ofSize(27, ItemStack.EMPTY), goods = DefaultedList.ofSize(12, ItemStack.EMPTY), forSell = DefaultedList.ofSize(1, ItemStack.EMPTY);
@Nullable
private String ownerNameCache;
private final DefaultedList<ItemStack> left = DefaultedList.ofSize(12, ItemStack.EMPTY);
private final DefaultedList<ItemStack> right = DefaultedList.ofSize(12, ItemStack.EMPTY);
private final DefaultedList<ItemStack> inventory = DefaultedList.ofSize(21, ItemStack.EMPTY);
private final DefaultedList<ItemStack> display = DefaultedList.ofSize(1, ItemStack.EMPTY);

public TradeStationBlockEntity(BlockPos pos, BlockState state) {
super(NeeBlockEntities.TRADE_STATION, pos, state);
Expand All @@ -37,19 +50,31 @@ public TradeStationBlockEntity(BlockPos pos, BlockState state) {
@Override
public void readNbt(NbtCompound nbt) {
super.readNbt(nbt);
if (nbt.contains(OWNER_KEY, NbtElement.INT_ARRAY_TYPE)) this.setOwner(nbt.getUuid(OWNER_KEY));
Inventories.readNbt(nbt.getCompound("coins"), this.coins);
Inventories.readNbt(nbt.getCompound("goods"), this.goods);
Inventories.readNbt(nbt.getCompound("forSell"), this.forSell);
this.readFromNbt(nbt);
Inventories.readNbt(nbt.getCompound("left"), this.left);
Inventories.readNbt(nbt.getCompound("right"), this.right);
Inventories.readNbt(nbt.getCompound("inventory"), this.inventory);
}

@Override
protected void writeNbt(NbtCompound nbt) {
super.writeNbt(nbt);
if (this.getOwner() != null) nbt.putUuid(OWNER_KEY, this.getOwner());
nbt.put("coins", Inventories.writeNbt(new NbtCompound(), this.coins));
nbt.put("goods", Inventories.writeNbt(new NbtCompound(), this.goods));
nbt.put("forSell", Inventories.writeNbt(new NbtCompound(), this.forSell));
this.writeToNbt(nbt);
nbt.put("left", Inventories.writeNbt(new NbtCompound(), this.left));
nbt.put("right", Inventories.writeNbt(new NbtCompound(), this.right));
nbt.put("inventory", Inventories.writeNbt(new NbtCompound(), this.inventory));
}

public void writeToNbt(NbtCompound nbt) {
if (this.owner != null) nbt.putUuid(OWNER_KEY, this.owner);
if (this.ownerNameCache != null) nbt.putString(OWNER_NAME_KEY, this.ownerNameCache);
nbt.put("display", Inventories.writeNbt(new NbtCompound(), this.display));
}

public void readFromNbt(NbtCompound nbt) {
if (nbt.contains(OWNER_KEY, NbtElement.INT_ARRAY_TYPE)) this.owner = nbt.getUuid(OWNER_KEY);
if (nbt.contains(OWNER_NAME_KEY, NbtElement.STRING_TYPE)) this.ownerNameCache = nbt.getString(OWNER_NAME_KEY);
Inventories.readNbt(nbt.getCompound("display"), this.display);
}

public @Nullable UUID getOwner() {
Expand All @@ -58,20 +83,56 @@ protected void writeNbt(NbtCompound nbt) {

public void setOwner(@Nullable UUID owner) {
this.owner = owner;
this.ownerNameCache = Optional.ofNullable(this.getWorld()).map(x -> x.getPlayerByUuid(owner)).map(PlayerEntity::getGameProfile).map(GameProfile::getName).orElse(null);
this.markDirty();
TradeStationComponent.COMPONENT.sync(this);
}

public Text getFloatingName() {
String ownerName = this.getOwnerName();
if (ownerName != null)
return Text.translatable("screen.not_enough_economy.trade_station_float", ownerName);
return Text.translatable("screen.not_enough_economy.trade_station_empty");
}

@Override
public Text getDisplayName() {
return Text.translatable("screen.not_enough_economy.trade_station");
String ownerName = this.getOwnerName();
if (ownerName != null)
return Text.translatable("screen.not_enough_economy.trade_station", ownerName);
return Text.translatable("screen.not_enough_economy.trade_station_empty");
}

@Override
public @Nullable ScreenHandler createMenu(int syncId, PlayerInventory playerInventory, PlayerEntity player) {
return new TradeStationOwnerScreenHandler(syncId, playerInventory, ImplementedInventory.of(this.coins), ImplementedInventory.of(this.goods), new ScreenHandlerContext() {
@Override
public <T> Optional<T> get(BiFunction<World, BlockPos, T> getter) {
return Optional.of(getter.apply(TradeStationBlockEntity.this.world, TradeStationBlockEntity.this.pos));
}
});
return new TradeStationOwnerScreenHandler(syncId, playerInventory,
ImplementedInventory.of(this.left, this::markDirty),
ImplementedInventory.of(this.right, this::markDirty),
ImplementedInventory.of(this.inventory, this::markDirty),
ImplementedInventory.of(this.display, () -> {
this.markDirty();
TradeStationComponent.COMPONENT.sync(this);
}),
new ScreenHandlerContext() {
@Override
public <T> Optional<T> get(BiFunction<World, BlockPos, T> getter) {
return Optional.of(getter.apply(TradeStationBlockEntity.this.world, TradeStationBlockEntity.this.pos));
}
});
}

public ItemStack getDisplayStack() {
return this.display.get(0);
}

@Nullable
public String getOwnerName() {
if (this.getOwner() == null) return this.ownerNameCache;
return Optional.ofNullable(this.getWorld()).map(x -> x.getPlayerByUuid(this.getOwner())).map(PlayerEntity::getGameProfile).map(GameProfile::getName).orElse(this.ownerNameCache);
}

@Override
public @Nullable Packet<ClientPlayPacketListener> toUpdatePacket() {
return BlockEntityUpdateS2CPacket.create(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;

public class NeeBlockEntities {
public final class NeeBlockEntities {
public static final BlockEntityType<TradeStationBlockEntity> TRADE_STATION = register("trade_station", BlockEntityType.Builder.create(TradeStationBlockEntity::new, NeeBlocks.TRADE_STATION).build(null));

public static <T extends BlockEntity> BlockEntityType<T> register(String id, BlockEntityType<T> type) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/iafenvoy/nee/registry/NeeRenderers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.iafenvoy.nee.registry;

import com.iafenvoy.nee.render.TradeStationBlockEntityRenderer;
import net.fabricmc.fabric.impl.client.rendering.BlockEntityRendererRegistryImpl;

public final class NeeRenderers {
public static void registerBlockEntityRenderers(){
BlockEntityRendererRegistryImpl.register(NeeBlockEntities.TRADE_STATION, TradeStationBlockEntityRenderer::new);
}
}
Loading

0 comments on commit b50151e

Please sign in to comment.