This commit is contained in:
2024-09-24 07:05:00 +02:00
parent a3fa859804
commit 78b2a2f022
2 changed files with 310 additions and 13 deletions

View File

@@ -8,6 +8,7 @@ import (
"github.com/google/uuid"
"math"
"net"
"time"
)
type Version struct {
@@ -38,6 +39,29 @@ type ServerStatus struct {
EnforcesSecureChat bool `json:"enforcesSecureChat"`
}
type PlayerAbilites struct {
Invulnerable bool
Flying bool
AllowFlying bool
CreativeMode bool
}
func (abilities *PlayerAbilites) export() (out byte) {
if abilities.Invulnerable {
out |= 0x01
}
if abilities.Flying {
out |= 0x02
}
if abilities.AllowFlying {
out |= 0x04
}
if abilities.CreativeMode {
out |= 0x08
}
return
}
type Player struct {
conn net.Conn
@@ -59,6 +83,14 @@ type Player struct {
DeathDimension *Dimension
DeathPosition BlockPosition
Abilites PlayerAbilites
FlyingSpeed float32
FOVModifier float32
MessageIndex int32
CombatStart int64
CombatEnd int64
Position EntityPosition
GameMode uint8
@@ -88,6 +120,21 @@ type Player struct {
uuid uuid.UUID
}
func (player *Player) startCombat() {
player.CombatStart = time.Now().Unix()
_ = player.sendPacket(0x3B, &bytes.Buffer{})
}
func (player *Player) endCombat() {
player.CombatEnd = time.Now().Unix()
length := player.CombatEnd - player.CombatStart
if length >= 0 && player.CombatEnd != 0 && player.CombatStart != 0 && player.CombatEnd > player.CombatStart {
var buf bytes.Buffer
addInt64(&buf, length)
_ = player.sendPacket(0x3A, &buf)
}
}
type Dimension struct {
ID int32
Name string
@@ -370,6 +417,21 @@ func (slot *Slot) add(buf *bytes.Buffer) {
}
}
func (slot *Slot) addTrade(buf *bytes.Buffer) {
addVarint(buf, slot.Count)
if slot.Count > 0 {
addVarint(buf, slot.ItemID)
addVarint(buf, int32(len(slot.ComponentsToAdd)))
for componentID, component := range slot.ComponentsToAdd {
addVarint(buf, componentID)
component = component //TODO IMPLEMENT ALL THESE STUPID COMPONENTS
}
for _, componentID := range slot.ComponentsToRemove {
addVarint(buf, componentID)
}
}
}
// PalettedContainer represents a palette-based storage of entries.
type PalettedContainer struct {
BitsPerEntry uint8
@@ -621,16 +683,62 @@ func (velocity *Velocity) add(buf *bytes.Buffer) {
}
type Entity struct {
ID int32
UUID uuid.UUID
Type int32
Position EntityPosition
Rotation EntityRotation
Data int32
Velocity Velocity
Animation uint8
status int8
PortalCooldown int32
World *World
ID int32
UUID uuid.UUID
Type int32
Position EntityPosition
Rotation EntityRotation
Data int32
Velocity Velocity
Animation uint8
status int8
PortalCooldown int32
Vehicle *Entity
PlayersInformed []*Player
}
// Function to calculate the Euclidean distance between two points in 3D space
func distance(p1, p2 EntityPosition) float64 {
dx := p2.X - p1.X
dy := p2.Y - p1.Y
dz := p2.Z - p1.Z
return math.Sqrt(dx*dx + dy*dy + dz*dz)
}
func getDeltas(p1, p2 EntityPosition) []int16 {
return []int16{
int16((p2.X * 4096) - (p1.X * 4096)),
int16((p2.Y * 4096) - (p1.Y * 4096)),
int16((p2.Z * 4096) - (p1.Z * 4096)),
}
}
func (entity *Entity) move(newPos EntityPosition, withRotation bool) {
diff := distance(entity.Position, newPos)
if diff <= 7 {
if withRotation {
for _, player := range entity.PlayersInformed {
player.sendEntityPositionRotationUpdate(entity, getDeltas(entity.Position, newPos))
}
} else {
for _, player := range entity.PlayersInformed {
player.sendEntityPositionUpdate(entity, getDeltas(entity.Position, newPos))
}
}
} else {
for _, player := range entity.PlayersInformed {
//TODO make entity teleport
player = player
//player.sendEntityTeleport(entity, newPos)
}
}
entity.Position = newPos
}
func (entity *Entity) onGround() bool {
//TODO ADD THIS
return true
}
func (entity *Entity) add(buf *bytes.Buffer) {
@@ -714,3 +822,54 @@ func (Map *Map) add(buf *bytes.Buffer) {
}
}
}
type VillageTrade struct {
In1 Slot
In2 *Slot
Out Slot
Disabled bool
Uses int32
MaxUses int32
XP int32
Modifier int32
Multiplier float32
Demand int32
}
func (villageTrade *VillageTrade) add(buf *bytes.Buffer) {
villageTrade.In1.addTrade(buf)
villageTrade.Out.add(buf)
villageTrade.In2.addTrade(buf)
addBool(buf, villageTrade.Disabled)
addInt32(buf, villageTrade.Uses)
addInt32(buf, villageTrade.MaxUses)
addInt32(buf, villageTrade.XP)
addInt32(buf, villageTrade.Modifier)
addFloat32(buf, villageTrade.Multiplier)
addInt32(buf, villageTrade.Demand)
}
type VillagerEntity struct {
Entity *Entity
Trades []VillageTrade
Level int32
XP int32
Wandering bool
CanRestock bool
}
func (VillagerEntity *VillagerEntity) addOffers(buf *bytes.Buffer, windowID int32) {
addVarint(buf, windowID)
addVarint(buf, int32(len(VillagerEntity.Trades)))
for _, trade := range VillagerEntity.Trades {
trade.add(buf)
}
addVarint(buf, VillagerEntity.Level)
addVarint(buf, VillagerEntity.XP)
addBool(buf, !VillagerEntity.Wandering)
addBool(buf, VillagerEntity.CanRestock)
}
type Window struct {
ID int32
}