Skip to content

Commit

Permalink
!!! TRADE OFFER !!!
Browse files Browse the repository at this point in the history
  • Loading branch information
TropheusJ committed Apr 2, 2024
1 parent 2775b3e commit 41e9706
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.tropheusj.its_as_shrimple_as_that.arrow.ShrimpArrowEntity;
import io.github.tropheusj.its_as_shrimple_as_that.command.KrillCommand;
import io.github.tropheusj.its_as_shrimple_as_that.entity.ShrimpEntity;
import io.github.tropheusj.its_as_shrimple_as_that.item.FriedRiceItem;
import net.fabricmc.api.ModInitializer;

import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
Expand All @@ -19,8 +20,10 @@
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.ArrowItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Item.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -40,6 +43,8 @@ public class ItsAsShrimpleAsThat implements ModInitializer {
.dimensions(EntityDimensions.fixed(0.5f, 0.5f))
.build();

public static final Item FRIED_RICE = new FriedRiceItem(FriedRiceItem.makeProperties());

public static final Item SHRIMP_ARROW = new ArrowItem(new Item.Properties());

public static final MobEffect KRILLED = new MobEffect(MobEffectCategory.BENEFICIAL, 1){}.addAttributeModifier(
Expand All @@ -52,6 +57,7 @@ public void onInitialize() {
Registry.register(BuiltInRegistries.ENTITY_TYPE, id("shrimp"), SHRIMP_TYPE);
Registry.register(BuiltInRegistries.ENTITY_TYPE, id("shrimp_arrow"), SHRIMP_ARROW_TYPE);
Registry.register(BuiltInRegistries.ITEM, id("shrimp_arrow"), SHRIMP_ARROW);
Registry.register(BuiltInRegistries.ITEM, id("fried_rice"), FRIED_RICE);
Registry.register(BuiltInRegistries.MOB_EFFECT, id("krilled"), KRILLED);

CommandRegistrationCallback.EVENT.register(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

import java.util.Optional;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import net.minecraft.core.BlockPos;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.network.syncher.SynchedEntityData.Builder;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.PathfinderMob;
Expand All @@ -19,6 +26,9 @@
public class ShrimpEntity extends PathfinderMob {
public static final EntityDataAccessor<Optional<BlockPos>> WORKSTATION = SynchedEntityData.defineId(ShrimpEntity.class, EntityDataSerializers.OPTIONAL_BLOCK_POS);

@Nullable
private ShrimpMerchant merchant;

public ShrimpEntity(EntityType<? extends ShrimpEntity> type, Level level) {
super(type, level);
}
Expand All @@ -35,6 +45,43 @@ protected void registerGoals() {
this.goalSelector.addGoal(5, new LookAtPlayerGoal(this, Player.class, 10, 0.1f));
}

@Override
public void onSyncedDataUpdated(EntityDataAccessor<?> data) {
super.onSyncedDataUpdated(data);
if (data == WORKSTATION) {
Optional<BlockPos> workstation = this.entityData.get(WORKSTATION);
this.merchant = workstation.isPresent() ? new ShrimpMerchant() : null;
}
}

@Override
@NotNull
protected InteractionResult mobInteract(Player player, InteractionHand interactionHand) {
if (!this.level().isClientSide && this.merchant != null && !this.merchant.isTrading()) {
this.merchant.setTradingPlayer(player);
this.merchant.openTradingScreen(player, this.getDisplayName(), 0);
return InteractionResult.CONSUME;
}
return InteractionResult.PASS;
}

@Override
public void remove(RemovalReason removalReason) {
super.remove(removalReason);
if (this.merchant != null) {
this.merchant.setTradingPlayer(null);
}
}

@Nullable
@Override
public Entity changeDimension(ServerLevel serverLevel, boolean bl) {
if (this.merchant != null) {
this.merchant.setTradingPlayer(null);
}
return super.changeDimension(serverLevel, bl);
}

public void becomeChef(BlockPos workstation) {
this.entityData.set(WORKSTATION, Optional.of(workstation));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package io.github.tropheusj.its_as_shrimple_as_that.entity;

import io.github.tropheusj.its_as_shrimple_as_that.ItsAsShrimpleAsThat;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.trading.ItemCost;
import net.minecraft.world.item.trading.Merchant;
import net.minecraft.world.item.trading.MerchantOffer;
import net.minecraft.world.item.trading.MerchantOffers;

public class ShrimpMerchant implements Merchant {
@Nullable
private Player tradingPlayer;

private MerchantOffers offers;

public ShrimpMerchant() {
this.offers = new MerchantOffers();
this.offers.add(itemForGems(ItsAsShrimpleAsThat.FRIED_RICE, 2));
}

private MerchantOffer itemForGems(Item item, int count) {
return new MerchantOffer(
new ItemCost(Items.AMBER_GEM, count),
new ItemStack(item),
Integer.MAX_VALUE, // max uses
1, // xp
1 // price multiplier
);
}

@Override
public void setTradingPlayer(@Nullable Player player) {
this.tradingPlayer = player;
}

@Nullable
@Override
public Player getTradingPlayer() {
return this.tradingPlayer;
}

public boolean isTrading() {
return this.tradingPlayer != null;
}

@Override
@NotNull
public MerchantOffers getOffers() {
return this.offers;
}

@Override
public void overrideOffers(MerchantOffers merchantOffers) {
this.offers = merchantOffers;
}

@Override
public void notifyTrade(MerchantOffer merchantOffer) {
merchantOffer.increaseUses();
}

@Override
@NotNull
public SoundEvent getNotifyTradeSound() {
return SoundEvents.VILLAGER_YES;
}

@Override
public boolean isClientSide() {
return false;
}

// don't care

@Override
public void notifyTradeUpdated(ItemStack itemStack) {
}

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

@Override
public void overrideXp(int i) {
}

@Override
public boolean showProgressBar() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import org.jetbrains.annotations.NotNull;

import net.minecraft.client.renderer.entity.EntityRendererProvider.Context;
import net.minecraft.client.renderer.entity.LivingEntityRenderer;
import net.minecraft.client.renderer.entity.MobRenderer;
import net.minecraft.resources.ResourceLocation;

public class ShrimpRenderer extends LivingEntityRenderer<ShrimpEntity, ShrimpModel<ShrimpEntity>> {
public class ShrimpRenderer extends MobRenderer<ShrimpEntity, ShrimpModel<ShrimpEntity>> {
public static final ResourceLocation TEXTURE = ItsAsShrimpleAsThat.id("textures/entity/shrimp/shrimp.png");
public static final ResourceLocation CHEF_TEXTURE = ItsAsShrimpleAsThat.id("textures/entity/shrimp/chef_shrimp.png");
public static final float SHADOW_RADIUS = 0.5f;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.tropheusj.its_as_shrimple_as_that.item;

import org.jetbrains.annotations.NotNull;

import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.stats.Stats;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;

public class FriedRiceItem extends Item {
public FriedRiceItem(Properties properties) {
super(properties);
}

public static Properties makeProperties() {
return new Properties().stacksTo(1).food(new FoodProperties.Builder().nutrition(5).build());
}

@Override
@NotNull
public ItemStack finishUsingItem(ItemStack stack, Level level, LivingEntity entity) {
if (entity instanceof ServerPlayer player) {
CriteriaTriggers.CONSUME_ITEM.trigger(player, stack);
player.awardStat(Stats.ITEM_USED.get(this));
}

stack.consume(1, entity);

return stack.isEmpty() ? new ItemStack(Items.BOWL) : stack;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"item.its_as_shrimple_as_that.fried_rice": "Fried Rice",

"entity.its_as_shrimple_as_that.shrimp": "Shrimp",

"commands.krill.success.single": "Krilled %s",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "its_as_shrimple_as_that:item/fried_rice"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 41e9706

Please sign in to comment.