Всем привет,решил сделать систему веса для игрока.Написал все через капу,но когда в инвентаре итем значение не прибавляется.
Java:
public interface IWeightHandler {
float getWeight();
float getWeightMax();
void setWeight(float weight);
void setWeightMax(float weightMax);
void addWeight(float weight);
void removeWeight(float weight);
}
Java:
public class WeightEventHandler {
public static void register(){
MinecraftForge.EVENT_BUS.register(new WeightEventHandler());
}
@SubscribeEvent
public void attachCapabilities(AttachCapabilitiesEvent<Entity> event) {
if (event.getObject() instanceof EntityPlayer)
event.addCapability(new ResourceLocation("MODID", "NAME"), new WeightProvider());
}
@SubscribeEvent
public void clonePlayer(PlayerEvent.Clone event) {
final IWeightHandler original = getHandler(event.getOriginal());
final IWeightHandler clone = getHandler(event.getEntity());
clone.setWeight(original.getWeight());
}
public static IWeightHandler getHandler(Entity entity) {
if (entity.hasCapability(WeightProvider.CAPABILITY_WEIGHT, EnumFacing.DOWN))
return entity.getCapability(WeightProvider.CAPABILITY_WEIGHT, EnumFacing.DOWN);
return null;
}
}
Java:
public class MessageWeight implements IMessage {
public float weight;
public float weight_max;
public MessageWeight() {}
public MessageWeight(float weight_max, float weight) {
this.weight = weight;
this.weight_max = weight_max;
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeFloat(weight);
buf.writeFloat(weight_max);
}
@Override
public void fromBytes(ByteBuf buf) {
weight = buf.readFloat();
weight_max = buf.readFloat();
}
//----------------------------------------------------------------------------------
public static class Handler implements IMessageHandler<MessageWeight, IMessage> {
@Override
public IMessage onMessage(MessageWeight packet, MessageContext context) {
float weight = packet.weight;
float weight_max = packet.weight_max;
EntityPlayer player = Minecraft.getMinecraft().player;
WeightEventHandler.getHandler(player).setWeightMax(weight_max);
WeightEventHandler.getHandler(player).setWeight(weight);
return null;
}
}
}
Java:
public class DefaultWeightHandler implements IWeightHandler {
private float weight = 0;
private float weightMax= 25;
@Override
public float getWeight() {
return this.weight;
}
@Override
public float getWeightMax() {
return this.weightMax;
}
@Override
public void setWeight (float weight) {
this.weight = weight;
}
@Override
public void setWeightMax (float weightMax) {
this.weightMax = weightMax;
}
@Override
public void addWeight (float weight) {
this.weight += weight;
}
@Override
public void removeWeight(float weight) {
this.weight -= weight;
if (this.weight < 0)
this.weight = 0;
}
}
вот так нахожу вес:
public class WeightSystem {
public static float checkWeighting(ItemStack slot,EntityPlayer player) {
float weighting = WeightEventHandler.getHandler(player).getWeight();
if (slot != null) {
Item item = slot.getItem();
if (WeightVanilaItems.getVanilaItem(item, slot.getItemDamage()) != 0.0F) {
weighting += WeightVanilaItems.getVanilaItem(item, slot.getItemDamage()*slot.getCount());
}
WeightEventHandler.getHandler(player).addWeight(weighting);
}
return weighting;
}
}
Последнее редактирование: